在iOS Swift中使用PushKit令牌进行PubNub推送注册

问题描述

我正在一个iOS swift项目中,我需要使用WebRTC和PubNub集成视频通话和聊天,我正在使用PubNubSwift','〜> 3.0.1 SDK for PubNub。我已将视频通话和聊天功能集成为前台状态。但是我想在后台或未命中状态下接到电话时通知用户。对于推送通知,后端团队正在使用pubnub。因此,在PubNub管理面板中,我启用了移动移动推送通知,并添加了TeamID,AuthKeyID并上传了Auth Key令牌(.p8文件)。请参见下图。

enter image description here

然后在我的代码中,在AppDelegate中,我注册了PushKit通知。并运行该应用程序后,我将在以下位置获取pushKit令牌:
以下方法。 func pushRegistry(_注册表:PKPushRegistry,didUpdate凭据:PKPushCredentials,类型:PKPushType){

} 

一旦获得令牌,我正在做的是,我使用pushKit令牌将设备和通道名称注册到pubnub中。我的代码是,

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)")
}

当在Pubnub中注册pushKit令牌时,我得到了成功响应。之后,我只是打印与相同令牌关联的频道名称(仅用于测试),并且在成功响应中,我会收到我的频道名称。这样就可以了。但是,当我尝试从PubNub调试控制台发送示例有效负载时,在以下方法中我没有收到任何回调,

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

}

但是当我使用此查询在终端中尝试时, curl -v -d'{“ aps”:{“ alert”:“ hello”}}'--http2 --cert VOIP.pem:Qinnovation1 https://api.development.push.apple.com/3/device/2923af68bf211ca1acf7d1b8dccd83432e8f5eaf30fdec65515a96db908f0c83,我在上述方法中得到了回调。但是,为什么我没有收到来自PubNub的任何回调?我的样本有效载荷是

{
   "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
      }
   }
}

在调试控制台中发送消息时,响应为

enter image description here

但是我没有收到任何回叫。请帮助我。

解决方法

我认为您不需要对设备令牌执行任何String编码,并且可以直接直接将credentials.token传递给PubNub和UserDefaults。

对于常规的远程通知调试,您可以通读相关的technical note。我发现在macOS上使用控制台应用程序,对应用程序的Bundle ID进行过滤,有助于查看是否存在与Push相关的有用系统日志。

特别是对于iOS 13+上的PushKit,请确保您正在呼叫reportNewIncomingCall(with:update:completion:)来通知CallKit已收到来电。参见pushRegistry(_:didReceiveIncomingPushWith:for:completion:)

如果这不能解决问题,您可以与PubNub support联系,他们可以提供其他帮助。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...