How to manage iOS apns token changes

2019-03-19 09:59发布

问题:

I had an issue where a user started receiving double notifications after uninstalling and reinstalling my app, as the device sent to my server 2 different APNS tokens - one from the first installation and the other after reinstalling the app. Since the tokens were different I could not know this is the same device.

Until iOS 9 came out, every time I uninstalled and reinstalled the app, I always got the same APNS, so it was easy to know that this is the same device the user used as before. Since iOS 9, it seems that the APNS token is changed on every installation.

My question is how to tell if a client uninstall and reinstall the app, and update his APNS token instead of adding a new token?

I am asking as this sounds to me like something most iOS developers had to handle, but I couldn't find any best practice from Apple on how to tackle this, so I hoped that other can share their experience with this issue.

回答1:

This is how we ended up solving it:

On each app launch:

  1. create push notification token
  2. is there a token in localstorage?
    • yes - compare the tokens to localstorage. are they identical?
      • yes - return
      • no - update server with new token, and after server response with 'OK' save it locally in local storage and keychain
    • no - check is there a token in keychain
      • yes - compare the tokens to keychain. are they identical?
        • yes - save token in localstorage and return
        • no - update server with new token, and after server response with 'OK' save it locally in local
          storage and keychain
    • no - update server with new token, and after server response with 'OK' save it locally in local storage and keychain

If anyone has a more elegant way to solve it I would love to hear about it