使用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 Push Notifications 並將其傳送至行動式裝置。
開始之前
您必須具備以下先決條件:
- 下載並安裝 Android Studio,以便您可以匯入並加強程式碼。
- 用來登入 Firebase 主控台以取得
project_id
、private_key
及client_email
的 Google 帳戶。 - IBM Cloud 帳戶。 如果您沒有帳戶,請 建立 IBM Cloud 帳戶。
本文檔中使用的說明使用 FCM 的 HTTP v1 API。 HTTP v1 API 具有安全存取權杖、高效自訂、面向未來以及更具可擴展性的客戶端平台版本等優點。 有關從 FCM 舊版 HTTP API 遷移到 HTTP v1 API 的更多信息,請參閱 將 FCM 舊版 HTTP API 遷移到 HTTP v1 API。
建立一個Event Notifications服務實例
- 登入 IBM Cloud 帳戶。
- 在 IBM Cloud 型錄 中,搜尋並選取
Event Notifications > Event Notifications
。 - 從受支援地區清單中選取
Region
,然後選取pricing plan
。 - 提供
Service name
。 - 選擇
resource group
。 - 按一下勾選框,以接受授權合約及條款。
- 按一下
Create
。
取得 FCM 認證
Firebase Cloud Messaging (FCM) 是將推送通知遞送至 Android 裝置的閘道。 若要在主控台上設定 Android Push 目的地,您必須取得 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
標籤。FCM 憑證 -
按一下 產生新的私密金鑰,以產生專案認證。 下載的檔案將包含:
project_id
、private_key
和client_email
。
產生 google-services.json
您還需要產生 google-services.json
文件。 請完成下列步驟:
-
在 Firebase 控制台專案概述部分的
Get started by adding Firebase to your app
部分下,按一下Android
圖示。Firebase 入門 -
在
Add Firebase to your Android app
視窗中,新增com.ibm.cloud.eventnotifications.destination.android
作為套件名稱。App nickname
欄位是可選的。 -
按一下註冊應用程式。
將 Firebase 新增至您的 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 specifications。
建立 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 會訂閱特定標籤的裝置。 設備訂閱特定標籤後,設備可以接收針對該標籤發送的通知。 將下列程式碼 Snippet 新增至 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。


將 FCM 舊版 HTTP API 移轉到 HTTP v1 API
使用 FCM 舊版 HTTP API 的應用程式應考慮使用本節中的說明遷移到 HTTP v1 API。 HTTP v1 API 具有安全存取權杖、高效自訂、面向未來以及更具可擴展性的客戶端平台版本等優點。
在 Event Notifications中,您可以遵循 FCM 規範來建立 android 目的地。 對於舊版 HTTP API,我們一直透過兩個必要參數 server_key
和 sender_id
提供支援。 對於v1 HTTP API,FCM 引入了三個新參數:project_id
、client_email
和 private_key
。 這裡要注意的重要一點是舊版
HTTP API 和v1 HTTP API 是互斥的,我們已經在Event Notifications中解決了這個問題。
以下說明如何從舊配置移轉至新配置。
-
在現有的 FCM Android 目的地配置下,您會找到所提供的
server_key
和sender_id
。 現在,您只需要透過提供project_id
、client_email
及private_key
USING the 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 HTTP v1 API
{ "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." } } } }