处理 iOS 10 上的用户通知

Handling user notifications on iOS 10

我无法确定用户何时在 iOS 10 上点击用户推送通知。

到目前为止,我一直在使用 -[UIApplicationDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:],它在

时被调用

这个方法注释明确说

Note that this behavior is in contrast to application:didReceiveRemoteNotification:, which is not called in those cases, and which will not be invoked if this method is implemented.

所有这些都按预期工作。

现在 iOS 10 弃用了此委托方法并引入了 UserNotification 框架,我无法使用该框架,因为我仍然以 iOS 8 和 9 为目标。

当我的应用程序在 iOS 10 上 运行 并且在应用程序处于活动状态 (Case 1) 时收到推送时,-[AppDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:] 被正确调用。

同样在 iOS 10 上,当用户通过点击通知 (Case 2) 启动应用程序时,此方法 不会被调用

我意识到,当我实施较旧的 -[UIApplicationDelegate application:didReceiveRemoteNotification:] 时,它会在 Case 2

中被调用

在iOS8和9,在Case 2是调用了-[AppDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:]方法。

这是否意味着我必须更新我的应用程序并为 iOS 10 实现旧的委托?

所以问题是,在不使用 UserNotification 框架的情况下,在 iOS 10 上处理接收到的推送的用户交互的正确实现是什么。

干杯, 一月

我们在这里遇到了同样的问题,我们只能使用此处给出的答案中的代码在 iOS 10 GM 版本上解决这个问题:https://forums.developer.apple.com/thread/54332

     - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {  

           NSOperatingSystemVersion version = [[NSProcessInfo processInfo] operatingSystemVersion];
           if (version.majorVersion == 10 && version.minorVersion == 0) {
              [self application: application 
   didReceiveRemoteNotification: userInfo    
         fetchCompletionHandler: ^(UIBackgroundFetchResult result) { 

            }];
       }

通过此修复,我们的代码在 iOS 9 和 10 上再次开始工作。

我们还必须更改 application state behavior (UIApplicationStateActive, UIApplicationStateInactive and UIApplicationStateBackground) 推送通知的处理方式,因为它似乎也在 iOS 10

上发生了变化

编辑:

  • 应用程序状态行为似乎在最新的 iOS 10 个版本中恢复正常。

Swift iOS 10 的代码:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    if #available(iOS 10.0, *) {
        let center = UNUserNotificationCenter.currentNotificationCenter()
        center.delegate = self
    }

    // ...

    return true
}

@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {

    print(response.notification.request.content.userInfo)        
}

@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {

    print(notification.request.content.userInfo)
}

这已在 iOS 10.1 Beta 1 中修复!!

当用户点击通知时 -[UIApplicationDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:] 被正确调用。