简体   繁体   中英

iOS 10 Push Notifications - Sandbox APNS Issue

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];
}

To handle notifications in iOS 10 .

There are two methods.

willPresentNotification

- (void)userNotificationCenter:(UNUserNotificationCenter *)center  
  willPresentNotification:(UNNotification *)notification  
  withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler  
{  
   NSLog( @"This method is called to Handle push from foreground" );  
  // custom code to handle push while app is in the foreground  
    NSLog(@"User info ==> %@", notification.request.content.userInfo);
}  

And didReceiveNotificationResponse

- (void)userNotificationCenter:(UNUserNotificationCenter *)center  
  didReceiveNotificationResponse:(UNNotificationResponse *)response  
  withCompletionHandler:(void (^)())completionHandler  
{  
     NSLog( @"Handle push from background or closed" );  
     NSLog(@"%@", response.notification.request.content.userInfo);
}  

Hope it helps..!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM