iOS 10 静默推送通知不会触发后台应用程序 [英] iOS 10 silent push notification not triggering backgrounded app
问题描述
我有一个应用程序,我试图在其中接收和处理 SILENT 推送通知.
I have an app in which I'm trying to receive and handle SILENT push notifications.
我注册 APNs 如下:
I'm registering for APNs as follows:
UNUserNotificationCenter.currentNotificationCenter().delegate = self
UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert]) {(granted, error) in
if granted {
application.registerForRemoteNotifications()
}
}
实施:
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
// Handle notification
}
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
// Handle notification
}
应用有 UIBackgroundModes
fetch
和 remote-notification
APN 内容如下:
{
"aps":{
"content-available":1,
"badge":0
},
"action":"shareDelete",
"shareId":633
}
当应用在前台时,userNotificationCenter(centre:withCompletionHandler:willPresentNotification)
在收到通知时被触发,但当应用在后台时,什么也没有发生.
When the app is in the foreground, userNotificationCenter(centre:withCompletionHandler:willPresentNotification)
is fired when the notification is received, but when the app is backgrounded, nothing happens.
我可以看到通过更改徽章编号收到了 APN,但在应用程序中没有触发任何内容.
I can see the APN is received by changing the badge number, but nothing is triggered in app.
我错过了什么?
(也在 Apple 开发论坛上提问)
推荐答案
方法
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
// Handle notification
}
当用户点击通知或在 Apple Watch 上查看通知时调用:
called when user taps on notification or check it on Apple Watch:
调用以让您的应用知道用户为给定通知选择了哪个操作.
Called to let your app know which action was selected by the user for a given notification.
你需要实现旧的方法
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
}
这篇关于iOS 10 静默推送通知不会触发后台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!