APNS 设备令牌创建后是否会更改?

发布于 2024-11-19 17:34:28 字数 79 浏览 2 评论 0原文

创建后,推送通知设备令牌是否会发生变化?

示例应用何时更新?或者在任何其他情况下它可以改变?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

潇烟暮雨 2024-11-26 17:34:28

苹果官方文档对此并没有明确说明。我观察到的是:令牌对于给定的设备、应用程序和域(生产与沙箱)是不变的。我相信为了使系统可靠地工作,这一点必须保持不变。考虑应用程序更新触发新 APN 令牌的情况;如果我使用的是最棒的类似 Twitter 的新应用程序,并且启用了通知,那么当我从 iTunes 更新我的应用程序时会发生什么?我是否应该期望它会继续发送通知,即使自从我将更新“同步”到我的设备后我还没有运行应用程序?更改应用程序的行为不会影响 APN 系统,因为即使您尚未运行更新的应用程序,操作系统也可以代表您接收通知。

需要明确的是,Apple 声明“应用程序应在每次启动时[向 APN 服务器]注册,并向其提供商提供当前令牌”。我完全同意;这样做可以保护您的应用程序免受错误假设或异常情况的影响。

的答案之一是推送通知单个设备的所有应用程序中的令牌都是唯一的? 表示设备令牌在每个“操作系统安装”中都是唯一的;从备份恢复到设备将保留令牌,但擦除设备将导致其获得新令牌。这完全符合苹果无缝操作和隐私的意图:擦除设备的行为非常严重,也许需要建立新的关联,但在操作系统更新后恢复图像的用户会希望保留其现有的通知。如果我记得最近在我的 iPad 上进行了 iOS5 更新,我在升级后恢复了最近的备份,因此这将保持我的通知令牌的一致性。 [编辑:将备份恢复到不同设备不会复制令牌。]

警告:
我对这个主题没有明确的知识,只是有一些与 APN 合作的合理经验(作为第三方开发人员)。与往常一样,最好验证您的假设。


更新(2012 年 6 月):

我最近有机会a>与 Apple 工程师交谈并 b>运行一些真实世界的测试,我想展示结果:

为了完整起见,当我谈论返回 APN 令牌时,我假设单个捆绑包标识符/应用程序的上下文。

首先,苹果工程师表示,两个设备不应该返回相同的 APN。尽管有下面的评论,我仍无法确定失败的情况。

其次,这是升级测试顺序和结果:

  1. 从iPhone4上安装的iOS4开始; iTunes中的备份设备

  2. 升级到 iOS5
    从之前的测试中,我知道 APN 令牌现在不同了

  3. 将备份恢复到设备
    APN 令牌现在与步骤 1 相同。

  4. 重置 iOS(干净设备)
    APN 令牌更改

  5. 将另一部手机备份到 iTunes 并将该备份恢复到测试设备;基本上,我正在恢复“错误”的备份,就像我正在切换手机一样。
    APN 令牌再次发生变化;此外,它是不同的,并且与原始令牌或“克隆”令牌都不匹配。

  6. 将“正确”备份恢复到设备。
    APN 令牌现在与步骤 1 相同。

  7. 最后,我将手机升级到 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:

  1. Start with iOS4 installed on iPhone4; backup device in iTunes

  2. Upgrade to iOS5
    From a previous test, I know that the APN token is now different

  3. Restore the backup to the device
    The APN token is now the same as step 1.

  4. Reset iOS (clean device)
    The APN Token changes

  5. 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.

  6. Restore the "correct" backup to the device.
    The APN token is now the same as step 1.

  7. 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.]

给我一枪 2024-11-26 17:34:28

来自 [Apple 文档 ApplePushService]2

此阶段令牌信任的形式确保只有 APN 生成
它稍后将兑现的令牌,并且它可以向自己保证
设备传递给它的令牌与之前的令牌相同
为该特定设备配置——并且仅针对该设备。

