iOS 10 new way to detect applicationState on push notifications?

In testing our apps for iOS 10, we noticed that push notifications are coming to the device and tapping the notifications opens the apps. However, the apps are just opening and not doing anything. It looks like [UIApplication sharedApplication].applicationState doesn't work as it did previously.


This is hit on app startup:

[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];


I can trace the code and see that this is firing and succeeding:

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
     NSLog( @"Device token: %@", deviceToken );
     // other setup code that connectes to an in-house server, and I watch the successful connection and information exchange

}

When a push notification is delivered to the device, the notification appears, but when the user taps the notification, the app opens, and this fires:

-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void
  (^)(UIBackgroundFetchResult))completionHandler
  {
       NSLog( @"HANDLE PUSH, didReceiveRemoteNotification: %@", userInfo );

       if( [UIApplication sharedApplication].applicationState == UIApplicationStateInactive )
       {
          NSLog( @"INACTIVE" );
          // code to handle push from inactive state
       }
       else if( [UIApplication sharedApplication].applicationState == UIApplicationStateBackground )
       {
          NSLog( @"BACKGROUND" );
          // code to handle push from background state
       }
       else
       {
          NSLog( @"ELSE" );
          // code to handle push from active state
       }
}


The HANDLE PUSH... line is making it to the console, and then ELSE. This happens regardless of the state of the app or device.


Did something change in the way we are supposed to detect application state in response to a user tapping a push notification? [UIApplication sharedApplication].applicationState doesn't seem to work any more.


These apps all still work as expected using the code above in iOS8 and 9.

Accepted Reply

Thanks for the input lapieuvre and ryan,


Both of you had a piece of what I was missing:

This is needed:

#import <UserNotifications/UserNotifications.h>


Then follow documentation from here:

https://developer.apple.com/reference/usernotifications/unusernotificationcenter


I'm not actually to where device state is being detected because I am having issues getting the token from a successful registration now that didRegisterForRemoteNotificationsWithDeviceToken is no longer called, but I'll start another thread on that.

EDIT: What I've got working is posted here:

https://forums.developer.apple.com/message/162269

Replies

An addendum:

  1. When the app is totally closed and the user taps a notification, the call to registerUserNotificationSettings and registerForRemoteNotifications is happening prior to the point at which didRegisterForRemoteNotificationsWithDeviceToken is firing. Those first two are called in the AppDelegate willFinishLunchingWithOptions method, so I guess my questions would be:
    1. Why or how is something firing prior to willFinishLaunchingWithOptions?
    2. How is the delegate method didRegisterForRemoteNotificationsWithDeviceToken even firing BEFORE registerForRemoteNotifications is called?
  2. When the app is totally closed and the user taps a notification, didReceiveRemoteNotification never fires
  3. When the app is in the background, didReceiveRemoteNotification fires, but [UIApplication sharedApplication].applicationState is not UIApplicationStateBackground as it was prior to iOS 10.


Did the bootstrap order change in iOS 10, or am I missing something?

I second this issue. It's happening to us as well.

I've found it. The new way is here: https://developer.apple.com/reference/usernotifications/unusernotificationcenter

Ryan, I looked at that, but I cannot get Xcode to recognize UNUserNotificationCenter. Error:

Unknown type name 'UNUserNotificationCenter'; did you mean 'NSNotificationCenter'?


I get this even when I have NotificationCenter, UserNotifications and UserNotificationsUI linked and have set the Deployment Target and Base SDK to 10.0.


Maybe there's another framework that needs to be linked? Or, perhaps, there's an import statement needed?

Hi,

You need to import the framwork or the header :

#import <UserNotifications/UserNotifications.h>

Thanks for the input lapieuvre and ryan,


Both of you had a piece of what I was missing:

This is needed:

#import <UserNotifications/UserNotifications.h>


Then follow documentation from here:

https://developer.apple.com/reference/usernotifications/unusernotificationcenter


I'm not actually to where device state is being detected because I am having issues getting the token from a successful registration now that didRegisterForRemoteNotificationsWithDeviceToken is no longer called, but I'll start another thread on that.

EDIT: What I've got working is posted here:

https://forums.developer.apple.com/message/162269

What about iOS 9 apps that run on devices upgraded to iOS 10? These apps would suffer from the same applicationState notification issue and would not handle push notiifcation taps properly. Is notification tap handling going to be broken until the user upgrades the app to a version that uses the new UNUserNotificationCenter?

This is the issue we're facing. On beta 7, our shipping app opens when the user taps the notification, but application:didReceiveRemoteNotification:fetchCompletionHandler: does not get called at all. Has anyone else tried this in beta 7?