IBM Cloud Docs
使用 Event Notifications 建立推送通知並將其傳送至 Android 行動式裝置

使用 Event Notifications 建立推送通知並將其傳送至 Android 行動式裝置

建立 Event Notifications 服務,新增 Firebase Cloud Messaging (FCM) 的推送目的地,並將訊息傳送至 Android 裝置。

何謂 Event Notifications?

Event Notifications 是事件通知遞送服務,可通知您在 IBM Cloud 帳戶中發生嚴重事件,或使用 Webhook 觸發自動化動作。 您可以從 IBM Cloud 服務 (例如 Availability Monitoring) 過濾並遞送事件通知至電子郵件、SMS、推送通知及 Webhook。

用戶端如何使用 Android Push Notifications?

下圖顯示用戶端如何使用 Android Push Notifications。

客戶如何使用推送通知
圖 1. 用戶端如何使用推送通知

目標

本指導教學教您如何傳送推送通知,如下所示:

  • 使用 Event Notifications建立行動式應用程式。
  • 取得 FCM 認證。
  • 下載程式碼並完成通知設定。
  • 配置 Android Push Notifications 並將其傳送至行動式裝置。

開始之前

您必須具備下列必要條件:

  • 下載並安裝 Android Studio,以便您可以匯入並加強程式碼。
  • 用來登入 Firebase 主控台以取得 project_idprivate_keyclient_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_idprivate_keyclient_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 認證
    圖 2. FCM 認證

  • 按一下 產生新的私密金鑰,以產生專案認證。 下載的檔案將包含: project_idprivate_keyclient_email

產生 google-services.json

您也需要產生 google-services.json 檔案。 請完成下列步驟:

  • 在「Firebase 主控台專案概觀」區段中,按一下 Get started by adding Firebase to your app 區段下的 Android 圖示。

    Firebase 入門
    圖 3. Firebase 入門

  • Add Firebase to your Android app 視窗中,新增 com.ibm.cloud.eventnotifications.destination.android 作為「套件名稱」。 App nickname 欄位是選用的。

  • 按一下註冊應用程式

    將 Firebase 新增至 Android 應用程式
    圖 4. 將 Firebase 新增至 Android 應用程式

  • 包括應用程式的套件名稱。 在 Add Firebase to your Android app 視窗中輸入套件名稱。 App nickname 欄位是選用的。

  • 按一下註冊應用程式。 請參閱下列範例:

    登錄 Android 應用程式
    圖 5. 登錄 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_idprivate_keyclient_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 裝置的推送通知。 您可以使用 NodeGo 管理 SDK,而不直接呼叫 API。

傳送通知
圖 6. 傳送通知

接收通知
圖 7. 接收通知

將 FCM 舊式 HTTP API 移轉至 HTTP v1 API

使用 FCM 舊式 HTTP API 的應用程式應該考慮使用本節中的指示移轉至 HTTP v1 API。 HTTP v1 API 有一些優點,例如安全存取記號、有效率的自訂作業,以及未來的證明,以及更可延伸的用戶端平台版本。

在 Event Notifications中,您可以遵循 FCM 規範來建立 android 目的地。 對於舊式 HTTP API,我們一直透過兩個必要參數 server_keysender_id 提供支援。 對於 v1 HTTP API,FCM 引進了三個新參數: project_idclient_emailprivate_key。 這裡要觀察的重要點是舊式和 v1 HTTP API 互斥,我們已在 Event Notifications中處理過。

以下說明如何從舊配置移轉至新配置。

  1. 在現有的 FCM Android 目的地配置下,您會找到所提供的 server_keysender_id。 現在,您只需要透過提供 project_idclient_emailprivate_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."
             }
          }
       }
    }