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

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

建立 Event Notifications 服務,新增 Apple Push Notification Service (Safari) 的推送目的地,並將訊息傳送至 iOS 裝置。

何謂 Event Notifications?

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

用戶端如何使用 Safari 推送通知?

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

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

目標

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

  • 使用 Event Notifications建立 Web 應用程式。
  • 取得 Safari 認證。
  • 下載代碼並完成通知設定。
  • 配置「Safari 推送通知」並將其傳送至 Safari Web。

開始之前

您必須具備以下先決條件:

  • IBM Cloud 帳戶。 如果您沒有帳戶,請 建立 IBM Cloud 帳戶
  • Event Notifications 實例。
  • IAM API 金鑰,容許 SDK 存取您的帳戶。 在 這裡建立一個。

建立一個Event Notifications服務實例

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

取得 Safari 認證

  • 將 Apple Worldwide 中繼憑證新增至金鑰鏈,您可以在 這裡找到憑證。

    證書,Apple 全球中級證書網站
    證書、識別碼和設定檔

    證書、Apple 全球中級證書新增
    證書、識別碼和設定檔

  • Apple Developer 帳戶中建立 Web Push ID。

    Web Push ID(捆綁識別碼)是標識特定應用程式的唯一識別碼。 每一個應用程式都需要一個「Web 推送 ID」。

  • 移至 Apple 開發人員入口網站,然後選取 Certificates, Identifiers & Profiles

    證書、識別碼和設定檔
    證書、識別碼和設定檔

  • 移至 Identifiers > Add identifier button

    標識符按鈕
    標識符按鈕

  • 按一下 Add a new Identifier 按鈕。

    新增的識別碼按鈕
    新增的識別碼按鈕

  • 選取 Web Push ID 選項。

    Web 推播 ID 選項
    Web 推播 ID 選項

  • 提供 Web Push ID 的 ID 及說明,ID 是 Web Push ID 的唯一反向網域字串,例如 web.com.example.domain (字串必須以 web 開頭)。 如果您的網站是 yourwebsite.com,請將 ID 新增為 web.com.yourwebsite

    網路推播 ID
    網路推播 ID

  • 繼續,然後按下一頁上的 Create certificate

    建立證書按鈕
    建立證書按鈕

    建立憑證頁面
    建立憑證頁面

  • 使用 KeyChain 建立「憑證簽署要求」,並將它儲存至磁碟。 如需相關資訊,請參閱 這裡

    憑證簽署*憑證
    建立憑證簽章

    憑證簽署*憑證
    建立憑證簽章

  • 上傳 CertificateSigningRequest.certSigningRequest 並點選繼續

    簽章憑證上傳
    上傳憑證簽章

    證書下載頁面
    上傳證書簽章

  • 下載副檔名為 .cer 的憑證,然後按兩下該憑證。 它會將它新增至 金鑰鏈存取 → 我的憑證

  • 取得您的 金鑰鏈存取 → 我的憑證,選取您的憑證匯出它。

    匯出憑證
    上傳憑證簽章

    匯出憑證新增密碼
    上傳憑證簽章

  • 新增密碼 (不要忘記它) 並儲存為 p12 憑證。

新增一般 API 來源

請執行下列步驟:

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

建立一個Event Notifications目的地

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

  • Name: 新增目的地的名稱。

  • Description: 新增目的地的選用說明。

  • Type: 從下拉清單中選取「Safari 推送通知」類型。

  • 選取目的地計劃: 前置正式作業目的地或正式作業目的地。

    • Pre-production destination-針對您的開發和測試環境,選取此目的地作為低成本推送目的地。
    • Production destination-使用此目的地的完整功能。 允許無限的設備和出站訊息。
  • 以詳細資料更新「Safari 推送認證」。

    • Website Name: 網站名稱。 這是在「通知中心」中使用的標題。
    • Website push ID:您的網站推送 ID 的唯一反向網域字串,例如web.com.example.domain (該字串必須以 web 開頭)。
    • Website URL:應允許訂閱 Safari 推播通知的網站 URL。
    • URL format string:點擊通知時要轉到的 URL。 使用 %@ 作為您在發送通知時填寫的參數的佔位符。 此 URL 必須使用 http 或 https 方案:否則無效。
    • 上傳 p12 憑證並提供憑證 password

建立 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 下選取類型,然後按一下 Add

設定 Event Notifications Safari Web SDK

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

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

  • 編輯 manifest_Website.json 檔。

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

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

    <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>",
       "safariDestinationId": "<safari_destination_id>",
       "websitePushIdSafari": "<Safari Web Push Id Identifier>"
    }
    
    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)
    })
    
  • 當設定完成時,請執行您的應用程式並登錄以取得推送通知。

將通知傳送至 Safari Web 裝置

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

接收通知
接收通知