Is there a way to force your iOS push notification deviceToken to change?
Asked Answered
V

3

6

Apples docs are a bit vague on when a device's deviceToken can change. It pays to code defensively, and to test that code.

I've written my app so that at startup it registers for push notifications and handles the didRegisterForRemoteNotificationsWithDeviceToken callback.

My didRegisterForRemoteNotificationsWithDeviceToken saves the last device token to user defaults, and if the token is still the same, it moves on. If it doesn't have a saved token at all, or if the tokens don't match, it uploads the new token our server.

I also have a mechanism that lets me name my test devices with human-readable names like "4s_1" "5s_1" "5s_2", etc. I have a server command line tool that takes the human-readable device name, looks up the device's 'identifierForVendor' from that, and then looks up the device token using the identifier. It then triggers a sandbox notification.

It seems that deleting the app and reinstalling it causes the 'identifierForVendor' to change, but not the deviceToken, which is exactly the opposite of what I'd like to have happen for testing. I want some value that never changes to uniquely identify a test device, and away to change the device token so I can test my code.

EDIT: Since writing this I've changed the app to generate it's own UUID and save that to the keychain as suggested by Wain in his answer.

Is there a way to force Apple's APNs to change a device's token so I can test out my code (both client side and server side) for handling the case where the token changes?

Volta answered 16/11, 2015 at 15:4 Comment(0)
C
3

I don't believe so.

If you want a single unique identifier then you should create one explicitly and store it in the keychain, then you can store the device name and all other details against this so you have a single point of truth.

Any identifier you stored in the keychain would survive until it was explicitly removed or the device was restored from a backup without that keychain content.

Counterbalance answered 16/11, 2015 at 15:25 Comment(3)
I was thinking of going the keychain route. Keychain entries definitely survive deleting all apps from a given developer account?Volta
Deleting from a developer account? Keychain survives app deletion from the device.Counterbalance
Cool. I was concerned that app keychain entries were like shared user defaults entries, and got purged when the last app from a developer account was deleted, but they do persist. Thanks for the suggestion.Volta
K
3

I'm not sure if this approach also resets the token, but you may try to reset the permission:

Resetting the Push Notifications Permissions Alert on iOS The first time a push-enabled app registers for push notifications, iOS asks the user if they wish to receive notifications for that app. Once the user has responded to this alert it is not presented again unless the device is restored or the app has been uninstalled for at least a day.

If you want to simulate a first-time run of your app, you can leave the app uninstalled for a day. You can achieve the latter without actually waiting a day by following these steps:

Delete your app from the device. Turn the device off completely and turn it back on. Go to Settings > General > Date & Time and set the date ahead a day or more. Turn the device off completely again and turn it back on.

Regarding the change of device identifiers: You could install a second application on your test devices (a blank one from the same issuer). If you reinstall your actual app, the identifierForVendor shouldn't change.

Kinase answered 16/11, 2015 at 15:26 Comment(2)
I just tried the steps you outline and it resets the push notification permission flag so that you get prompted ("This apps wants to send you push notifications. OK?") again, but it does not change the deviceToken. :(Volta
Too bad. Thanks anyway for giving it a try.Kinase
B
1

Client side you could test be changing the stored device token.

Terminate App in device

  • Download (xcode devices window) the Container.
  • Find default in Library/Perferences/ app bundle plist.
  • Edit entry in plist.
  • Replace Container (xcode devices window)

Important: I found a restart of the device was required for the changes to take effect.

Then on restart your old device token will be incorrect and what ever action is required can be done.

I used this when debugging an adhoc app I did not have src code for. But, if you have src code might be easier to just make the compare of deviceTokens fail.

Blindly answered 3/10, 2018 at 14:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.