Search code examples
iosobjective-capple-push-notificationsios13

didRegisterForRemoteNotificationsWithDeviceToken not called in ios8, but didRegister...Settings is


I followed this thread, but the method didRegisterForRemoteNotificationsWithDeviceToken is still not called :

the documentation says :

After you call the registerForRemoteNotifications method of the UIApplication object, the app calls this method when device registration completes successfully

didRegisterUser appears well, but not did register notif.

Here is my code in the AppDelegate (the app version is 8.1) :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //register notif
    UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                    UIUserNotificationTypeBadge |
                                                    UIUserNotificationTypeSound);
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
    [application registerUserNotificationSettings:settings];


    return YES;
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    //register to receive notifications
    [application registerForRemoteNotifications];
    NSLog(@"didRegisterUser");
}

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"error here : %@", error);//not called
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    /*
    // Store the deviceToken in the current installation and save it to Parse.
    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [currentInstallation setDeviceTokenFromData:deviceToken];
    currentInstallation.channels = @[ @"global" ];
    [currentInstallation saveInBackground];
     */
    NSLog(@"did register notif");//not called
}

I also have background mode -> remote notification in the info.plist.


Solution

  • Your code seems to be correct. As a minor improvement you could write your didRegisterUserNotificationSettings method like so:

    - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
        if (notificationSettings.types != UIUserNotificationTypeNone) {
            NSLog(@"didRegisterUser");
            [application registerForRemoteNotifications];
        }
    }
    

    There may be a configuration problem that causes a failed APN registration.

    1. Assure that your Provisioning Profile contains the aps-environment entry

    2. Assure that you have a unique App Identifier (a string without any "*") set in your Provisioning Profile. You should also use this exact identifier as "Bundle identifier" in your Info.plist

    3. Maybe you have declined the Push-Feature after the initial installation - in this case you will never see the in-app-push-alert again and have to enable Push in the settings app again.

    4. Try another device.