When working with push notifications via the Apple's APN service, when my app is installed and the user opts into receiving push notifications a device token is generated.
Is this device token unique to every single device or is there a global device token I can use to send push notification messages?
I am guessing if the device token is unique, I would have to save it up to a DB.
Well, Device token is unique but it become change when you reinstall it from appstore. You have to store that Token in database to send notification through that token. It should automatically register in database after installing with first time opening the app, It always hit but your database code should not reinsert it if same device token string found in the database.
NOTE: Device Token, UDID and UUID all are different and unique with some different usage purposes.
Related
I am adding Push Notifications to my app and I know I need to store the device tokens in my database so that I can send push notifications to specific devices (or all devices).
My question is what is the best practice for maintenance of these device tokens? I can store all device tokens as they are received, but how can I detect and remove old device tokens that are no longer valid? I assume a device token can become invalid if user deletes the app, or if user turns off notifications for the app.
Update - Having a user authentication and linking it to the device token (and updating based on login/logout) makes sense. But what about if the user deletes the app? there is no logout, how do you remove the device token then?
The Apple feedback service is no longer used. Instead we have to look for a response status of 401 from the apple push notification service to determine that a token is invalid.
To test this in a development environment. Use the trick below (it says it's for feedback service, but should work with the new status code from APNS as well)
How to test Apple Push Notifications Feedback Service?
Use the APNS feedback service, to find the device tokens that belonged to an app that was uninstalled.
See this apple documentation
If you are using something like Amazon SNS for push notifications, you can use their API to get list of disabled arns and remove the corresponding device tokens from your database.
Make an webservice which stores user's device token in database for particular user. Call this webservice only if user is logged in or you have identify user as per your requirement. You need to call this webservice when device successfully register for notification and if user is not identify (i.e. not logged-in) then call this service after login api.
Also pass device token when in login and register API if you have according to your flow and replace device token for particular user.
And when user logout just unregister for notification
As you said in your last statement I assume a device token can become invalid if user deletes the app, or if user turns off notifications for the app.
General scenario is when user again login to the app or register to the app you need to again take the device token from the user and need to store it in your database
Suppose I had one app and again I install only that at that time whenever I login again to the app at that time the api must having parameter for deviceID so whenever api call happen for login at that time new device token take place in your database by just replacing the old one. Same thing will happen for new register with that app.
Hope above description help you. :)
Your app server won't know if a particular app has been forcefully deleted & your server would still have let's say token T1 mapped to deleted app/device let's say A1. Now, it may be possible that another valid user (A2) comes up with same device token T1. You just need to make sure that at any point of time one device token (i.e. T1) is mapped to only one device (the device which has provided the T1 latest). All other older device token mappings i.e. A1-T1 shall be deleted at this point else A2 might receive A1's notifications.
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?
I have been developing an iOS app, which is sending remote notifications. For that I am saving the device token on our server. When the user logs in to our app, the device token saves on our server and remote notifications work fine.
But after some time, the notifications stop coming. I think it's due to that the device token changes but doesn't get updated on our server. I studied Apple's document on remote notifications; it says that if the device token ever changes, then it is the responsibility of the iOS to call the
didRegisterForRemoteNotificationsWithDeviceToken
of the app delegate with the updated device token. I have written the code to save the device token to our server in this method, but the device token does not change and remote notifications do not come.
Do I have to update the device token myself? If yes, then where to do that?
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.
iOS Device Token for the push notification is an opaque identifier of a device.
The provider can't get any device related information from the device token.
Based on Apple Push Notification programming guide, the provider needs to provide device token and payload to APNs server for the push notification.
Can provider send phone number OR bundle id OR advertising identifier OR vendor identifier to the APNs Server when the provider send token and payload to APNs server during push notification ?
So, the APNs server will not send the message to device if the device token is not associated with any device properties (like phone number or advertising identifier or vendor id).
I think you answered your own question:
Based on Apple Push Notification programming guide, the provider needs to provide device token and payload to APNs server for the push notification.
If you could provide the phone number, for example, you could simply use harvested phone numbers and spam people with push notifications. The token is not only for identifying the phone; you only get one if the user explicitly said "Yes, I want push notifications."
The only properties that determine if a push notification is delivered to an iOS application on a given device are the push certificate used to establish the connection with APNS (which determines the destination application) and the device token (which determines the destination device). Any other parameters you choose to include in your payload will have no effect on the delivery of the message.
The solution that we implemented (and it works very well), is:
have the device token sent to the server upon registration
server stores that in a 'user_device' table (which ties the user to the device - once you get the info.... at this time you only have the token, so the userId is 0)
the user continues with their login info.....
as the user completes their login (going to the server for verification), include the token and match that up in the database (note that you could also just send it at this point, but we like to send the token in #2 so we could see how many people register devices but not log in...)
Now, you have a match-up from the user data to their deviceId and can then match up various USER properties (or even the device properties if you have gotten them from other means - like when the user signed in and completed their profile) and send the appropriate push notice.