iOS 应用每次都接收新的 APNS 设备令牌
iOS app is receiving a new APNS device token every time
我正在使用 Azure 推送通知,我注意到每次启动我的应用程序时 APNS 都会向我发送一个新的设备令牌。根据 Apple 的文档,设备令牌通常应保持不变:
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.
然而,根据通过 Azure 发送的失败消息的恒定数量,我可以推断出我的应用程序在每次启动应用程序时都会从 APNS 接收一个新的设备令牌。有人可以告诉我吗:
为什么我每次都收到一个新的设备令牌,即使我没有更改 phone 设置或删除应用程序?
Apple 在上面的摘录中,"deleting device's data" 是什么意思? APNS究竟使用什么数据来确定"unique app and device combination"?
代码如下:
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeAlert |
UIUserNotificationTypeSound)
categories:nil];
[application registerUserNotificationSettings:settings];
} else {
[application registerForRemoteNotifications];
}
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//initiate registration process with Apple Push Notification service
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *) token {
// Setting token with Azure hub
[[MyAzureNotificationHub sharedInfo] setDeviceTokenData:token];
}
你可以阅读这个:
Never cache device tokens; always get them from the system when you need them. Although device tokens are unique to an app and device, they can change over time. The device token can change at any time but is guaranteed to be different when the user restores their 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 from the system ensures that you always have the current token needed to communicate with APNs. In addition, if the token has not changed, fetching it is fast and does not incur any significant overhead.
根据我使用 APNS 的经验,令牌会在您注册远程通知(重新启动应用程序)时随时更改。每次您应该向后端重新发送新令牌时,它都会使用最新的令牌发送推送通知。
简短的回答是设备令牌不应在每次应用程序向 APNS 注册时更改。来自 Apple:
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
我的应用程序每次都获得一个新的设备令牌,因为我没有正确地将 deviceToken 的数据类型从数据转换为字符串。 APNS 将设备令牌作为数据类型发送,但要将其传递给 Azure,我需要先使用以下函数将令牌转换为字符串:
private func convertDataToString(data:NSData) -> String {
let singleChar = UnsafePointer<CChar>(data.bytes)
var tokenbuilder = String()
if data.length > 0
{
for index in 0...data.length - 1
{
tokenbuilder += String(format: "%02.2hhx", arguments: [singleChar[index]])
}
return tokenbuilder
}
else
{
return ""
}
}
在我的例子中,我忘记了第一个字符的位置从 0(而不是 1)开始,因此,原来的 for 循环从 0 到 data.length(而不是 data.length - 1), 导致函数 return 一个带有随机额外字符的字符串。即使字符串中包含额外的字符,该应用程序也确实会收到推送通知;但是,我们在 Azure 日志中也有大量无效令牌。这也导致用户每次 he/she 启动应用程序(并注册设备令牌)时都会收到一个新的 deviceToken。一旦我修复了 for 循环,只要应用程序没有被删除(因此,删除设备数据)并重新安装到我的设备上,APNS 就会开始 return 向用户发送相同的设备令牌;因此,创建一个 "unique app and device combination".
我正在使用 Azure 推送通知,我注意到每次启动我的应用程序时 APNS 都会向我发送一个新的设备令牌。根据 Apple 的文档,设备令牌通常应保持不变:
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.
然而,根据通过 Azure 发送的失败消息的恒定数量,我可以推断出我的应用程序在每次启动应用程序时都会从 APNS 接收一个新的设备令牌。有人可以告诉我吗:
为什么我每次都收到一个新的设备令牌,即使我没有更改 phone 设置或删除应用程序?
Apple 在上面的摘录中,"deleting device's data" 是什么意思? APNS究竟使用什么数据来确定"unique app and device combination"?
代码如下:
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeAlert |
UIUserNotificationTypeSound)
categories:nil];
[application registerUserNotificationSettings:settings];
} else {
[application registerForRemoteNotifications];
}
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//initiate registration process with Apple Push Notification service
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *) token {
// Setting token with Azure hub
[[MyAzureNotificationHub sharedInfo] setDeviceTokenData:token];
}
你可以阅读这个:
Never cache device tokens; always get them from the system when you need them. Although device tokens are unique to an app and device, they can change over time. The device token can change at any time but is guaranteed to be different when the user restores their 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 from the system ensures that you always have the current token needed to communicate with APNs. In addition, if the token has not changed, fetching it is fast and does not incur any significant overhead.
根据我使用 APNS 的经验,令牌会在您注册远程通知(重新启动应用程序)时随时更改。每次您应该向后端重新发送新令牌时,它都会使用最新的令牌发送推送通知。
简短的回答是设备令牌不应在每次应用程序向 APNS 注册时更改。来自 Apple:
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
我的应用程序每次都获得一个新的设备令牌,因为我没有正确地将 deviceToken 的数据类型从数据转换为字符串。 APNS 将设备令牌作为数据类型发送,但要将其传递给 Azure,我需要先使用以下函数将令牌转换为字符串:
private func convertDataToString(data:NSData) -> String {
let singleChar = UnsafePointer<CChar>(data.bytes)
var tokenbuilder = String()
if data.length > 0
{
for index in 0...data.length - 1
{
tokenbuilder += String(format: "%02.2hhx", arguments: [singleChar[index]])
}
return tokenbuilder
}
else
{
return ""
}
}
在我的例子中,我忘记了第一个字符的位置从 0(而不是 1)开始,因此,原来的 for 循环从 0 到 data.length(而不是 data.length - 1), 导致函数 return 一个带有随机额外字符的字符串。即使字符串中包含额外的字符,该应用程序也确实会收到推送通知;但是,我们在 Azure 日志中也有大量无效令牌。这也导致用户每次 he/she 启动应用程序(并注册设备令牌)时都会收到一个新的 deviceToken。一旦我修复了 for 循环,只要应用程序没有被删除(因此,删除设备数据)并重新安装到我的设备上,APNS 就会开始 return 向用户发送相同的设备令牌;因此,创建一个 "unique app and device combination".