我们可以在推送通知被传递或关闭时从客户端进行服务调用吗?
此外,APNS 是否提供有关通知是否在个人级别传递的任何信息,以及我们是否可以使用任何 API 来查询传递状态报告?
如果将content-available
标志发送到aps
字典中的1
,则可以检测远程通知的传递。如果设置了密钥,则会调用应用程序委托application:didReceiveRemoteNotification:fetchCompletionHandler:
方法。
此外,从iOS 10开始,您还可以检测远程通知的关闭。为此,您需要实现用户通知框架。
您需要执行以下步骤:
使用 iOS 10 API 注册远程通知的类别。您需要检查相同的方法
[[UNUserNotificaionCenter currentNotificationCenter] setNotificationCategories]
。见 https://developer.apple.com/documentation/usernotifications/unusernotificationcenteriOS 10 引入了一个名为
UNNotificationCategory
的新类,用于封装类别实例。您需要在类别实例上设置CustomDismissAction
,以便能够接收消除回调。
见 https://developer.apple.com/documentation/usernotifications/unnotificationcategory
- 通过实现
userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:
方法实现UNUserNotificationCenterDelegate
协议。此方法将接收对消除操作的回调,前提是您执行上述步骤。
请参阅 https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate
将代理设置为用户通知中心 - [[UNUserNotificaionCenter currentNotificationCenter].delegate = your_delegate_implementation
- 在有效负载中使用
CustomDismissAction
属性设置类别。
{
"aps": {
"alert": "joetheman",
"sound": "default",
"category": "my-custom-dismiss-action-category",
"content-available":1
},
"message": "Some custom message for your app",
"id": 1234
}
是的,当应用程序未处于活动状态时收到通知时,您可以从iOS应用程序拨打服务电话。在以下位置执行此操作:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
<#code#>
}
是的,您可以在推送通知有效负载中添加其他键值对。
{
"aps": {
"alert": "joetheman",
"sound": "default"
},
"message": "Some custom message for your app",
"id": 1234
}