使用Event Notifications建立推播通知並將其傳送至iOS行動設備
建立 Event Notifications 服務,新增 Apple Push Notification 服務 (APNs) 的推送目的地,並將訊息傳送至 iOS 裝置。
何謂 Event Notifications?
Event Notifications是一項事件通知路由服務,可通知您IBM Cloud帳戶中發生的關鍵事件,或使用網路鉤子觸發自動操作。 您可以從 IBM Cloud 服務 (例如 Availability Monitoring) 過濾並遞送事件通知至電子郵件、SMS、推送通知 (FCM 或 APNs) 及 Webhook。
用戶端如何使用 iOS Push Notifications?
下圖顯示用戶端如何使用 iOS Push Notifications。
目標
本指導教學教您如何傳送推送通知,如下所示:
- 使用Event Notifications建立行動應用程式。
- 取得 APNs 認證。
- 下載代碼並完成通知設定。
- 配置 iOS Push Notifications 並將其傳送至行動式裝置。
開始之前
您必須具備以下先決條件:
- IBM Cloud 帳戶。 如果您沒有帳戶,請 建立 IBM Cloud 帳戶。
- Event Notifications 實例。
- IAM API 金鑰,容許 SDK 存取您的帳戶。 在 這裡建立一個。
- Xcode 9.3+
- Swift 4.2+
- iOS 10.0+
建立一個Event Notifications服務實例
- 登入 IBM Cloud 帳戶。
- 在 IBM Cloud 型錄 中,搜尋
Event Notifications > Event Notifications
。 - 從受支援地區清單中選取
Region
,然後選取pricing plan
。 - 提供
Service name
。 - 選擇
resource group
。 - 按一下勾選框,以接受授權合約及條款。
- 按一下
Create
。
取得 APNs 認證
對於 iOS 裝置和應用程式,Apple Push Notification Service (APNs) 容許應用程式開發人員從 Event Notifications 上的 IBM Cloud 服務實例(提供者)將遠端通知傳送到 iOS 裝置和應用程式。 訊息會傳送到裝置上的目標應用程式。
您需要取得並配置 APNs 認證。 APNs 憑證是透過 Event Notifications 服務安全地進行管理,並且用來以提供者身分連接至 APNs 伺服器。
登錄 App ID
App ID(組合 ID)是用於識別特定應用程式的唯一 ID。 每個應用程式都需要 App ID。 諸如推播通知服務之類的服務被配置到App ID。
-
移至 Apple 開發者入口網站,然後選取 憑證、ID 及設定檔。
證書、識別碼和設定檔 -
移至
Identifiers > Add identifier button
。標識符按鈕 -
按一下「新增
Identifier
」按鈕。新增的識別碼按鈕 -
選取 App ID 選項。
應用程式 ID 選項 -
選擇類型並點擊
Continue
。App ID類型 -
為 App ID Prefix 提供字串。 在
Description
欄位中提供應用程式名稱。 例如,Event Notifications iOS 應用程式。 -
對於
Bundle ID
,選擇 明確 並提供「組合 ID」值。 建議提供反向網域名稱樣式字串。 例如,com.ibm.cloud.en.app
。建立新的App IDID詳細資料 -
從「功能」中選取
Push Notifications
勾選框,然後按一下Continue
。啟用推播通知 -
瀏覽您的設定並點擊
Register > Done
。
現在,您的 App ID 已登錄。

