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?
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.
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.