APNS 设备令牌创建后是否会更改?
创建后,推送通知设备令牌是否会发生变化?
示例应用何时更新?或者在任何其他情况下它可以改变?
Once created does the push notification device token ever change?
Example when the app is updated? or in any other case it can change??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
苹果官方文档对此并没有明确说明。我观察到的是:令牌对于给定的设备、应用程序和域(生产与沙箱)是不变的。我相信为了使系统可靠地工作,这一点必须保持不变。考虑应用程序更新触发新 APN 令牌的情况;如果我使用的是最棒的类似 Twitter 的新应用程序,并且启用了通知,那么当我从 iTunes 更新我的应用程序时会发生什么?我是否应该期望它会继续发送通知,即使自从我将更新“同步”到我的设备后我还没有运行应用程序?更改应用程序的行为不会影响 APN 系统,因为即使您尚未运行更新的应用程序,操作系统也可以代表您接收通知。
需要明确的是,Apple 声明“应用程序应在每次启动时[向 APN 服务器]注册,并向其提供商提供当前令牌”。我完全同意;这样做可以保护您的应用程序免受错误假设或异常情况的影响。
的答案之一是推送通知单个设备的所有应用程序中的令牌都是唯一的? 表示设备令牌在每个“操作系统安装”中都是唯一的;从备份恢复到设备将保留令牌,但擦除设备将导致其获得新令牌。这完全符合苹果无缝操作和隐私的意图:擦除设备的行为非常严重,也许需要建立新的关联,但在操作系统更新后恢复图像的用户会希望保留其现有的通知。如果我记得最近在我的 iPad 上进行了 iOS5 更新,我在升级后恢复了最近的备份,因此这将保持我的通知令牌的一致性。 [编辑:将备份恢复到不同设备不会复制令牌。]
警告:
我对这个主题没有明确的知识,只是有一些与 APN 合作的合理经验(作为第三方开发人员)。与往常一样,最好验证您的假设。
更新(2012 年 6 月):
我最近有机会a>与 Apple 工程师交谈并 b>运行一些真实世界的测试,我想展示结果:
为了完整起见,当我谈论返回 APN 令牌时,我假设单个捆绑包标识符/应用程序的上下文。
首先,苹果工程师表示,两个设备不应该返回相同的 APN。尽管有下面的评论,我仍无法确定失败的情况。
其次,这是升级测试顺序和结果:
从iPhone4上安装的iOS4开始; iTunes中的备份设备
升级到 iOS5
从之前的测试中,我知道 APN 令牌现在不同了
将备份恢复到设备
APN 令牌现在与步骤 1 相同。
重置 iOS(干净设备)
APN 令牌更改
将另一部手机备份到 iTunes 并将该备份恢复到测试设备;基本上,我正在恢复“错误”的备份,就像我正在切换手机一样。
APN 令牌再次发生变化;此外,它是不同的,并且与原始令牌或“克隆”令牌都不匹配。
将“正确”备份恢复到设备。
APN 令牌现在与步骤 1 相同。
最后,我将手机升级到 iOS6(beta2),恢复了备份并重新测试。正如预期,令牌继续与步骤 1 中的令牌匹配。
此时,我非常有信心 APN 令牌不能在不同设备之间重复;也许这可能是 iOS 早期版本中的一个错误,但我相信 iOS5(大概还有 iOS6)能够正确处理 APN 令牌。
更新(2012 年 8 月)
我刚刚意识到我没有添加此内容:设备令牌将会发生变化。一位苹果开发人员与我分享,令牌实际上会过期(我认为是 2 年后)。对于许多目的来说,这足够长,可以被认为是不变的。
[我不担心是否必须每两年使用新令牌更新我的测试脚本,特别是因为我每年都会更换手机。]
Apple's official documentation is unclear on this point. What I have observed is this: the token is invariant for a given device, application, and domain (production vs. sandbox). I believe that this must remain true in order for the system to work reliably. Consider the situation where an application update triggers a new APN token; if I were using the greatest new Twitter-like app, with notifications enabled, what would happen when I update my app from iTunes? Should I have the expectation that it will continue to be sent notifications even though I have not run the applications since I "sync" the update onto me device? The act of changing the application cannot affect the APN system since the OS can receive notifications on your behalf even though you haven't run the updated app.
To be clear, Apple states "An application should register [with APN servers] every time it launches and give its provider the current token". I wholeheartedly agree; doing so will protect your application from bad assumptions or unusual situations.
One of the answers to Are push notification tokens unique across all apps for a single device? indicates that device tokens are unique per "operating system install"; and that restoring from backup to a device would maintain the token but wiping a device will cause it to get a new token. This would be entirely consistent with Apple's intentions of seamless operation and privacy: wiping a device is severe enough that perhaps it warrants a new association, but a user restoring an image after an OS update would want to preserve their existing notifications. If I recall the recent iOS5 update on my iPad, I restored the most recent backup after upgrading, so this would have maintained my notification token's consistency. [Edit: restoring a backup to a different device will NOT duplicate the token.]
caveat:
I do not have definitive knowledge on the subject, just some reasonable experience working with APN (as a third-party developer). As always, it is best to verify your assumptions.
Update (June 2012):
I recently had a chance to a> talk to Apple engineers and b> run some real world tests, and I wanted to present the results:
To be complete, when I talk about returning an APN token, I am assuming the context of a single bundle identifier/application.
First, the Apple engineers said that it should not be possible for two devices to return the same APN. Notwithstanding the comments below, I have not been able to identify a circumstance where this fails.
Second, here is the upgrade test sequence and results:
Start with iOS4 installed on iPhone4; backup device in iTunes
Upgrade to iOS5
From a previous test, I know that the APN token is now different
Restore the backup to the device
The APN token is now the same as step 1.
Reset iOS (clean device)
The APN Token changes
Backup a different phone to iTunes and restore that backup to test device; basically, I'm restoring the "wrong" backup, as if I were switching phones.
The APN token changes again; further it is distinct and does not match the tokens either the original token or the "cloned" token.
Restore the "correct" backup to the device.
The APN token is now the same as step 1.
Lastly, I upgraded the phone to iOS6 (beta2), restored my backup, and re-tested. As expected, the token continued to match the token in step 1.
At this point, I'm pretty confident that APN tokens can't be duplicated between different devices; perhaps this may have happened as a bug in earlier versions of iOS, but I'm confident that iOS5 (and presumably iOS6) are handling APN tokens correctly.
Update (August 2012)
I just realized I had not added this: device tokens will change. One of the Apple devs shared with me that tokens do actually expire (after 2 years, I think). For many purposes, this is long enough that can be thought of as invariant.
[I'm not worried if I have to update my test scripts with new tokens every two years, especially since I change phones every year.]
来自 [Apple 文档 ApplePushService]2
From [Apple Documentation ApplePushService]2
我刚刚使用 iOS9 和 APN 推送令牌更改(如果我重新安装应用程序)对其进行了测试。
I've just tested it with iOS9 and APN Push token changes if I reinstall an app.
是,设备令牌可以更改。
每当您的应用程序收到令牌时,它都应该存储它。然后,每当收到新令牌(最终将会发生)时,将新令牌与存储的令牌进行比较,如果不同,则:
>nil
)实际上,最后一步很可能并不简单。例如,如果您有一项服务根据设备订阅的邮政编码向设备令牌发送天气警报,那么您需要传递
old_token
和new_token 到所述服务,以便它可以更新交付。
因此,一般来说,100% 接受“设备令牌”的 API 还必须具有针对该令牌的某种
UPDATE
工具。 不为此构建就是为错误传递和未传递的通知构建。YES, device tokens can change.
Anytime your app receives a token, it should store it. Then, whenever a new token is received (which will happen, eventually), compare the new token to the stored token and, if they are different:
nil
)As a practical matter, the last step is the most likely to be non-trivial. For example, if you have a service which is sending weather alerts to a device token based on what zip code that device has subscribed to, then you need to pass the
old_token
and thenew_token
to said service so it can update delivery.Ergo, generally speaking 100% of APIs accepting a "device token" must also have some kind of
UPDATE
facility for that token. To not build for this is to build for mis-delivered and non-delivered notifications.从 iOS 8 及更高版本开始,设备令牌确实发生了变化,
请参阅 Apple 网站上的以下文本。 注册、安排和处理用户通知
Device token does change from iOS 8 and later
Please see text below from Apple website. Registering, Scheduling, and Handling User Notifications
我认为值得一提的是,在您调用
unregisterForRemoteNotifications
后,令牌发生了变化,没有人这样做。当您下次调用registerForRemoteNotifications
时,令牌会有所不同。我没有在苹果文档中找到任何对此的确认,但我亲眼目睹了这种行为。请记住这一点I think it is worth mentioning as nobody did it that the token changes after you have called
unregisterForRemoteNotifications
. When you callregisterForRemoteNotifications
next time the token is different. I failed to find any confirmation of this in the Apple docs but I witnessed such a behavior myself. Please keep this in mind苹果的链接很快就会过时!所以我引用现在看来很清楚的内容:
摘自本指南
Links quickly become obsolete with apple! so i'm quoting what seems to be quite clear now :
From this guide
来自 - Apple 文档
此外:
来自最新文档 (2023)
From - Apple Docs
Additionally:
From Latest Docs (2023)
它不应该改变,除非您的应用程序恢复到新设备上(此时不会再次要求它接受推送通知,并且只会向您发送注册的呼叫,此时您应该接受新令牌)。
但苹果不保证它永远不会改变(因此文档从未提及它)。你最好做好最坏的打算,并假设有一天情况可能会改变。此外,定期向服务器发送令牌使您能够删除一段时间未注册的令牌,并且可能已经卸载了您的应用程序或不久前失去了兴趣(并且文档确实将此指定为所需行为!)。
It shouldn't change, unless your app is restored onto a new device (at which point it won't be asked to accept push notifications again, and will simply send you the registered call at which point you should accept the new token).
But Apple doesn't guarantee that it never changes (hence the documentation never mentions it). You better program for the worst and assume it may change one day. Also, sending a token to your server regularly enables you to remove tokens that haven't registered for a while, and have probably deinstalled your app or lost interest a while ago (and the documentation does specify this as wanted behavior!).
作为参考
Apple 推送通知内容
设备令牌是向特定设备上的应用程序发送推送通知的关键。设备令牌可能会发生变化,因此您的应用程序需要在每次启动时重新注册,并将收到的令牌传递回您的服务器。如果您无法更新设备令牌,远程通知可能无法到达用户的设备。当用户将备份数据恢复到新设备或计算机或重新安装操作系统时,设备令牌始终会发生变化。将数据迁移到新设备或计算机时,用户必须启动您的应用程序一次,然后才能将远程通知传递到该设备。
切勿缓存设备令牌;每当您需要时,始终从系统获取令牌。如果您的应用程序之前注册了远程通知,则再次调用 registerForRemoteNotifications 方法不会产生任何额外的开销,并且 iOS 会立即将现有设备令牌返回给您的应用程序委托。此外,只要设备令牌发生变化,iOS 就会调用您的委托方法,而不仅仅是响应您的应用程序注册或重新注册。
As reference to
Apple push notification stuff
The device token is your key to sending push notifications to your app on a specific device. Device tokens can change, so your app needs to reregister every time it is launched and pass the received token back to your server. If you fail to update the device token, remote notifications might not make their way to the user’s device. Device tokens always change when the user restores backup data to a new device or computer or reinstalls the operating system. When migrating data to a new device or computer, the user must launch your app once before remote notifications can be delivered to that device.
Never cache a device token; always get the token from the system whenever you need it. If your app previously registered for remote notifications, calling the registerForRemoteNotifications method again does not incur any additional overhead, and iOS returns the existing device token to your app delegate immediately. In addition, iOS calls your delegate method any time the device token changes, not just in response to your app registering or re-registering.
根据 此链接 设备令牌
According to this link the device token
是的它可以改变。
理想情况下,当我们通过回调方法收到令牌时
应用程序应在远程服务器上注册/刷新令牌。这将确保 APNS 和您的服务器上的令牌保持同步。
根据 Apple 文档,
Yes it can change.
Ideally when ever we receive a token via the callback method
The app should register / refresh the token on the remote server. This will ensure that the token on the APNS and your server is kept in sync.
As per Apple documentation,
设备令牌在应用程序安装时中继。
这意味着如果您重新安装应用程序,它就会发生变化;如果您从备份、iOS 升级等中执行此操作,这并不重要。
为了避免出现任何问题,使用它的正确方法是在每个应用程序中获取
NSPAppDelegate
上给出的一个启动,在方法didRegisterForRemoteNotificationsWithDeviceToken
中The device token relay on the installation of the app.
It means that if you reinstall the application, it changes; it doesn't metter if you do it from a backup, an iOS upgrade ecc..
The right way to use it, to avoid any problem, is to get the one given on the
NSPAppDelegate
at each application launch, in the methoddidRegisterForRemoteNotificationsWithDeviceToken