ios - 我们是否应该在应用程序启动期间每次都注册 iOS 推送通知?

标签 ios xcode

我们在我们的 iOS 应用程序中使用远程通知,我们在 application: didFinishLaunching: 委托(delegate)方法中注册到 APNS 服务器。这意味着每次应用程序重新启动时,它都会在 APNS 服务器上注册。下面是代码片段。

[[UIApplication sharedApplication] registerForRemoteNotifications]

我们注意到设备 token (由 APNS 服务器发送给我们)每次都是相同的。 因此,我们认为我们可以将 token 保存在 NSUserDefaults 中以备后用。在 application: didFinishLaunching: 方法中,我们可以检查设备 token 是否存在于 NSUserDefaults 中。如果它可用,我们可以使用它而不是注册到 APNS。否则,我们可以选择注册 APNS。

但是,从 Apple 文档中可以看出,“他们鼓励我们在每次新启动应用程序时注册远程通知”。以下是文档的屏幕截图。

enter image description here

设备 token 值实际何时更改?我可以负担得起将设备 token 存储在 NSUserDefaults 中并在以后使用它而不是每次都注册吗?请帮忙!!提前致谢!!

最佳答案

用一些苹果文档回答你的问题:

The form of this phase of token trust ensures that only APNs generates the token which it will later honor, and it can assure itself that a token handed to it by a device is the same token that it previously provisioned for that particular device—and only for that device.

If the user restores backup data to a new device or reinstalls the operating system, the device token changes.

这意味着您不应该真正将 token 存储在您的 NSUserDefaults 中。

但是,如果您无论如何都想保存它,我建议将其保存在钥匙串(keychain)中。 请参阅此示例如何执行此操作: Store Device Token in Keychain

苹果文档中的另一段引述:

"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 other than the one that the backup was created for (for example, the user migrates data to a new device), 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 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 iOS passing the device token to the delegate immediately without incurring additional overhead."

编辑: 看起来上面指向 Apple 文档的链接现在已损坏。这是一个更新的链接(感谢@Enrico Cupellini):https://developer.apple.com/library/content/technotes/tn2265/_index.html

关于ios - 我们是否应该在应用程序启动期间每次都注册 iOS 推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29740913/

相关文章:

ios - 在 webview 中打开图像(当图像和 css 存储在本地存储中时)

ios - 在 objective-c 中扩展 tableview 时按钮不起作用?

objective-c - ARC - 取消分配图片库选择器?

ios - 静态库的 Xcode 7 存档错误

ios - 核心数据 : How to display results of a fetchrequest for calculating values in a tableview cell

ios - Firebase Crashlytics DSYM 文件

iphone - AVAudioPlayer 错误加载文件

ios - 在IOS应用之间创建链接

ios - JSon 和下标的模糊使用

c++ - 我怎么可能从一个还没有定义一些成员方法的类中创建一个实例呢? (C++)