According to this forum post Does the APNS device token ever change, once created? The device token might be expire or APNS might change the device token. My question is that whether the APNS will use the expired token for notification if the server send this expired token to Apple? Can APNS use this expired token for another device?
1 Answers
I've never encountered an expired device token, so I can't tell you from my personal experience. Nor can I tell you from Apple's APNS documentation, because they don't answer your question (and I read all their APNS docs more than once).
Your app and your server should be able to handle device token expiration regardless to what the answer to your question is.
Always call
registerForRemoteNotificationTypes
when your app is launched, and send the device token to the server if it's different than the last device token your app got on that device.Assign in your server another unique identifier for each device that uses your app. Have the app send that identifier to your server along with the device token. This way, if the device token changes, your server will know it's a new device token for an existing device and not a new device where your app was installed.
Following #1 and #2 will ensure that your server will have the current device token for each device on which your app was launched recently (on devices where your app wasn't launched recently, the users probably don't care much about your app, so I'm not sure that sending them push notifications will make any difference).
If you do send a notification to an old device token, if it works, all is good. If it doesn't, you'd either get an Invalid Token error response or you'd get that device token in the Feedback service. In either of those two cases, you should stop sending notifications to that token.
I assume APNS won't reuse an expired token for another device, but if it does, your server can identify it by using the unique identifier I suggested in #2. In that case, make sure your server assigns the device token only to the other device.

- 387,369
- 54
- 702
- 768
-
4The problem is that how this unique identifier maintained? If user install/uninstall the app this unique identifier will be lost. If OS is updated this unique identifier(and the device token, acc to above link) will be lost. Is there is any unique key in the device which will never change and remain unique for this device? – Imran Qadir Baksh - Baloch Nov 05 '13 at 15:14
-
@user960567 The unique identifier should be stored in some external storage that doesn't get wiped when the app is uninstalled and can be re-used when it's re-installed (I know it's possible on Android, not sure about iOS though). I don't think Apple allows you to use the unique key of the device. – Eran Nov 05 '13 at 15:59
-
How to do this in Android? – Imran Qadir Baksh - Baloch Nov 05 '13 at 16:57
-
1@user960567 On Android you can create files in the external storage if your app has the `android.permission.WRITE_EXTERNAL_STORAGE` permission. You can create folders and files under `Environment.getExternalStorageDirectory().getAbsolutePath()`. – Eran Nov 05 '13 at 17:07
-
What if I have multiple accounts for the same app? So in my server, I have to a primary key using user_id, device_id, and device_token, right? Also how will the server clean up old tokens? @Eran – Olla Ashour Dec 07 '15 at 14:18