Michael
Michael

Reputation: 2993

How do I access my data in a PKPushPayload?

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?

Upvotes: 0

Views: 2155

Answers (2)

C. Kontos
C. Kontos

Reputation: 1258

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

Upvotes: 0

buckleyJohnson
buckleyJohnson

Reputation: 489

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

NSLog(@"pushRegistry:didReceiveIncomingPushWithPayload:forType:withCompletionHandler:%@", 
      payload.dictionaryPayload);

Upvotes: 1

Related Questions