使用Event Notifications创建并向 Android 手机发送推送通知
创建 Event Notifications 服务,添加 Firebase Cloud Messaging (FCM) 的推送目标,并将消息发送到 Android 设备。
什么是 Event Notifications?
Event Notifications是一项事件通知路由服务,可通知您 "IBM Cloud账户中发生的重要事件,或使用网络钩子触发自动操作。 您可以将事件通知从 IBM Cloud 服务 (例如 Availability Monitoring) 过滤和路由到电子邮件,SMS,推送通知和 Webhook。
客户如何使用 Android Push Notifications?
下图显示了客户机如何使用 Android Push Notifications。
目标
本教程向您展示了如何发送推送通知,如下所示:
- 使用Event Notifications 创建移动应用程序。
- 获取 FCM 凭证。
- 下载代码并完成通知设置。
- 配置 Android 推送通知并将其发送到移动设备。
准备工作
您必须具备以下先决条件:
- 下载并安装 Android Studio,以便您可以导入和增强代码。
- 用于登录到 Firebase 控制台以获取
project_id
,private_key
和client_email
的 Google 帐户。 - IBM Cloud 帐户。 如果您没有帐户,请 创建 IBM Cloud 帐户。
本文档中使用的说明使用了 FCM 的 HTTPv1API。 HTTPv1应用程序接口具有安全访问令牌、高效定制、面向未来以及更多可扩展客户端平台版本等优势。 有关从 FCM 传统 HTTP API 迁移到 HTTPv1API 的更多信息,请参阅 将 FCM 传统 HTTP API 迁移到 HTTPv1API。
创建Event Notifications服务实例
- 登录到 IBM Cloud 帐户。
- 在 IBM Cloud 目录 中,搜索并选择
Event Notifications > Event Notifications
。 - 从受支持区域列表中选择
Region
,然后选择pricing plan
。 - 提供 "
Service name
。 - 选择 "
resource group
。 - 通过单击复选框来接受许可协议和条款。
- 单击
Create
。
获取 FCM 凭证
Firebase 云消息传递 (FCM) 是向 Android 设备传递推送通知的网关。 要在控制台上设置 Android 推送目标,必须获取 FCM 凭证 project_id
,private_key
和 client_email
。
-
转至 Firebase 控制台。 这需要 Google 用户帐户。
-
单击
Create a project
。 如果您已具有项目,请单击Add Project
。 -
在
Create a project window
中,输入项目名称,然后通过选择切换开关并单击Continue
,接受术语并启用或禁用 Google 分析 (可选)。 -
如果启用了 Google 分析,那么在
Configure Google Analytics
窗口中,选择Analytics location
,然后接受条款。 -
单击
Create Project
。 -
新项目准备就绪后,点击 "
Continue
。 -
在导航面板中,选择 "
Project Overview
旁边的 "settings
图标,然后选择 "Settings > Project settings
。 -
单击 "
Service Accounts
选项卡。{: caption="证书FCM 全权证书FCM" caption-side="bottom"}证书
-
单击 生成新的专用密钥 以生成项目凭证。 下载的文件将包含:
project_id
,private_key
和client_email
。
生成 google-services.json
您还需要生成 "google-services.json
文件。 完成以下步骤:
-
在 Firebase 控制台的 "项目概览 "部分,点击 "
Get started by adding Firebase to your app
"部分下的 "Android
图标。Firebase 入门* Firebase -
在“
Add Firebase to your Android app
窗口中,添加”com.ibm.cloud.eventnotifications.destination.android
作为软件包名称。App nickname
字段为可选项。 -
单击注册应用程序。
到您的 Android -
包括应用程序的软件包名称。 在
Add Firebase to your Android app
窗口中输入软件包名称。App nickname
字段为可选项。 -
单击注册应用程序。 请参阅以下示例:
注册 Android -
生成
google-services.json
文件。 -
下载 "您的应用程序 "下的最新配置文件 "
google-services.json
。
添加通用 API 源
请执行以下步骤:
- 转至 Event Notifications 仪表板的
Sources
部分。 - 单击
Add
并选择 API 源。 - 输入名称和可选描述,然后单击
Add
。
创建 Event Notifications 目标
在 Event Notifications 控制台中单击 Destinations
,然后添加以下目标详细信息:
Name
: 添加目标的名称。Description
: 添加目标的可选描述。Type
: 从下拉列表中选择Android Push Notifications (FCM)
类型。- 选择目标计划: 生产前目标或生产目标。
Pre-production destination
-针对开发和测试环境,选择此目标作为低成本推送目标。Production destination
-使用此目标的完整功能。 允许无限设备和外发信息。
- 使用先前下载的文件中的
project_id
,private_key
和client_email
更新 FCM 推送凭证。 - 单击添加。
创建 Event Notifications 主题
在 Event Notifications 控制台中选择 Topics
,然后单击 Create
。 输入以下主题详细信息:
Name
: 输入主题的名称。Description
: 添加主题的可选描述。Source
:从下拉列表中选择来源。Event type
: 从下拉列表中选择事件类型。Event sub type
: 从事件子类型下拉列表中选择事件子类型。Severity
: 从严重性下拉列表中选择严重性。Advanced conditions
: 编写您自己的定制条件,必须遵循 jsonpath 规范。
创建Event Notifications订阅
在 Event Notifications 控制台中单击 Subscriptions
。 输入以下订购详细信息:
Click
创建以显示预订向导。- 填写以下订购信息:
Subscription name
: 预订的名称。Subscription description
: 添加可选描述。
- 在
Subscribe to a topic
部分下,从下拉列表中选择主题,然后从目标下拉列表中选择目标。 Destination type
: 在Destination
下选择类型,然后单击 添加。
设置 Event Notifications Android SDK
Android SDK 支持 Android 应用程序接收推送通知。 完成以下步骤以安装 Event Notifications Android SDK,初始化 SDK 并注册 Android 应用程序的通知。
-
使用 Gradle安装 Event Notifications。
compile 'com.ibm.cloud:eventnotifications-destination-android:0.0.1'
-
遵循 event-notifications-destination-android-sdk 以安装 SDK。
-
安装 Gradle 时,导入并初始化 SDK。
import com.ibm.cloud.eventnotifications.destination.android.ENPush; String instanceGUID = "<instance_guid>>"; String destinationID = "<instance_destination_id>"; String apiKey = "<instance_apikey>"; ENPush enPush = ENPush.getInstance(); enPush.setCloudRegion(ENPush.REGION_US_SOUTH); // Set your region enPush.initialize(getApplicationContext(),instanceGUID,destinationID, apiKey);
-
初始化 SDK 时,注册 以获取推送通知。
// Register the device to Event Notifications enPush.registerDeviceWithUserId("userId",new ENPushResponseListener<String>() { @Override public void onSuccess(String deviceId) { //handle successful device registration here } @Override public void onFailure(ENPushException ex) { //handle failure in device registration here } });
-
如果还可以为推送设备创建推送标签订阅,还可以选择 subscribeToPushTag。 预订 API 预订特定标记的设备。 设备订阅特定标签后,就能接收为该标签发送的通知。 将以下代码片段添加到 Android 移动应用程序以预订标记列表。
// Subscribe to the given tag, if tagname is not available it will first get create then push device will get subscribe to it. enPush.subscribe(tagName, new ENPushResponseListener<String>() { @Override public void onSuccess(String arg) { System.out.println("Succesfully Subscribed to: "+ arg); } @Override public void onFailure(ENPushException ex) { System.out.println("Error subscribing to Tag1.." + ex.getMessage()); } });
-
添加用于在应用程序中接收通知的通知侦听器。
//Handles the notification when it arrives ENPushNotificationListener notificationListener = new ENPushNotificationListener() { @Override public void onReceive (final ENSimplePushNotification message){ // Handle Push Notification } };
if(enPush != null) { enPush.listen(notificationListener); }
-
设置完成后,运行应用程序并注册推送通知。
向 Android 设备发送通知
使用 发送通知 API 来发送 Android 设备的推送通知。 您可以使用 Node 或 Go 管理 SDK,而不是直接调用 API。
{: caption="发送" caption-side="bottom"}
{: caption="接收通知接收" caption-side="bottom"}
将 FCM 传统 HTTP API 迁移到 HTTPv1API
使用 FCM 传统 HTTP API 的应用程序应考虑使用本节中的说明迁移到 HTTPv1API。 HTTPv1应用程序接口具有安全访问令牌、高效定制、面向未来以及更多可扩展客户端平台版本等优势。
在 Event Notifications中,您可以遵循 FCM 规范来创建 android 目标。 对于传统 HTTP API,我们通过两个必填参数 server_key
和 sender_id
提供支持。 For the v1 HTTP API, FCM introduced three new parameters: project_id
, client_email
,
and private_key
. 这里需要注意的是,传统 HTTP API 和v1HTTP API 是相互排斥的,我们在Event Notifications 中已经解决了这个问题。
以下是如何从旧配置迁移到新配置的方法。
-
在现有 FCM Android 目标配置下,您将找到提供的
server_key
和sender_id
。 现在,您只需通过提供project_id
,client_email
和private_key
USING 该 PATCH /destinations/{id} API 调用来更新现有目标。现有 Android 目标配置:
{ "name": "Existing android destination", "description": "Android destination with legacy parameters", "type": "push_android", "config": { "params": { "sender_id": "xxxxxx", "server_key": "xxxxxx" } } }
新的 Android 目标配置:
{ "name": "New android destination", "description": "Android destination with V1 HTTP parameters", "type": "push_android", "config": { "params": { "project_id": "xxxxx", "private_key": "xxxxx", "client_email": "abc@xyz.pqr" } } }
FCM 有效内容更改示例
-
FCM 传统 HTTP API
{ "notification": { "title": "Incidunt qui porro sequi iste assumenda esse animi.", "body": "Minus reprehenderit ut nisi. Aut earum qui est iure eos fuga." }, "data": { "name": "Willie Greenholt", "description": "Voluptas voluptatem sed quia expedita error a at sit rerum. Numquam unde debitis incidunt qui impedit et necessitatibus. Cupiditate exercitationem enim ut laborum itaque et." } }
-
FCM HTTPv1应用程序接口
{ "message": { "android": { "notification": { "title": "Incidunt qui porro sequi iste assumenda esse animi.", "body": "Minus reprehenderit ut nisi. Aut earum qui est iure eos fuga." }, "data": { "name": "Willie Greenholt", "description": "Voluptas voluptatem sed quia expedita error a at sit rerum. Numquam unde debitis incidunt qui impedit et necessitatibus. Cupiditate exercitationem enim ut laborum itaque et." } } } }