get ios push deviceToken second time

819 views Asked by At

I need to get deviceToken. When I added push notification to my app I received deviceToken via didRegisterForRemoteNotificationsWithDeviceToken

but now I need to get it again and i didnt save is. didRegisterForRemoteNotificationsWithDeviceToken calls just once and other times app runs just registerUserNotificationSettings

I thought to save tokens from all new users to [NSUserDefaults standardUserDefaults] but it will work just for new ones. What to do with current users?

1

There are 1 answers

0
Teja Kumar Bethina On

Add the following code in “didFinishLaunchingWithOptions” method…

if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
{
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
} 
else
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}

Then “didRegisterForRemoteNotificationsWithDeviceToken” method will be called on every call of “didFinishLaunchingWithOptions” during app launching.