DidRegisterForRemoteNotificationsWithDeviceToken not called on ios10

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

When trying the first iphone:

didRegisterForRemoteNotificationsWithDeviceTokenthe method is not called when the user clicks "allow" on a warning Although, the method is called didRegisterUserNotificationSettings. In this case, the device does NOT receive push notifications.

When trying a second iphone:

Both methods are called here. And the device MUST receive push notifications.

Then I checked the ios 8 simulator

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

I checked several answers on a similar question, but they did not help me. I doubt the problem is somewhere in the push notification settings, cus ios 9 is working fine. So the problem is somewhere within ios 10.

Questions:

  • How can I call a method didRegisterForRemoteNotificationsWithDeviceToken
  • ,

!

+4
3

iOS 10 xCode 8 GM.

. : - Xcode 8 GM Seed. MAC OS: - EL 10.11.6

IOS 9 .

1: - → Xcode → → PushNotifications.

2: - Framework UserNotifications → →

3: -

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

@end

4: - didFinishLaunchingWithOptions 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;
    }

5: - 2 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{

}
+9

didFinishLaunchingWithOptions

func registerForNotifications(){
        if #available(iOS 10.0, *) {
            let center = UNUserNotificationCenter.current()
            center.delegate = self
            center.requestAuthorization(options:[.alert,.sound,.badge]) { (granted, error) in
                if granted{
                    UIApplication.shared.registerForRemoteNotifications()
                }else{
                    print("Notification permission denied.")
                    if !(SharedPrefs.sharedInstance!.isLoggedIn){
                        Defaults.sharedPref.removeNotificationToken()
                    }else{
                        UIApplication.shared.unregisterForRemoteNotifications()
                        print("We will delete the token at the time of logout")
                    }
                }
            }

        } else {
            // For ios 9 and below
            let type: UIUserNotificationType = [.alert,.sound,.badge];
            let setting = UIUserNotificationSettings(types: type, categories: nil);
            UIApplication.shared.registerUserNotificationSettings(setting);
            UIApplication.shared.registerForRemoteNotifications()
        }
    }
0

, . . → → Push Notification ON.

0

Source: https://habr.com/ru/post/1655063/


All Articles