如果用户将备份数据恢复到新设备或重新安装
操作系统,设备令牌发生变化。

From [Apple Documentation ApplePushService]2

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.

花落人断肠 2024-11-26 17:34:28

我刚刚使用 iOS9 和 APN 推送令牌更改(如果我重新安装应用程序)对其进行了测试。

I've just tested it with iOS9 and APN Push token changes if I reinstall an app.

心欲静而疯不止 2024-11-26 17:34:28

,设备令牌可以更改。

每当您的应用程序收到令牌时,它都应该存储它。然后,每当收到新令牌(最终将会发生)时,将新令牌与存储的令牌进行比较,如果不同,则:

  1. 更新设备的本地存储(包括可能的>nil)
  2. 更新设备上使用该令牌以了解新令牌的所有内容
  3. 将任何了解此令牌的 API 更新为新令牌。

实际上,最后一步很可能并不简单。例如,如果您有一项服务根据设备订阅的邮政编码向设备令牌发送天气警报,那么您需要传递 old_tokennew_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:

  1. Update the device's local storage, (including possibly to nil)
  2. Update anything on the device which uses the token to be aware of the new token
  3. Update any APIs which are aware of this token to the new token.

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 the new_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.

蒗幽 2024-11-26 17:34:28

从 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

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.

A君 2024-11-26 17:34:28

我认为值得一提的是,在您调用 unregisterForRemoteNotifications 后,令牌发生了变化,没有人这样做。当您下次调用 registerForRemoteNotifications 时,令牌会有所不同。我没有在苹果文档中找到任何对此的确认,但我亲眼目睹了这种行为。请记住这一点

I think it is worth mentioning as nobody did it that the token changes after you have called unregisterForRemoteNotifications. When you call registerForRemoteNotifications 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

随梦而飞# 2024-11-26 17:34:28

苹果的链接很快就会过时!所以我引用现在看来很清楚的内容:

切勿在您的应用中缓存设备令牌;相反,当您需要它们时从系统中获取它们。当某些事件发生时,APN 会向您的应用程序发出新的设备令牌。设备令牌保证不同,例如,当用户从备份恢复设备时、当用户在新设备上安装您的应用程序时以及当用户重新安装操作系统时。获取令牌(而不是依赖缓存)可确保您拥有提供商与 APN 通信所需的当前设备令牌。当您尝试获取设备令牌但它未更改时,获取方法会快速返回。

摘自本指南

Links quickly become obsolete with apple! so i'm quoting what seems to be quite clear now :

Never cache device tokens in your app; instead, get them from the system when you need them. APNs issues a new device token to your app when certain events happen. The device token is guaranteed to be different, for example, when a user restores a device from a backup, when the user installs your app on a new device, and when the user reinstalls the operating system. Fetching the token, rather than relying on a cache, ensures that you have the current device token needed for your provider to communicate with APNs. When you attempt to fetch a device token but it has not changed, the fetch method returns quickly.

From this guide

于我来说 2024-11-26 17:34:28

来自 - Apple 文档

APN 可以出于多种原因颁发新的设备令牌:

  • 用户在新设备上安装您的应用

  • 用户从备份恢复设备

  • 用户重新安装操作系统

  • 其他系统定义的事件

因此,应用必须在启动时请求设备令牌。

此外:

重要

APN 设备令牌的长度可变。不要对它们进行硬编码
尺寸。


来自最新文档 (2023)

切勿在本地存储中缓存设备令牌。 APNs 发行新令牌
当用户从备份恢复设备时、当用户安装时
您的应用程序在新设备上,并且当用户重新安装操作时
系统。每次要求系统时,您都会获得一个最新的令牌
提供令牌。

From - Apple Docs

APNs can issue a new device token for a variety of reasons:

  • User installs your app on a new device

  • User restores device from a backup

  • User reinstalls the operating system

  • Other system-defined events

As a result, apps must request the device token at launch time.

Additionally:

I M P O R T A N T

APNs device tokens are of variable length. Do not hard-code their
size.


