how to send content-available = 1 from javapns

1.4k Views Asked by At

I am using java client to send notifications using javapns. But now I need to notify the client application about the new notification using "content-available": 1, so that the app can raise a flag of content available at its end.

So far I have been using a similar code to one below,

 PushNotificationPayload payload = PushNotificationPayload.complex();

    payload.addAlert(apnsUser.getPushMessage());
    payload.addSound("default");
    payload.addCustomDictionary("someKey", someValue);

    List<ListNotification> notifications = Push.payload(payload, somekeyStore, somepassword, isproduction, threads, devices);

But the payload class does not have a place holder for "content-available": 1. I also checked for

Push.contentAvailable(keystore, vKeyStoreName, production, devices) 

but it does not allow setting custom message value "someKey".

Please suggest a way to send "content-available": 1 while triggering notification.

2

There are 2 best solutions below

0
On BEST ANSWER

Sorry for posting my answer so late, i figured it out back then. Its quite simple.

  JSONObject vSomeDictionary = new JSONObject();
  vSomeDictionary.put("content-available", 1);
  vSomeDictionary.put("alert", "SomeMessage");
  vSomeDictionary.put("sound", "default");
  JSONObject vJPayload = new JSONObject();
  vJPayload.put("aps", vSomeDictionary);

This way you can set the content-available, by creating two different Jso objects and fitting one into another as "aps".

0
On
PayloadBuilder payloadBuilder = APNS.newPayload()
                .alertBody(message)
                .sound(sound)
                .actionKey(actionLocKey)
                .localizedKey(locKey)
                .localizedArguments(locArgs)
                .badge(badge)
                .customFields(customData)
                .category(category);
        if (contentAvailable!=null && contentAvailable.intValue()==1){
            payloadBuilder.instantDeliveryOrSilentNotification();
        }
        payload =payloadBuilder.build();