IBM Cloud Event Notifications 服务 iOS 目标 SDK 版本 0.0.5
与各种 IBM Cloud Event Notifications 服务 进行交互的 iOS 目标客户端库。
免责声明:该 SDK 初始版本作为 预发布版 发布。可能发生更改,这些更改可能会影响使用此 SDK 的应用程序。
目录
概述
IBM Cloud Event Notifications 服务 iOS 目标 SDK 允许开发者在 IBM 云中注册 Event Notifications 服务的 APNS 目标。
服务名称 | 工件坐标 |
---|---|
事件通知服务 | ENPushDestination:0.0.5 |
先决条件
安装
此 SDK 的当前版本是:0.0.5
要使用 Event Notifications iOS 目标 SDK,定义一个包含服务工件坐标(组 ID、工件 ID 和版本)的依赖项,如下所示
Cocoapods
use_frameworks!
target 'MyApp' do
pod 'ENPushDestination', '~> 0.0.5'
end
Carthage
要使用 Carthage 安装 ENPushDestination
,将以下内容添加到您的 Cartfile 中。
github "IBM/event-notifications-destination-ios-sdk" ~> 0.0.5
然后运行以下命令构建依赖项和框架
$ carthage update --platform iOS
Swift Package Manager
将以下内容添加到您的 Package.swift
文件中,以将 ENPushDestination 识别为依赖项。当您使用 swift build 构建项目时,包管理器将克隆 ENPushDestination。
dependencies: [
.package(url: "https://github.com/IBM/event-notifications-destination-ios-sdk", from: "0.0.5")
]
使用 SDK
要消费的 SDK 方法
安装
初始化 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 : 事件通知实例的地区。例如;
Region.usSouth
、Region.london
、Region.sydney
、Region.frankfurt
和Region.madrid
- region : 事件通知实例的地区。例如;
注册通知
使用 ENPush.registerDevice()
API 将设备与事件通知服务中的 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用于传递注册事件通知时唯一的userId值。
从通知中注销
使用以下代码片段注销从事件通知中。
enPush.unregisterDevice { response, statusCode, error in
/**.....*/
}
注意:要从基于UserId的注册中注销,必须调用注册方法。请参阅注册通知中的
不使用userId的注册选项
。
事件通知目标标签订阅
订阅标签
subscribe
API将设备订阅给特定的标签。设备被订阅到某个特定标签后,该设备可以接收对该标签发送的通知。
将以下代码片段添加到您的iOS移动应用程序中,以订阅一系列标签。
// Subscribe to the given tag
enPush.subscribeToTags(tagName: "<tag_name>") { response, statusCode, error in
/**.....*/
});
检索已订阅的标签
retrieveSubscriptionsWithCompletionHandler
API将返回设备订阅的标签列表。在移动应用程序中使用以下代码片段来获取订阅列表。
// Get a list of tags that to which the device is subscribed.
enPush.retrieveSubscriptionsWithCompletionHandler { response, statusCode, error in
/**.....*/
}
从标签中取消订阅
unsubscribeFromTags
API将从标签列表中移除设备的订阅。使用以下代码片段允许您的设备从标签中注销。
// unsubscibe from the given tag ,that to which the device is subscribed.
enPush.unsubscribeFromTags(tagName: "<tag_name>") { response, statusCode, error in
/**.....*/
}
在 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()
}
此回调方法在用户点击操作按钮时被调用。此方法的实现必须执行与指定标识符相关的任务,并执行完成处理程序参数中的块。
添加自定义 DeviceId 进行注册
要使用DeviceId
,请使用ENPushClientOptions
类的setDeviceId
方法。
let options = ENPushClientOptions();
options.setDeviceId(deviceId: "YOUR_DEVICE_ID");
注意:请记住使每个设备的自定义DeviceId
唯一
。
启用丰富媒体通知
在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)
}
发送通知的投递状态
您可以通过实现sendDeliveryStatus函数
来将通知状态(查看/打开)发送回事件通知服务。
根据通过拦截通知接收通知的时间,您可以发送回查看状态,当其被打开时,您可以发送回打开状态。
要使用此函数,以下是一个示例
对于打开状态-
ENPush.sharedInstance.sendDeliveryStatus(notificationId:"en_nid",status: ENDeliveryStatus.open){ response, statusCode, error in
guard error.isEmpty else {
return
}
print(response ?? "")
}
对于查看状态-
ENPush.sharedInstance.sendDeliveryStatus(notificationId:"en_nid",status: ENDeliveryStatus.seen){ response, statusCode, error in
guard error.isEmpty else {
return
}
print(response ?? "")
}
问题
如果您在使用此SDK或有关IBM Cloud服务的问题上遇到困难,请请在Stack Overflow上提问。
问题
如果您在项目中遇到问题,欢迎使用问题报告。在提交之前,请搜索类似的问题。可能有人已经报告了该问题。
开源 @ IBM
在IBM GitHub页面上找到更多开源项目
贡献
查看CONTRIBUTING。
许可证
IBM Cloud Event Notifications Service iOS目标SDK在Apache 2.0许可证下发布。许可证的全文可以在LICENSE中找到。