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