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.
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: