Search code examples
iosswiftpushkit

Force change iOS PKPushRegistry token for VOIP Type


in AppDelegate didFinishLaunch i call

func pushRegistrySetup() {
        let pushRegistry = PKPushRegistry(queue: DispatchQueue.main)
        pushRegistry.delegate = self
        pushRegistry.desiredPushTypes = [.voIP]
    }

Then i listen to token

func pushRegistry(_ registry: PKPushRegistry, didUpdate credentials: PKPushCredentials, for type: PKPushType) {
        
        let token = credentials.token.reduce("", {$0 + String(format: "%02X", $1)})
        print("callToken: \(token)")
    }

The problem is that i receive the same token although i have remove app and reinstall also try to unsubscribe from VOIP the resubscribe.


Solution

  • You cannot force a change of the push token. The token is for your app on a given device. The token can change, so your app should always be prepared to receive a new token via the delegate function.

    Push tokens are globally unique, so you will never have the same token value for two different devices.

    It is up to your app to work with your backend server to manage token association to user accounts:

    • When a user logs in to your app you should associate the token with that user in your back end. If the same token is associated with any other user, remove that association
    • When a user logs out from your app you should remove the association between the push token and that user in your back end.
    • If a user simply deletes the app you will not have an opportunity to update your back end directly, so you need to listen to the response you get from the APNS service that will eventually inform you that the push token is no longer valid.

    If a user re-installs the app on a device, then they may well receive the same token, as you have found.

    You could do nothing. When the user logs in, your back end will associate the token with the logged in user, and remove it from any other user that may be associated with it, as described in the first bullet point.

    A problem with this is that if the user does not log in straight away, the device may receive notifications for the user that was previously associated with this device without logging in.

    A better solution is that if your app launches and no user is logged in, it informs your back end that the push token is no longer associated with any user. That way no notifications will be sent to the device until a user has logged in.