Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get device token for push notifications after app was deleted

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?

like image 601
Sebastian Wramba Avatar asked Jun 07 '13 10:06

Sebastian Wramba


2 Answers

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.

like image 108
maroux Avatar answered Sep 21 '22 16:09

maroux


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.

like image 38
Vishal's p Avatar answered Sep 21 '22 16:09

Vishal's p