IBM Cloud Docs
使用Event Notifications建立推播通知並將其傳送至Firefox網頁

使用Event Notifications建立推播通知並將其傳送至Firefox網頁

建立 Event Notifications 服務,新增 Firefox的推送目的地,並將訊息傳送至 Firefox Web 裝置。

何謂 Event Notifications?

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

用戶端如何使用 Firefox Web 推送通知?

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

用戶端如何使用推播通知
用戶端如何使用推播通知

目標

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

  • 使用 Event Notifications建立網站。
  • 取得 Firefox Web 認證。
  • 下載 SDK 並完成通知設定。
  • 配置 Firefox Web Push Notifications 並將其傳送至瀏覽器。

建立 Event Notifications 服務實例

  • 登入 IBM Cloud 帳戶
  • IBM Cloud 型錄 中,搜尋 Event Notifications > Event Notifications
  • 從受支援地區清單中選取 Region,然後選取 pricing plan
  • 提供 Service name
  • 選擇 resource group
  • 按一下勾選框,以接受授權合約及條款。
  • 按一下 Create

新增一般 API 來源

請執行下列步驟:

  • 移至 Event Notifications 儀表板的 Sources 區段。
  • 按一下 Add 並選取 API 來源。
  • 輸入名稱及選用說明,然後按一下 Add

建立 Event Notifications 目的地

按一下 Event Notifications 主控台中的 Destinations,並新增下列目的地詳細資料:

  • Name: 新增目的地的名稱。
  • Description: 新增目的地的選用說明。
  • Type: 從下拉清單中選取 Firefox Push Notifications 類型。
  • 選取目的地計劃: 前置正式作業目的地或正式作業目的地。
    • Pre-production destination-針對您的開發和測試環境,選取此目的地作為低成本推送目的地。
    • Production destination-利用此目的地的完整功能。 允許無限制的裝置和外寄訊息。
  • 提供您網站的 URL。
  • 按一下新增
  • 建立 Firefox 目的地之後。 編輯已建立的目的地,以取得 Web SDK 所需的 public vapId key

目的地編輯
接收通知

目的地
接收通知

建立 Event Notifications 主題

在 Event Notifications 主控台中選取 Topics,然後按一下 Create。 輸入下列主題詳細資訊:

  • Name: 輸入主題的名稱。
  • Description: 新增主題的選用說明。
  • Source:從下拉式清單中選取來源。
  • Event type: 從下拉清單中選取事件類型。
  • Event sub type: 從事件子類型下拉清單中選取事件子類型。
  • Severity: 從嚴重性下拉清單中選取嚴重性。
  • Advanced conditions: 撰寫您自己的自訂條件,必須遵循 jsonpath specifications。 Jsonpath 表達式可以在 jsonpath.comextendsclass.com.

建立 Event Notifications 訂閱

在 Event Notifications 主控台中按一下 Subscriptions。 輸入下列訂閱詳細資訊:

  • Click 建立以顯示訂閱精靈。
  • 完成下列訂閱詳細資訊:
    • Subscription name: 訂閱的名稱。
    • Subscription description: 新增選用說明。
  • Subscribe to a topic 區段下,從下拉清單中選取主題,並從目的地下拉清單中選取目的地。
  • Destination type: 在 Destination 下選取類型,然後按一下 Add

設定 Event Notifications Firefox Web SDK

Firefox Web SDK 可讓 Firefox 網站接收推送通知。 完成下列步驟,以安裝 Event Notifications Firefox Web SDK,起始設定 SDK,並登錄您網站的通知。

  • 若要將 SDK 併入專案中,請將 ENPushSDK.jsENPushServiceWorker.jsmanifest_Website.json 檔案新增至專案根資料夾。

  • 編輯 manifest_Website.json 檔案。

    {
       "name": "YOUR_WEBSITE_NAME"
    }
    
  • manifest_Website.json 檔名變更為 manifest.json

  • 在 html 檔案的 <head> 標籤中包含 manifest.json

    <link rel="manifest" href="https://github.com/IBM/event-notifications-destination-webpush-sdk/blob/main/manifest_Website.json">
    
  • 將 IBM Cloud Web 推送 SDK 併入 Script。

    <script src="https://github.com/IBM/event-notifications-destination-webpush-sdk/blob/main/ENPushSDK.js" async></script>
    
  • 請完成下列步驟,讓網站能夠起始設定 SDK。

    var enPush = new ENPush()
    
    function callback(response) {
       alert(response.response)
    }
    
    var initParams = {
       "instanceGUID": "<instance_guid>",
       "apikey": "<instance_apikey>",
       "region": "<region>",
       "deviceId": "<YOUR_DEVICE_ID>",
       "firefoxDestinationId": "<firefox_destination_id>",
       "firefoxApplicationServerKey": "<Firefox_VapId_public_key>"
    }
    
    enPush.initialize(initParams, callback)
    
    • region: Event Notifications 實例的區域。例如: us-southeu-gbau-sydeu-deeu-es

    • deviceId: 用於裝置登錄的選用 deviceId

  • 若要登錄通知,請使用 register()registerWithUserId() API 向 IBM Cloud Event Notifications 服務登錄裝置。 選擇下列一個選項:

    • 在不使用 UserId 的情況下登錄:

      enPush.register(function(response) {
         alert(response.response)
      })
      
    • UserId 登錄。 對於 userId 型通知,登錄方法將再接受一個參數- userId

      bmsPush.registerWithUserId("UserId",function(response) {
         alert(response.response)
      })
      

      UserId 是您要用來在推送服務實例中登錄裝置的使用者 ID 值。

  • subscribe API 會訂閱裝置的標籤。 裝置訂閱特定標籤後,就可以接收為該標籤傳送的通知。 將下列程式碼 Snippet 新增至 Web 應用程式,以訂閱標籤清單。

    enPush.subscribe(tagName, function(response) {
       alert(response.response)
    })
    
  • 當設定完成時,請執行您的應用程式並登錄以取得推送通知。

將通知傳送至 Firefox 裝置

使用 傳送通知 API 來傳送 Firefox 裝置的推送通知。 您可以使用 NodeGo 管理 SDK,而不直接呼叫 API。

接收通知
接收通知