0

XCode 7, Swift 2. (甚至不要从我这里开始,这也不是我最喜欢的一组约束)

所以我正在使用node-apn发送数据(非常感谢那些人!)。我正在使用(pushkit 选项)*.voip主题,我得到了所有的工作。我可以看到我的设备上收到了通知(对libimobiledevice 大喊大叫)。

在我的服务器上撰写笔记时,我正在做

var note = new apn.Notification();
note.topic = 'mine.voip';
note.payload = {
    message: 'text',
    somethingElse: 'this other one '
    payload: {
        k1: v1,
        k2: {
          k3: v2
        }        
    }
};

我应该如何获取我的有效负载对象?遵循一些(第 3 方)pushkit 示例(可能是1

func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!) {
    let payloadDict = payload.dictionaryPayload["aps"] as? Dictionary<String, String>
    let message = payloadDict?["alert"]
}

我试着模仿它

let myWeirdPayload = payload.dictionaryPayload["payload"] as? Dictionary<String, String>

甚至

let myWeirdPayload = payload.dictionaryPayload["payload"] as? Dictionary<String, Any>

但那些不起作用(我明白了nil)。

我该怎么做?

4

2 回答 2

1

这是我在 Obj-C 中记录它的方式:

NSLog(@"pushRegistry:didReceiveIncomingPushWithPayload:forType:withCompletionHandler:%@", 
      payload.dictionaryPayload);
于 2018-10-11T21:22:48.263 回答
0

让我通过解释您要查找的数据PKPushPayload及其属性之间的关系来尝试说明这一点。NSDictionary

对于func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!)你的payload论点是 a PKPushPayload

您要查找的数据存储在_dictionaryPayloadwhich is a中NSDictionary,它是payload参数的一个属性。

因此,要访问数据,您可以执行以下操作:

let payloadDictionary = payload.dictionaryPayload as NSDictionary
let myData = payloadDictionary.value(forKey: "myData_unique_key")
于 2022-01-31T14:58:31.223 回答