0

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];
}
Community
  • 1
  • 1
Abhinav
  • 37,684
  • 43
  • 191
  • 309
  • r u registered your device token – Anbu.Karthik Nov 30 '16 at 04:28
  • Never faced this issue. Try resetting the simulator. – Inder Kumar Rathore Nov 30 '16 at 04:28
  • I received the push token, handed over to my server and it is registered at server side. They are sending push payload as well. – Abhinav Nov 30 '16 at 04:30
  • can you show ur appdelegate code – Anbu.Karthik Nov 30 '16 at 04:31
  • Please check your Device token is properly registered or correct ? How you are testing Push for Sandbox APNS ? – Wolverine Nov 30 '16 at 04:35
  • Do you register every time when app launch or only once ? – Wolverine Nov 30 '16 at 04:36
  • see this once my bro http://stackoverflow.com/questions/39382852/didreceiveremotenotification-not-called-ios-10/39383027#39383027 – Anbu.Karthik Nov 30 '16 at 04:38
  • I am doing test on device and is registering every time I launch the application. We are using sandbox push certificate on server side to send payload. I believe token is registered correctly as I get notification on delete app & re-launch. – Abhinav Nov 30 '16 at 04:39
  • Ok. In which state of your app, when you receive notifications ? – Wolverine Nov 30 '16 at 04:44
  • Its in foreground state. – Abhinav Nov 30 '16 at 04:46
  • Try checking to send notification again when your app in foreground state. And If possible then try to communicate with backend developer to verify that notification sent from by them or not. Or you should try Pusher app to test without backend. here is pusher app code. https://github.com/noodlewerk/NWPusher – Wolverine Nov 30 '16 at 04:49
  • Just use the .app file. Add device token, select your sandbox certificate, and try to send notification. – Wolverine Nov 30 '16 at 04:54
  • ok, let me try that @Wolverine. Much thanks for the direction. One observation while I was doing some research. After I implemented `application:didReceiveRemoteNotification:fetchCompletionHandler:` method, probability of push receipt has been increased. I am still missing notification though. On delete and re-install, I get it 100% of the times. – Abhinav Nov 30 '16 at 04:57
  • For iOS 10, there are 2 methods. One is **didReceiveNotificationResponse**is Handle push from background or closed. And another is **willPresentNotification** Handle push from foreground. – Wolverine Nov 30 '16 at 05:03
  • I think you are missing to implement **willPresentNotification** – Wolverine Nov 30 '16 at 05:04
  • Indeed @Wolverine. Could you please put this in answer and I shall accept it. The only callout here is if I call completion block then it present both my custom notification as well as system notification from top. – Abhinav Nov 30 '16 at 05:18

1 Answers1

1

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

Wolverine
  • 4,264
  • 1
  • 27
  • 49