Deep
Deep

Reputation: 41

Does device token ever change in Apple Push Notifications

I am developing an iOS app in which i have implemented Push Notification. Everything is working fine. But just wish to ask if device Token for my Apple device will ever change??

Also do we need internet connectivity for generating device token.

Thanks

Upvotes: 4

Views: 11709

Answers (4)

Derek Lee
Derek Lee

Reputation: 3670

I was recently troubleshooting an issue with push notifications for a user. I was a bit confused by the guidance here, indicating that the token would only change in rare circumstances, such as "moving to a new device" or "re-installing the OS".

While the above events are likely valid events where the token is updated, I also see the token is updated simply when the user updates the OS on their device.

For example:

  • When my device runs iOS 15.4.1, and I install an application and request a push notification token, I'm provided with token "A".
  • After I update my device to iOS 15.7, with the same application still installed, and request a push notification token, I'm provided with token "B".

Therefore while rare events like restoring backup data to a new device or re-installing the OS are valid for this case, more frequent events such as a user simply updating the OS of their device are also valid for causing the push notification token to change.

Thus, this will likely change quite frequently and should always be requested from the device and updated on your server.

Upvotes: 0

Jayachandra A
Jayachandra A

Reputation: 1342

From the apple docs -

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.

For more APNS Doc

Upvotes: 0

Nayan
Nayan

Reputation: 3014

device Token for my Apple device will ever change

-- YES. If you restores backup data to a new device or reinstall the operating system, the device token changes. So my suggestion is to update the server with token

do we need internet connectivity for generating device token

-- as far as I know, YES. When you register user, you call method for registration for push notification. This on successful registration call the delegate method -

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

indicating you are registered successfully for a push notification or on failure it calls -

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error

indicating failed to register for notification.

You can check it by turning off network and running your application.

Upvotes: 5

Lucas Huang
Lucas Huang

Reputation: 4016

Based on the Apple Documentation, the answer is YES:

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.

Upvotes: 2

Related Questions