我需要获得deviceToken。
当我向我的应用添加推送通知时,我通过didRegisterForRemoteNotificationsWithDeviceToken
但是现在我需要再次获得它而且我没有保存。
didRegisterForRemoteNotificationsWithDeviceToken
只调用一次,其他时间只调用registerUserNotificationSettings
我想将所有新用户的令牌保存到[NSUserDefaults standardUserDefaults]
,但它只适用于新用户。如何处理当前用户?
答案 0 :(得分:1)
在“didFinishLaunchingWithOptions”方法中添加以下代码...
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)];
}
然后在应用程序启动期间每次调用“didFinishLaunchingWithOptions”时都会调用“didRegisterForRemoteNotificationsWithDeviceToken”方法。