Handling user notifications on iOS 10

让人想犯罪 __ 提交于 2019-11-28 05:51:07

Swift code for 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)
}

We were facing the same problem here and we were only able to solve this problem on iOS 10 GM release by using the code on the answer given here: 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) { 

            }];
       }

With this fix our code started working again both on iOS 9 and 10.

We also had to change the way we handle application state behavior (UIApplicationStateActive, UIApplicationStateInactive and UIApplicationStateBackground) on push notifications, as it seems it also changed on iOS 10

EDIT:

  • It seems that application state behavior is back to normal on latest iOS 10 versions.

This has been fixed in iOS 10.1 Beta 1 !!

The -[UIApplicationDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:] is correctly called when the user taps on a notification.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!