Device Token - Apple push Notification Service - ipad

is device token changes each time when i opens my application?
Apple server uses the same device token every time or the new regenerated device token?

You can check developer documentaion, following is mentioned there -
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.
So they are uniques to iPhone until OS is reinstalled.
You can check details here - http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html#//apple_ref/doc/uid/TP40008194-CH100-SW12

Related

Does a iOS Push notification device token change?

Is there any possibilities for a device token to change after the app is installed and registered for the notification in any kind of scenarios.
Scenarios like:
App update
iOS update
or in any other random scenario's.
Previously in document it was like,
If the user restores backup data to a new device or reinstalls the
operating system, the device token changes.
From new documentation here:
https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html
Important: To protect user privacy, do not attempt to use a device
token to identify a device.
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, as described in APNs-to-Device Connection Trust and Device Tokens.

Push notification device tokens and their validity

I have a general question to device tokens for push notifications.
this is a part of the apple documentation:
A device token is not a unique ID that you can use to identify a
device. Device tokens can change after updating the operating system
on a device. As a result, apps should send their device token
I understood that each app on a device get an unique token.
For example, the token of a app on my device is XYZ
With this token I can send from my server an push notification to XYZ and I will received it on my device.
According to Apple, the device token can change after an OS update
but what happens if the device token of my device changes and I don't start the app for a few days, so that the new device token isn't in my token table in my database?
What will be happen with the notification, which I will send to the old device token?

Should we register for iOS push notification every time during application launch?

We are using remote notifications in our iOS application and we are registering to APNS server inside application: didFinishLaunching: delegate method. That means it will be registered with APNS server every time the application launched freshly. Below is the code snippet.
[[UIApplication sharedApplication] registerForRemoteNotifications]
We have noticed that the device token (which is delivered to us by APNS server) is same every time. So, we thought that we can save the token in NSUserDefaults and use it later. In application: didFinishLaunching: method, we can give a check if the device token is present in NSUserDefaults or not. If it's available, we can use the same instead of registering to APNS. Otherwise, we can opt to register with APNS.
But, from the Apple documentation, it's observed that "they encourage us to register for remote notifications each time the application is launched freshly". Below is the screenshot from docs.
When does the device token value change actually? Can I afford to store the device token in NSUserDefaults and use it later instead of registering every time? Please help!! Thanks in advance!!
To answer your question with some apple documentation:
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.
This means you shouldn't really store the token in your NSUserDefaults.
However if you want to save it anyway I recommend to save it in the Keychain.
See this example how to do so:
Store Device Token in Keychain
Another quote from apple documentation:
"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."
EDIT:
Looks like the above links to Apple's documentation are broken by now. Here is a updated link (thanks to #Enrico Cupellini): https://developer.apple.com/library/content/technotes/tn2265/_index.html

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

Can / will the APN Token ever change?

It seems to be customary for apps that are using APN to get the token from the OS whenever the app launches. However if an app has already obtained it once, is there any point in it bothering to obtain it again (and consequently sending it to the server if its already done so).
Can the APN token obtained from the OS ever change?
The answer to your question is inside Apple docs (Local and Push Notification Programming Guide):
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 or computer other than the one that the backup was created for (for example, the user migrates data to a new device or computer), 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 computer, 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 the operating system passing the device token to the delegate immediately without incurring additional overhead.

Resources