Search code examples
iosswiftapple-push-notificationscallkitpushkit

IOS VoiP push doesn't work with APNS push


I have a project which use VoiP pushes for calling and APNS pushes for simple notifications. APNS push comes, but VoiP doesn't come. When server tries to send me VoiP push by my VoiP token server throws exception "Invalid Token". Please, see my solution and let me know what I do worst.

I created two certificates (APNS and VoiP)

I added certificate to identify, but I can add just APNS

Next, I generated p12 keys and send to server for using them.

In UIApplicationDelegate I retrieve APNS token and send to server

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
    
    //next send APNS token
}

APNS token I receive successful here

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)

VoiP push notification I register at first

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

Here I receive VoiP push token

    public func pushRegistry(_ registry: PKPushRegistry, didUpdate credentials: PKPushCredentials, for type: PKPushType) {
    let token:String? = credentials.token.map { String(format: "%02x", $0) }.joined()
    //send VoiP token to server
}

By documentation sending VoiP push I must to receive here

    func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void)

But when server sends VoiP push it gets error Invalid Token. What do I worst?


Solution

    • Firstly, you should use p8 authKey to send push notifications

    • Secondly, PKPushRegistry is stored in the func? This is strange. Move it to field of your class

    • Thirdly, try converting the token from data by the following code:

        class TokenConverter {
      
            static func token(from data: Data) -> String {
      
                let tokenParts = data.map { data -> String in
                    return String(format: "%02.2hhx", data)
                }
      
                return tokenParts.joined()
            }
        }