iosswiftpubnub

PubNub Push Registration with PushKit token in iOS swift


I'm working on an iOS swift project in which I need to integrate video call and chat using WebRTC and PubNub, I'm using PubNubSwift SDK version 3.0.1 for PubNub.

I have integrated the video call and chat in foreground state. But I want to notify users when receiving a call in background or killed state. And for push notification, the backend team is using PubNub. So In PubNub admin panel, I enabled the mobile Mobile Push Notifications, and I added TeamID, AuthKeyID and uploaded the Auth Key token(.p8 file). See the following image.

enter image description here

And In my code, in AppDelegate, I register for PushKit notification. And once run the application, I will get the pushKit token in the following method.

func pushRegistry(_ registry: PKPushRegistry, didUpdate credentials: PKPushCredentials, for type: PKPushType) { 

} 

And once I got the token, what I'm doing is, I registering my device and channel names into PubNub by using the pushKit token. My code is,

func pushRegistry(_ registry: PKPushRegistry, didUpdate credentials: PKPushCredentials, for type: PKPushType) { 
    print(credentials.token)
    let deviceToken = credentials.token.map { String(format: "%02x", $0) }.joined()
    let dataStr: Data = Data(deviceToken.utf8)
    let stringNS = NSData(data: dataStr)
    
    if let channel = UserDefaults.standard.value(forKey: "pubnubUserName") as? String {
        let chatChannel = "\(channel).chat"
        let callChannel = "\(channel)-stdby"
        
        pubnub.modifyAPNSDevicesOnChannels(
            byRemoving: [],
            thenAdding: [chatChannel, callChannel],
            device: dataStr,
            on: "com.Qinnovation.Connect-Debug",
            environment: .development
        ) { result in
            switch result {
                case let .success(response):
                  print("Successful Push Modification Response: \(response)")

                case let .failure(error):
                  print("Failed Push List Response: \(error.localizedDescription)")
            }
        }
    }
    
    UserDefaults.standard.set(deviceToken, forKey: "PushToken")
    print("pushRegistry -> deviceToken :\(deviceToken)")
}

And when register the pushKit token with PubNub, I'm getting success response. After that I'm just printing the channel names associated with the same token(just for testing), and in success response I'm receiving my channel names. So it is working. But When I try to send a sample payload from the PubNub Debug Console, I'm not receiving any callback in the following method,

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

But when I try in terminal by using this query,

curl -v -d '{"aps":{"alert":"hello"}}' --http2 --cert VOIP.pem:Qinnovation1 https://api.development.push.apple.com/3/device/2923af68bf211ca1acf7d1b8dccd83432e8f5eaf30fdec65515a96db908f0c83

I'm getting callback in the above method. But why I'm not receiving any callbacks from PubNub? My sample payload is:

{
   "pn_apns":{
      "aps":{
         "content-available":1,
         "alert": "hello world"   
      },
      "pn_push":[
         {
            "push_type":"voip",
            "collapse_id":"1312414asdaqr124",
            "expiration":"2020-01-01 00:00:00.00Z",
            "targets":[
               {
                  "environment":"development",
                  "topic":"com.Qinnovation.Connect-Debug"
               }
            ],
            "version":"v2"
         }
      ],
      "custom_data_a":"important_string",
      "custom_data_b":{
         "important_string":true
      }
   }
}

When I send in Debug Console, the response is

enter image description here

But I'm not receiving any callback.


Solution

  • I don't believe you need to be performing any String encoding on the device token, and can directly pass credentials.token directly (to both PubNub and UserDefaults).

    For general Remote Notification debugging you can read through the related technical note. I've found using the Console app on macOS, filtering on your application's Bundle ID, helpful to see if there are any helpful system logs related to Push.

    For PushKit on iOS 13+ specifically, ensure that you're calling the reportNewIncomingCall(with:update:completion:) to inform CallKit that an incoming call was received. See pushRegistry(_:didReceiveIncomingPushWith:for:completion:).

    If this doesn't resolve the issue, you can reach out to PubNub support, and they can provide additional assistance.