在 Cordova 中处理推送通知

improve this page | report issue


概述

在 iOS、Android 和 Windows Cordova 应用程序可以接收和显示推送通知之前,需要将 cordova-plugin-mfp-push Cordova 插件添加到 Cordova 项目中。 在配置应用程序后,可以使用 MobileFirst 提供的通知 API 来注册和注销设备、预订和取消预订标记以及处理通知。 在本教程中,您将学会如何在 Cordova 应用程序中处理推送通知。

注:由于还存在缺陷,Cordova 应用程序当前不支持已认证的通知。 但是,提供了以下变通方法:可通过 WLAuthorizationManager.obtainAccessToken("push.mobileclient").then( ... ); 来包装每个 MFPPush API 调用。 提供的样本应用程序中使用了此变通方法。

有关 iOS 中的静默通知或交互式通知的信息,请参阅:

先决条件:

跳转至

通知配置

创建新的 Cordova 项目或使用现有项目,并添加一个或多个受支持的平台:iOS、Android 或 Windows。

如果该项目中还没有 MobileFirst Cordova SDK,请遵循将 Mobile Foundation SDK 添加到 Cordova 应用程序教程中的指示信息。

添加“推送”插件

  1. 命令行窗口中,浏览至 Cordova 项目的根目录。

  2. 运行以下命令以添加“推送”插件:

    cordova plugin add cordova-plugin-mfp-push
    
  3. 运行以下命令以构建 Cordova 项目:

    cordova build
    

iOS 平台

iOS 平台需要一个额外步骤。
在 Xcode 的功能屏幕中,为您的应用程序启用推送通知。

要点:为应用程序选择的 bundleId 必须与 Apple Developer 站点中先前创建的 AppId 相匹配。 请参阅[推送通知概述]教程。

Xcode 中功能位置的图像

Android 平台

Android 平台需要一个额外步骤。
在 Android Studio 中,将以下 activity 添加到 application 标记:

<activity android:name="com.ibm.mobilefirstplatform.clientsdk.android.push.api.MFPPushNotificationHandler"
           android:theme="@android:style/Theme.NoDisplay"/>

通知 API

客户端

Javascript 函数 描述
MFPPush.initialize(success, failure) 初始化 MFPPush 实例。
MFPPush.isPushSupported(success, failure) 设备是否支持推送通知。
MFPPush.registerDevice(options, success, failure) 向推送通知服务注册设备。
MFPPush.getTags(success, failure) 在推送通知服务实例中检索所有可用标记。
MFPPush.subscribe(tag, success, failure) 预订特定标记。
MFPPush.getSubsciptions(success, failure) 检索设备当前预订的标记。
MFPPush.unsubscribe(tag, success, failure) 取消对特定标记的预订。
MFPPush.unregisterDevice(success, failure) 从推送通知服务注销设备。

API 实现

初始化

初始化 MFPPush 实例。

  • 在客户机应用程序使用适当的应用程序上下文连接到 MFPPush 服务时为必需项。
  • 应先调用此 API 方法,然后再使用任何其他 MFPPush API。
  • 注册回调函数以处理已收到的推送通知。
MFPPush.initialize (
    function(successResponse) {
        alert("Successfully intialized");
        MFPPush.registerNotificationsCallback(notificationReceived);
    },
    function(failureResponse) {
        alert("Failed to initialize");
    }
);

是否支持推送

检查设备是否支持推送通知。

MFPPush.isPushSupported (
    function(successResponse) {
        alert("Push Supported: " + successResponse);
    },
    function(failureResponse) {
        alert("Failed to get push support status");
    }
);

注册设备

向推送通知服务注册设备。 如果不需要任何选项,可将 options 设置为 null

var options = { };
MFPPush.registerDevice(
    options,
    function(successResponse) {
        alert("Successfully registered");
    },
    function(failureResponse) {
        alert("Failed to register");
    }
);

获取标记

从推送通知服务检索所有可用标记。

MFPPush.getTags (
    function(tags) {
        alert(JSON.stringify(tags));
},
    function() {
        alert("Failed to get tags");
    }
);

预订

预订所需的标记。

var tags = ['sample-tag1','sample-tag2'];

MFPPush.subscribe(
    tags,
    function(tags) {
        alert("Subscribed successfully");
    },
    function() {
        alert("Failed to subscribe");
    }
);

获取预订

检索设备当前预订的标记。

MFPPush.getSubscriptions (
    function(subscriptions) {
        alert(JSON.stringify(subscriptions));
    },
    function() {
        alert("Failed to get subscriptions");
    }
);

取消预订

取消对标记的预订。

var tags = ['sample-tag1','sample-tag2'];

MFPPush.unsubscribe(
    tags,
    function(tags) {
        alert("Unsubscribed successfully");
    },
    function() {
        alert("Failed to unsubscribe");
    }
);

注销

从推送通知服务实例注销设备。

MFPPush.unregisterDevice(
    function(successResponse) {
        alert("Unregistered successfully");
    },
    function() {
        alert("Failed to unregister");
    }
);

处理推送通知

通过在已注册的回调函数中对响应对象执行操作,可以处理已收到的推送通知。

var notificationReceived = function(message) {
    alert(JSON.stringify(message));
};

样本应用程序图像

样本应用程序

单击以下载 Cordova 项目。

注:要运行此样本,需要在任何 Android 设备上安装最新版本的 Google Play Services。

用法样例

请查看样本的 README.md 文件以获取指示信息。

Inclusive terminology note: The Mobile First Platform team is making changes to support the IBM® initiative to replace racially biased and other discriminatory language in our code and content with more inclusive language. While IBM values the use of inclusive language, terms that are outside of IBM's direct influence are sometimes required for the sake of maintaining user understanding. As other industry leaders join IBM in embracing the use of inclusive language, IBM will continue to update the documentation to reflect those changes.
Last modified on June 01, 2020