苹果推送通知 – iOS 10静音推送通知不触发背景应用程序

前端之家收集整理的这篇文章主要介绍了苹果推送通知 – iOS 10静音推送通知不触发背景应用程序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个应用程序,我正在尝试接收和处理SILENT推送通知.

我正在注册APNs如下:

  1. UNUserNotificationCenter.currentNotificationCenter().delegate = self
  2. UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge,.Sound,.Alert]) {(granted,error) in
  3. if granted {
  4. application.registerForRemoteNotifications()
  5. }
  6. }

实现:

  1. func userNotificationCenter(center: UNUserNotificationCenter,didReceiveNotificationResponse response: UNNotificationResponse,withCompletionHandler completionHandler: () -> Void) {
  2. // Handle notification
  3. }
  4.  
  5. func userNotificationCenter(center: UNUserNotificationCenter,willPresentNotification notification: UNNotification,withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
  6. // Handle notification
  7. }

该应用程序具有UIBackgroundModes提取和远程通知

APN内容如下所示:

  1. {
  2. "aps":{
  3. "content-available":1,"badge":0
  4. },"action":"shareDelete","shareId":633
  5. }

当应用程序处于前台时,当收到通知时,userNotificationCenter(center:withCompletionHandler:willPresentNotification)将被触发,但是当应用程序背景时,没有任何反应.

我可以看到通过更改徽章号码收到APN,但在应用程序中没有触发任何内容.

我失踪了什么

(也在Apple开发论坛上询问)

解决方法

方法
  1. func userNotificationCenter(center: UNUserNotificationCenter,withCompletionHandler completionHandler: () -> Void) {
  2. // Handle notification
  3. }

用户点击通知或在Apple Watch上查看时呼叫:

Called to let your app know which action was selected by the user for a given notification.

你需要实现旧的方法

  1. func application(_ application: UIApplication,didReceiveRemoteNotification userInfo: [AnyHashable : Any],fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  2.  
  3. }

猜你在找的iOS相关文章