建立開發和配送 APNs SSL 憑證
在取得 APNs 憑證之前,必須先產生一個憑證簽署要求 (CSR),然後將其提交給 Apple(憑證管理中心 (CA))。 CSR 所含的資訊可識別您的公司,以及用來簽署 Apple Push Notifications 的公開和私密金鑰。 然後,在 iOS 開發人員入口網站上產生 SSL 憑證。 該憑證與其公開金鑰和私密金鑰一起儲存在「金鑰鏈存取」中。
您可以在兩種模式下使用 APNs:
- 沙盤推演模式,用於開發和測試。
- 正式作業模式,用於透過 App Store(或其他企業配送機制)配送應用程式。
必須分別針對開發環境和配送環境取得不同的憑證。 憑證與接收遠端通知的應用程式的 App ID 相關聯。 對於正式作業,您最多可以建立兩個憑證。IBM Cloud 使用憑證來建立與 APN 的 SSL 連線。
建立 APNs p12 憑證
-
移至 Apple 開發人員入口網站,然後選取「憑證、ID 及設定檔」。
證書、識別碼和設定檔 -
在
Identifiers area
中,選取 App ID。標識符選項卡 -
選擇
Push Notifications
複選框或選擇Edit
選項。推播通知複選框 - 在開發 SSL 憑證窗格中,按一下建立憑證...
- 在生產 SSL 憑證窗格中,按一下建立憑證...
證書類型 -
使用 Mac 上的 Keychain Access 應用程式建立憑證簽署請求 (CSR)。
-
從選單中選擇
Keychain Access > Certificate Assistant > Request a Certificate From a Certificate Authority…
。請求新證書類型 -
在憑證資訊中,輸入與您的應用程式開發人員帳戶關聯的電子郵件地址和常用名稱。 請提供有意義的名稱,協助您識別它是用於開發(沙盤推演)還是配送(正式作業)的憑證;例如,sandbox-apns-certificate 或 production-apns-certificate。
-
選擇
Saved to disk
將.certSigningRequest
檔案下載到您的 Mac,然後點選Continue
。證書詳細資料 -
在
Save As
功能表選項中,將.certSigningRequest
檔案命名,選取必須儲存檔案的位置,然後按一下Save
。儲存 CSR 檔案 -
按一下
Done
。 您現在有 CSR。創造企業社會責任 -
返回 Apple 開發人員門戶,對於上傳 CSR 文件選項,按一下選擇文件,然後選擇檔案
CertificateSigningRequest.certSigningRequest
。 按一下Continue
。上傳 CSR 檔案 -
按一下
Download
。 便會下載aps_development.cer
檔案。下載憑證 -
尋找新安裝的憑證。 按兩下憑證,以將它安裝至「金鑰鏈存取」。
-
在您的 Mac 上,移至
Keychain Access > My Certificates
。 -
選擇憑證和私鑰,然後選擇
Export
將憑證轉換為個人資訊交換格式(.p12
格式)。出口證明 -
在
Save As
欄位中,為憑證提供一個有意義的名稱。 例如,sandbox-apns.p12 或 production-apns.p12,然後按一下Save
。儲存憑證 -
在
Enter a password
欄位中,輸入密碼以保護匯出的項目,然後按OK
。 您可以使用此密碼,在 Event Notifications 服務主控台上配置 APNs 設定。保護憑證 -
密鑰存取應用程式會提示您從鑰匙圈畫面匯出密鑰。 輸入 Mac 的管理密碼以允許系統匯出這些項目,然後選擇「始終允許」選項。 將在您選擇的位置產生.p12憑證。
建立 APNs p8 記號
-
移至 Apple 開發人員入口網站,然後選取
Certificates, Identifiers & Profiles
。證書、識別碼和設定檔 -
移至
Keys
標籤。鍵選項卡 -
輸入
Key Name
並啟用 APN,然後按一下continue
。P8 代幣詳細資料 -
按一下
Register
。註冊p8令牌 -
請務必在註冊後在新的重定向頁面中下載金鑰,因為這是一次性活動。
KeyID
在檔案中可見並顯示在使用者介面中,此 KeyID
用於登錄 APNS 憑證。TeamID
由開發人員帳戶的 Apple 提供,可以在「團隊名稱」旁邊看到。TeamID
用於登錄 APNS 憑證。BundleID
可以登錄為 P12 憑證。
新增一般 API 來源
請執行下列步驟:
- 移至 Event Notifications 儀表板的
Sources
區段。 - 按一下
Add
並選取 API 來源。 - 輸入名稱及選用說明,然後按一下
Add
。
建立一個Event Notifications目的地
按一下 Event Notifications 主控台中的 Destinations
,並新增下列目的地詳細資料:
Name
: 新增目的地的名稱。Description
: 新增目的地的選用說明。Type
: 從下拉清單中選取iOS Push Notifications
類型。- 選取目的地計劃: 前置正式作業目的地或正式作業目的地。
Pre-production destination
-針對您的開發和測試環境,選取此目的地作為低成本推送目的地。Production destination
-使用此目的地的完整功能。 允許無限的設備和出站訊息。
- 使用以
Authentication Type
為基礎的詳細資料來更新 iOS 推送認證,然後選取Environment
。- 如果憑證類型為
p8
,請上傳 .p8 檔案,並提供KeyID
、TeamID
及BundleID
。 - 如果憑證類型為
p12
,請上傳 p12 憑證並提供憑證password
。
- 如果憑證類型為
- 按一下
Add
。
建立 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 IOS SDK
iOS SDK 可讓 iOS 應用程式接收推送通知。 請完成下列步驟,以安裝 Event Notifications iOS SDK,起始設定 SDK,並登錄 iOS app的通知。
安裝
此 SDK 的現行版本為: 0.0.1
若要使用 Event Notifications iOS 目的地 SDK,請定義包含服務之構件座標 (群組 ID、構件 ID 及版本) 的相依關係,如下所示:
CocoaPods
use_frameworks!
target 'MyApp' do
pod 'ENPushDestination', '~> 0.0.1'
end
Carthage
若要使用 Carthage 來安裝 ENPushDestination
,請將下列內容新增至 Cartfile。
github "IBM/event-notifications-destination-ios-sdk" ~> 0.0.1
然後,執行下列指令,以建置相依關係及架構:
carthage update --platform iOS
Swift 套件管理程式
將下列內容新增至 Package.swift
檔案,以將 ENPushDestination 識別為相依關係。 當您使用 Swift 建置來建置專案時,套件管理程式會複製 ENPushDestination。
dependencies: [
.package(url: "https://github.com/IBM/event-notifications-destination-ios-sdk", from: "0.0.1")
]
安裝-起始設定 SDK
請完成下列步驟,讓 iOS 應用程式能夠接收通知。
-
在
.swift
檔案中新增import
陳述式。import ENPushDestination
-
初始化ENPushDestination SDK
let instanceGUID = "<instance_guid>>"; let destinationID = "<instance_destination_id>"; let apiKey = "<instance_apikey>"; let enPush = ENPush.sharedInstance enPush.setCloudRegion(region: .usSouth) enPush.initialize(instanceGUID, destinationID, apiKey)
region
: Event Notifications 實例的區域。 例如,Region.usSouth
。
登錄以取得通知
使用 ENPush.registerDevice()
API 在 Event Notifications 服務中向 iOS 目的地登錄裝置。
支援下列選項:
-
無需userId:
/**Register iOS devices*/ enPush.registerWithDeviceToken(deviceToken: "<apns-device-token>") { response, statusCode, error in print(response?.id ?? "") }
-
以 UserId登錄。 對於
userId
型通知,登錄方法會再接受一個參數-userId
。/**Register iOS devices*/ enPush.registerWithDeviceToken(deviceToken: "<apns-device-token>", withUserId: "userId") { response, statusCode, error in print(response?.id ?? "") }
userId
用來傳遞登錄 Event Notifications的唯一userId
值。
從通知取消登錄
使用下列程式碼 Snippet,從 Event Notifications取消登錄。
enPush.unregisterDevice { response, statusCode, error in
/**.....*/
}
若要從 UserId
型登錄取消登錄,您必須呼叫登錄方法。 請參閱 登錄通知 中的 Register without userId option
。
Event Notifications 目的地標籤訂閱
在 iOS 裝置上接收推送通知
若要在iOS裝置上接收推播通知,請將以下Swift方法新增至應用程式的 appDelegate.swift
:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
//UserInfo dictionary will contain data sent from the server
}
通知選項
支援以下通知選項。
互動式通知
- 若要啟用互動式推送通知,必須在通知物件中傳入通知動作參數。 下列是啟用互動式通知的範例程式碼:
let actionOne = ENPushNotificationAction(identifierName: "FIRST", buttonTitle: "Accept", isAuthenticationRequired: false, defineActivationMode: .foreground)
let actionTwo = ENPushNotificationAction(identifierName: "SECOND", buttonTitle: "Reject", isAuthenticationRequired: false, defineActivationMode: .destructive)
let category = ENPushNotificationActionCategory(identifierName: "category", buttonActions: [actionOne, actionTwo])
let notificationOptions = ENPushClientOptions()
notificationOptions.setInteractiveNotificationCategories(categoryName: [category])
enPush.initialize(instanceGUID, destinationID, apiKey, notificationOptions)
-
在 AppDelegate.swift 上實作回呼方法:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { switch response.actionIdentifier { case "Accept": print("Clicked Accept") case "Reject": print("Clicked Reject") default: } completionHandler() }
當使用者按一下操作按鈕時,將呼叫此回調方法。 此方法的實作必須執行與指定識別碼關聯的任務並執行completionHandler參數中的區塊。
新增自訂 DeviceId 以進行登錄
若要傳送 DeviceId
,請使用 ENPushClientOptions
類別的 setDeviceId
方法。
let options = ENPushClientOptions();
options.setDeviceId(deviceId: "YOUR_DEVICE_ID");
請記得為每一個裝置保留自訂 DeviceId unique
。
啟用複合式多媒體通知
iOS 10 或更新版本支援複合式多媒體通知。 若要接收富媒體通知,請實作UNNotificationServiceExtension。 延伸將截取並處理複合式多媒體通知。
在服務擴充的didReceive( ) 方法中,新增下列程式碼以擷取豐富的推播通知內容。
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
ENPushRichPushNotificationOptions.didReceive(request, withContentHandler: contentHandler)
}
將通知傳送至 iOS 裝置
使用 傳送通知 API 來傳送 iOS 裝置的推送通知。 您可以使用 Node 或 Go 管理 SDK,而不直接呼叫 API。