From Latest Docs (2023)

Never cache device tokens in local storage. APNs issues a new token
when the user restores a device from a backup, when the user installs
your app on a new device, and when the user reinstalls the operating
system. You get an up-to-date token each time you ask the system to
provide the token.

最单纯的乌龟 2024-11-26 17:34:28

它不应该改变,除非您的应用程序恢复到新设备上(此时不会再次要求它接受推送通知,并且只会向您发送注册的呼叫,此时您应该接受新令牌)。

但苹果不保证它永远不会改变(因此文档从未提及它)。你最好做好最坏的打算,并假设有一天情况可能会改变。此外,定期向服务器发送令牌使您能够删除一段时间未注册的令牌,并且可能已经卸载了您的应用程序或不久前失去了兴趣(并且文档确实将此指定为所需行为!)。

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!).

国产ˉ祖宗 2024-11-26 17:34:28

作为参考
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.

你的往事 2024-11-26 17:34:28

根据 此链接 设备令牌

每个请求中包含的设备令牌代表了身份
接收通知的设备。 APNs 使用设备令牌来
识别每个独特的应用程序和设备组合。
它还使用它们来
验证发送到设备的远程通知的路由。
每次您的应用程序在设备上运行时,它都会从 APNs 获取此令牌
并将其转发给您的提供商。您的提供商存储令牌并
在向特定应用程序和设备发送通知时使用它。
令牌本身是不透明且持久的,仅当
设备的数据和设置将被删除。
只有 APN 可以解码和读取
设备令牌。

According to this link the device token

The device token included in each request represents the identity of
the device receiving the notification. APNs uses device tokens to
identify each unique app and device combination.
It also uses them to
authenticate the routing of remote notifications sent to a device.
Each time your app runs on a device, it fetches this token from APNs
and forwards it to your provider. Your provider stores the token and
uses it when sending notifications to that particular app and device.
The token itself is opaque and persistent, changing only when a
device’s data and settings are erased.
Only APNs can decode and read a
device token.

简单 2024-11-26 17:34:28

是的它可以改变。
理想情况下,当我们通过回调方法收到令牌时

  • (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

应用程序应在远程服务器上注册/刷新令牌。这将确保 APNS 和您的服务器上的令牌保持同步。

根据 Apple 文档,

获取和处理特定于应用程序的设备令牌的工作原理如下:

当有新消息出现时,您的应用会向 APN 注册以进行远程通知
需要设备令牌,APNs 使用包含的信息生成一个设备令牌
在设备的证书中。它使用令牌密钥加密令牌
并将其返回给设备,如中间向右所示
箭。系统通过以下方式将设备令牌返回给您的应用程序:
打电话给你的
应用程序:didRegisterForRemoteNotificationsWithDeviceToken:委托
方法。收到令牌后,您的应用程序(在委托内
方法)必须以二进制或
十六进制格式。您的提供商无法向
没有此令牌的设备。详情请参见注册接收
配置远程通知支持中的远程通知。

Yes it can change.
Ideally when ever we receive a token via the callback method

  • (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

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,

Obtaining and handling an app-specific device token works as follows:

Your app registers with APNs for remote notifications When a new
device token is needed, APNs generates one using information contained
in the device’s certificate. It encrypts the token using a token key
and returns it to the device, as shown in the middle, right-pointing
arrow. The system delivers the device token back to your app by
calling your
application:didRegisterForRemoteNotificationsWithDeviceToken: delegate
method. Upon receiving the token, your app (within the delegate
method) must forward it to your provider in either binary or
hexadecimal format. Your provider cannot send notifications to the
device without this token. For details, see Registering to Receive
Remote Notifications in Configuring Remote Notification Support.

顾冷 2024-11-26 17:34:28

设备令牌在应用程序安装时中继。

这意味着如果您重新安装应用程序,它就会发生变化;如果您从备份、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 method didRegisterForRemoteNotificationsWithDeviceToken

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文