Get device token for push notifications after app was deleted

13,316

Solution 1

From Push Notification Programming Guide

An application should register every time it launches and give its provider the current token. It calls the registerForRemoteNotificationTypes: method to kick off the registration process.

By requesting the device token and passing it to the provider every time your application launches, you help to ensure that the provider has the current token for the device. If a user restores a backup to a device or computer other than the one that the backup was created for (for example, the user migrates data to a new device or computer), he or she must launch the application at least once for it to receive notifications again. If the user restores backup data to a new device or computer, or reinstalls the operating system, the device token changes. Moreover, never cache a device token and give that to your provider; always get the token from the system whenever you need it. If your application has previously registered, calling registerForRemoteNotificationTypes: results in the operating system passing the device token to the delegate immediately without incurring additional overhead.

To answer your question: Call registerForRemoteNotificationTypes: on every launch, and use the latest token.

Solution 2

call registerForRemoteNotificationTypes on every launch of your application so your didRegisterForRemoteNotificationsWithDeviceToken method get call and you will get your device token every time from APNS. And device token for your application is same on every launch.

Share:
13,316
Sebastian Wramba
Author by

Sebastian Wramba

B.Sc. Systems Engineering, University of Duisburg-Essen Several years of web, desktop and mobile software engineering Competences Software Engineering and Architectures Requirements Engineering User Interface Engineering Technical Project Management Development HTML5, CSS, JavaScript/jQuery Java SE/EE Cocoa Touch/Objective-C Mobile Development (iPhone, Android)

Updated on July 28, 2022

Comments

  • Sebastian Wramba
    Sebastian Wramba almost 2 years

    When you install an app for the first time and want to register for Push notifications, the app asks you whether you want to receive alerts or not. This is being permanently saved in the settings, even after deletion of the app.

    Basically, to save the token we are doing this:

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

    But the problem is, NSUserDefaults are wiped when the app is removed from the device, but the push settings are not. So it won't ask again, thus don't call the delegate method again, thus I don't have the token anymore, but push is still activated.

    Is there any chance to get the token back in the described scenario?

  • Sebastian Wramba
    Sebastian Wramba almost 11 years
    Thanks, you are absolutely right. I implemented the didFail method and as it turned out, it was just a certificate problem (Team Provisioning Profile does not work)
  • John
    John about 9 years
    Could you expand on how to implement this? When I delete, etc the new token is not registerered for some reason.