Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

didRegisterForRemoteNotificationsWithDeviceToken is not called on ios10

I have 2 iphones: 1 - with ios 10 and 2 - with ios 9

While trying the 1st iphone:

didRegisterForRemoteNotificationsWithDeviceToken method is not called when user clicks "allow" on the alert Though, the method didRegisterUserNotificationSettings IS called. In this case device does NOT receive push notifications.

While trying the 2nd iphone:

Both methods are called here. And device DOES receive push notifications.

Then I checked on simulator ios 8

In this case the same as in 1st. Only one method is called.

I checked some answers for similar question but they didn't help me. I doubt that the issue is somewhere within push notifications settings, cuz ios 9 works OK. So the issue is somewhere within ios 10.

The very questions are:

  1. How can I call method didRegisterForRemoteNotificationsWithDeviceToken
  2. Or how can I get the device token since it's the goal

Looking forward for your help!

like image 458
Tung Fam Avatar asked Sep 19 '16 12:09

Tung Fam


2 Answers

For iOS 10 using xCode 8 GM.

This issue solved with the following steps. Requirements :- Xcode 8 GM Seed. MAC OS :- Captain EL 10.11.6

Do not remove your code for IOS 9 or below versions.

Step 1:- Go To --> Target Settings in Xcode --> Capabilities --> Enable PushNotifications.

Step 2:- Add UserNotifications framework --> Build Phase --> Link Libraries

Step 3:-

#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder   <UIApplicationDelegate,UNUserNotificationCenterDelegate>

@end

Step 4:- In the method didFinishLaunchingWithOptions register for UIUserNotificationSettings.

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

    if(SYSTEM_VERSION_EQUALTO(@"10.0")){
        UNUserNotificationCenter *notifiCenter = [UNUserNotificationCenter currentNotificationCenter];
        notifiCenter.delegate = self;
        [notifiCenter requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
             if( !error ){
                 [[UIApplication sharedApplication] registerForRemoteNotifications];
             }
         }];  
    }

    return YES;
    }

Step 5:- Implement the 2 delegate methods of UNUserNotificationCenterDelegate.

    -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{

//Do Your Code.................Enjoy!!!!
    }

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{

}
like image 73
Darshan Mothreja Avatar answered Sep 24 '22 18:09

Darshan Mothreja


You may want to check the Project Settings. Select your project. Go to Second tab -> Capabilities -> Select Push Notification to ON.

like image 36
Ram G. Avatar answered Sep 23 '22 18:09

Ram G.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!