我们在iOS应用程序中使用远程通知,我们正在注册应用程序内的APNS服务器:didFinishLaunching:delegate方法.这意味着每次应用程序刚刚启动时,它都将在APNS服务器上注册.以下是代码段.
[[UIApplication sharedApplication] registerForRemoteNotifications]
我们注意到设备令牌(由APNS服务器交付给我们)每次都是相同的.因此,我们认为我们可以在NSUserDefaults中保存令牌并在以后使用它.在application:didFinishLaunching:方法中,我们可以检查NSUserDefaults中是否存在设备令牌.如果它可用,我们可以使用相同而不是注册到APNS.否则,我们可以选择向APNS注册.
但是,从Apple文档中可以看出,“他们鼓励我们在每次新推出应用程序时注册远程通知”.以下是文档的截图.
设备令牌值何时实际更改?我是否能够将设备令牌存储在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.
这意味着您不应该将令牌真正存储在NSUserDefaults中.
但是,如果你想保存它,我建议将它保存在钥匙串中.
请参阅此示例如何执行此操作:
Store Device Token in Keychain
Apple文档的另一个引用:
“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