didRegisterForRemoteNotificationsWithDeviceToken is not called on ios10

10,692

Solution 1

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{

}

Solution 2

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

Share:
10,692
Tung Fam
Author by

Tung Fam

iOS Engineer at Revolut

Updated on June 25, 2022

Comments

  • Tung Fam
    Tung Fam almost 2 years

    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!