fatCop
fatCop

Reputation: 2576

How to add a JSON to payload in JavaPNS push notification?

I want to add a JSON with the notification. I can send alert now. How can I send a JSON with the notification?

Code snippet I'm currently Using:

PushNotificationPayload payload = PushNotificationPayload.complex();
payload.addAlert("Hello!!!");
payload.addBadge(1);
payload.addSound("default");
payload.addCustomDictionary("id", "1");
List<PushedNotification> NOTIFICATIONS = Push.payload(payload, "D:\\ios_dev.p12", "1234", true, "b3ead5d64ba0e08241e236f3ee041d8a9f036b39a0b0537e99a5f8b0607");

for (PushedNotification NOTIFICATION : NOTIFICATIONS) {
  if (NOTIFICATION.isSuccessful()) {
     System.out.println("PUSH NOTIFICATION SENT SUCCESSFULLY TO: " + NOTIFICATION.getDevice().getToken());
  }
}

I want to add JSON like:

{
  "message": message,
  "m_id": mId,
  "callNo": callNo
}

How can I pass this?

Upvotes: 1

Views: 3807

Answers (2)

Giulio Piancastelli
Giulio Piancastelli

Reputation: 15808

I'm very late to the party, but for future readers: you can add an arbitrary JSON object to a PushNotificationPayload by using the addCustomDictionary method that takes an Object as a second parameter. Looking at the source code, it seems that the entire payload is a JSONObject instance, and that the Object passed to addCustomDictionary is directly added to it. So, if you pass a JSONObject instance to addCustomDictionary, it will be properly serialized as a JSON object when the message is sent.

If your custom JSON object is large, remember to use PushNotificationBigPayload.complex() to create a PushNotificationPayload instance instead of PushNotificationPayload.complex().

Upvotes: 0

Nekak Kinich
Nekak Kinich

Reputation: 934

I found the solution for you.

PushNotificationPayload payload=PushNotificationPayload.fromJSON("{\"aps\":{\"content-available\":1,\"sound\":\"\",\"alert\":\"Some alert\"}}");

Only take a String, create the JSON object and pass as String to 'PushNotificationPayload.fromJSON'

It works for me :)

Upvotes: 2

Related Questions