加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 18:09:37 所属栏目:百科 来源:网络整理
导读:我们在iOS应用程序中使用远程通知,我们正在注册应用程序内的APNS服务器:didFinishLaunching:delegate方法.这意味着每次应用程序刚刚启动时,它都将在APNS服务器上注册.以下是代码段. [[UIApplication sharedApplication] registerForRemoteNotifications]
我们在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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读