ios - 删除应用程序后获取推送通知的设备 token

标签 ios objective-c apple-push-notifications

当您第一次安装应用程序并想要注册推送通知时,该应用程序会询问您是否要接收提醒。这将永久保存在设置中,即使在删除应用程序后也是如此。

基本上,为了保存 token ,我们这样做:

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

但问题是,当应用程序从设备中删除时,NSUserDefaults 会被删除,但推送设置不会。所以它不会再询问,因此不会再调用委托(delegate)方法,因此我没有 token 了,但推送仍然激活。

在描述的场景中是否有机会取回 token ?

最佳答案

来自 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.

回答您的问题:在每次启动时调用 registerForRemoteNotificationTypes:,并使用最新的 token 。

关于ios - 删除应用程序后获取推送通知的设备 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16982216/

相关文章:

ios - 如何从 ios 中的应用程序文件夹中按日期对 uiimages 进行排序

push-notification - 在 iPhone 上推送通知而不向用户发出警报

JAVAPNS 2.2 成功推送但未在设备上接收

iphone - NSMutable 字符串 "Out of scope"

iphone - 当用户收到推送通知时打开特定的选项卡/ View

ios - 嵌套的 dispatch_async(dispatch_async 在另一个 dispatch_async 中)

ios - 将 Picker 设置为默认的 CoreData 值?

iphone - 如何在 iOS 的 ViewController 中的其他 View 下方添加 View ?

objective-c - 如何在 NSOpenPanel 中添加用于创建新文件的按钮?(不是创建文件夹按钮)

ios - 将 CGPoints 的 NSArray 转换为 NSData 的 block