PKPushRegistryDelegate never called

Viewed 1009

I got many results when I tried with PKPushRegistryDelegate not called but nothing solved my issue. All are suggesting to check info.plist for background modes which is fine in my app. I enabled voip, background fetch, Remote notifications. Create VoIP certificate in apple developer account also. I wrote following lines in didFinishLanunchingWithOptions

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

And delegate methods as below

func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
    if let token = String(bytes: pushCredentials.token, encoding: String.Encoding.utf8) {
        print("Push token: \(token)")
    }
}
func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) {
    print("notification received")
    print(payload)
}

func pushRegistry(_ registry: PKPushRegistry, didInvalidatePushTokenFor type: PKPushType) {
    print("Token invalid")
}

But those delegate methods are never called. As VoIP certificate is only on production, I tried with production certificate and checked logs for push token but not found. Is there a way to debug it with development certificates? Am I missing anything here? Please advice.

1 Answers

I faced the same issue in iPhone 6 with version 11.4. Tried with a different device iPhone 6+ and it worked normally. This might be an issue in some of the devices, but this is not mentioned anywhere in Apple docs.

Please use the following code in didUpdate delegate method:

func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
        let pushToken = pushCredentials.token.map { String(format: "%02.2hhx", $0) }.joined()
        print("voip token: \(pushToken)")
    }
Related