Handling user notifications on iOS 10

2019-01-17 06:58发布

I have troubles determining when the user taps on a user push notification on iOS 10.

So far, I have been using the -[UIApplicationDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:] which is called when

  • Case 1: the application is active and the push is received
  • Case 2: when the user launched the app after taping a received notification

This method comments explicitly say

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.

All this work as expected.

Now iOS 10 deprecated this delegate method and introduced the UserNotification framework which I cannot use because I'm still targeting iOS 8 and 9.

When my app is running on iOS 10 and a push is received while the app is active (Case 1), the -[AppDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:] is called correctly.

Again on iOS 10, when the user starts the app by tapping a notification (Case 2) this method is not called.

I realise that when I implement the older -[UIApplicationDelegate application:didReceiveRemoteNotification:] it is the one that gets called in the Case 2

On iOS 8 and 9, in the Case 2 it is the -[AppDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:] method is called.

Does it mean that I have to update my application and implement the older delegate just for iOS 10?

So the question is, what is the proper implementation of handling the user interaction of a received push on iOS 10 without using the UserNotification framework.

cheers, Jan

3条回答
干净又极端
2楼-- · 2019-01-17 07:10

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.

查看更多
forever°为你锁心
3楼-- · 2019-01-17 07:13

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.
查看更多
Anthone
4楼-- · 2019-01-17 07:17

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)
}
查看更多
登录 后发表回答