I am noticing a weird push notifications issue with iOS 10. I have done suggested code changes in this SO thread to receive pushes form sandbox APNS.
After making these changes I am getting device token back from APNS and able to receive the notifications. However, if I kill & re-launch the application from Xcode again, pushes do not work. If I delete the app from the device and put it again from Xcode, pushes start coming.
Anyone have seen this behaviour and know about the possible fix would be really helpful. Please advise.
Here is the step by step implementation in my AppDelegate
:
Step 1: Import UserNotifications
#import <UserNotifications/UserNotifications.h>
Step 2: Conform to notification protocol
@interface MyAppDelegate() <UIApplicationDelegate, UNUserNotificationCenterDelegate>
Step 3: In application:didFinishLaunchingWithOptions: register for notification
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10")) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if( !error ){
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
}];
}
Step 4: Finally handle the push notification
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler {
[self handlePushNotificationWithUserInfo:response.notification.request.content.userInfo];
}