didRegisterForRemoteNotificationsWithDeviceToken never called on specific device [duplicate]

北慕城南 提交于 2019-12-02 20:40:47

I got a similar problem, the code was already implemented and working fine. Suddenly, after some adjustments, it just don't work it anymore.

The scenario running on device was:

  • Call registerForRemoteNotifications on didFinishLaunchingWithOptions.
  • The didRegisterForRemoteNotificationsWithDeviceToken won't call
  • nor didFailToRegisterForRemoteNotificationsWithError won't call.

I tried everything, almost reset all push configurations and certificates and provisioning profiles, etc.. All devices with the last version was working, but when I install the new version, just don't work anymore.

To solve this, I just did this:

  1. Went to the target capabilities;
  2. Turn the Push Notification Off;
  3. Build & Run the app on device and wait;
  4. Stop running the app;
  5. Turn the Push Notification On again.
  6. Build & Run the app on device;
  7. And like magic, it worked again

After 6h fighting with Xcode, that was my solution, without any explanation.

I hope this help someone.

I was asked to debug similar behaviour on a clients app, under iOS9.

It turns out the app was calling registerUserNotificationSettings and registerForRemoteNotifications from the appDelegate as is common.

However, it was also doing this a second time shortly after in a subsequent permissionsViewController.

If a user declined the request resulting from the appDelegate then the subsequent attempts to allow notifications after a user had seen a rationale for allowing it (the second calls from the permissionsViewController) were set with types set to zero same as the appDelegate calls.

Removing the initial appDelegate calls and having only the permissionsViewController calls present solved this.

Another thing to check is the system status of APNS at Apple https://developer.apple.com/system-status/.

I was tearing my hair out wondering why I couldn't register my device while the APNS system was 'experiencing problems'.

try using this: (works well), sorry stack doesn't seem to format this well

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        NSLog(@"iOS 8 Requesting permission for push notifications..."); // iOS 8
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:
                                                UIUserNotificationTypeAlert | UIUserNotificationTypeBadge |
                                                UIUserNotificationTypeSound categories:nil];
        [UIApplication.sharedApplication registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        NSLog(@"iOS 7 Registering device for push notifications..."); // iOS 7 and earlier
        [UIApplication.sharedApplication registerForRemoteNotificationTypes:
         UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |
         UIRemoteNotificationTypeSound];
    }


    return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{

    UIApplicationState state = [application applicationState];


    if (state == UIApplicationStateActive)
    {

        NSLog(@"received a notification while active...");

    }
    else if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground  )
    {
        //opened from a push notification when the app was on background
        NSLog(@"i received a notification...");
        NSLog(@"Message: %@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
        NSLog(@"whole data: %@",userInfo);
    }

}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    self.token = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    self.token = [self.token stringByReplacingOccurrencesOfString:@" " withString:@""];
    [[NSUserDefaults standardUserDefaults] setObject:self.token forKey:@"deviceToken"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    NSLog(@"-->> TOKEN:%@",self.token);
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
    NSLog(@"Registering device for push notifications..."); // iOS 8
    [application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier
forRemoteNotification:(NSDictionary *)notification completionHandler:(void(^)())completionHandler
{
    NSLog(@"Received push notification: %@, identifier: %@", notification, identifier); // iOS 8 s
    completionHandler();
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    // Respond to any push notification registration errors here.
    NSLog(@"Failed to get token, error: %@", error);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!