5

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?

TejAces
  • 551
  • 5
  • 11
  • If your app use user authentication then you store on server on authentication and when user logout then you remove from server behalf of any unique ID. if its not then explan your app functionality – Sharda Prasad Jul 12 '17 at 17:15
  • That makes sense for a logout, but what if user deletes the app? How do we remove the device token then? – TejAces Jul 12 '17 at 21:31
  • 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. – Ayush Jul 13 '17 at 05:01
  • @Ayush is right – Sharda Prasad Jul 13 '17 at 07:42

5 Answers5

2

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?

TejAces
  • 551
  • 5
  • 11
1

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.

user1984795
  • 167
  • 1
  • 9
  • I don't think there is any feedback service for enhanced APNS (http/2) interface. – Ayush Jul 13 '17 at 10:56
  • @Ayush, In that case you can still use the http status code if using HTTP APNS interface to find out the tokens that are no longer active. HTTP status code of 410 indicates that the token is no longer active. See [this apple documentation](https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html#//apple_ref/doc/uid/TP40008194-CH11-SW1) – user1984795 Jul 13 '17 at 19:09
  • Are you sure that - in case user forcefully removes the app, Apple will reject saying 410 & will delete the device token? I have seen a case, where app forcefully removed & installed back, same device token is allocated again to app. I think those notifications will be delivered to iOS & iOS will drop it in case app is forcefully removed. How will apple cloud detect that app is removed? I think when Apple will try to deliver notification to client which has deleted the app, that time it might detect & invalidate the token. Is that the case if you can confirm? – Ayush Jul 14 '17 at 11:51
  • The feedback service is under the legacy section. Is it still used? – TejAces Aug 04 '17 at 18:16
0

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

vivek bhoraniya
  • 1,526
  • 2
  • 17
  • 36
0

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

Maulik Pandya
  • 2,200
  • 17
  • 26
0

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.

Ayush
  • 390
  • 3
  • 11