0

XCode 7, Swift 2. (don't even start on me about that, it's not my favorite set of constraints either)

So I'm sending data using node-apn (BIG thanks to those folks!). I'm using the (pushkit option) *.voip topic and I got all that working. I can see the notification is received on my device (big shout out to libimobiledevice).

In composing the note on my server i'm doing

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

How am I supposed to get at my payload object? Following some (3rd party) pushkit examples (maybe it was 1? that showed like

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

I tried to mimic it like

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

or even

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

but those don't work (I get nil).

How am I supposed to do this?

Michael
  • 2,973
  • 1
  • 27
  • 67
  • For some reason using NSDictionary works for me, but I don't know why that should be necessary? if let myPayload = payloadDict["payload"] as? NSDictionary { ... – Michael Mar 21 '18 at 21:33

2 Answers2

1

Here's how I log it in Obj-C:

NSLog(@"pushRegistry:didReceiveIncomingPushWithPayload:forType:withCompletionHandler:%@", 
      payload.dictionaryPayload);
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
buckleyJohnson
  • 459
  • 4
  • 12
0

Let me try to make this clear by explaining the relation between PKPushPayload and its NSDictionary property with the data you are looking for.

For the func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!) you have the payload argument which is a PKPushPayload.

The data you are looking for are stored in the _dictionaryPayload which is a NSDictionary and it is a property of the payload argument.

So to access the data you can do:

let payloadDictionary = payload.dictionaryPayload as NSDictionary
let myData = payloadDictionary.value(forKey: "myData_unique_key")
C. Kontos
  • 1,198
  • 12
  • 21