删除应用程序后获取推送通知的设备令牌 [英] Get device token for push notifications after app was deleted

查看:25
本文介绍了删除应用程序后获取推送通知的设备令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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];
}

但问题是,当从设备中删除应用程序时,NSUserDefaults 会被擦除,但推送设置不会.所以它不会再询问,因此不再调用委托方法,因此我不再有令牌,但仍然激活推送.

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?

推荐答案

来自 推送通知编程指南

应用程序应该在每次启动时注册并提供提供当前令牌.它调用registerForRemoteNotificationTypes:方法来启动注册过程.

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.

通过每次请求设备令牌并将其传递给提供者当你的应用程序启动时,你帮助确保提供者具有设备的当前令牌.如果用户将备份恢复到除为其创建备份的设备或计算机之外的设备或计算机(例如,用户将数据迁移到新设备或计算机),他或者她必须至少启动应用程序一次才能接收再次通知.如果用户将备份数据恢复到新设备或计算机,或重新安装操作系统,设备令牌变化.此外,永远不要缓存设备令牌并将其提供给您的提供者;随时从系统获取令牌.如果您的应用程序之前已注册,请致电registerForRemoteNotificationTypes:在操作系统中的结果立即将设备令牌传递给委托,而不会产生额外的开销.

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:,并使用最新的令牌.

To answer your question: Call registerForRemoteNotificationTypes: on every launch, and use the latest token.

这篇关于删除应用程序后获取推送通知的设备令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