javapns.notification
Class PushNotificationPayload

java.lang.Object
  extended by javapns.notification.Payload
      extended by javapns.notification.PushNotificationPayload

public class PushNotificationPayload
extends Payload

A payload compatible with the Apple Push Notification Service.

Author:
Maxime Peron, Sylvain Pedneault

Constructor Summary
PushNotificationPayload()
          Create a default payload with a blank "aps" dictionary.
PushNotificationPayload(String rawJSON)
          Construct a Payload object from a JSON-formatted string.
PushNotificationPayload(String alert, int badge, String sound)
          Create a payload and immediately add an alert message, a badge and a sound.
 
Method Summary
 void addAlert(String alertMessage)
          Add a simple alert message.
 void addBadge(int badge)
          Add a badge.
 void addCategory(String category)
          Add a category.
 void addCustomAlertActionLocKey(String actionLocKey)
          Create a custom alert (if none exist) and add a custom text for the right button of the popup.
 void addCustomAlertBody(String body)
          Create a custom alert (if none exist) and add a body to the custom alert.
 void addCustomAlertLaunchImage(String launchImage)
          Create a custom alert (if none exist) and add a launch-image parameter.
 void addCustomAlertLocArgs(List args)
          Create a custom alert (if none exist) and add sub-parameters for the loc-key parameter.
 void addCustomAlertLocKey(String locKey)
          Create a custom alert (if none exist) and add a loc-key parameter.
 void addSound(String sound)
          Add a sound.
static PushNotificationPayload alert(String message)
          Create a pre-defined payload with a simple alert message.
static PushNotificationPayload badge(int badge)
          Create a pre-defined payload with a badge.
static PushNotificationPayload combined(String message, int badge, String sound)
          Create a pre-defined payload with a simple alert message, a badge and a sound.
static PushNotificationPayload complex()
          Create an empty payload which you can configure later.
static PushNotificationPayload fromJSON(String rawJSON)
          Create a PushNotificationPayload object from a preformatted JSON payload.
 int getMaximumPayloadSize()
          Return the maximum payload size in bytes.
static PushNotificationPayload sound(String sound)
          Create a pre-defined payload with a sound name.
static PushNotificationPayload test()
          Create a special payload with a useful debugging alert message.
 
Methods inherited from class javapns.notification.Payload
addCustomDictionary, addCustomDictionary, addCustomDictionary, asSimulationOnly, estimatePayloadSizeAfterAdding, getCharacterEncoding, getExpiry, getPayload, getPayloadAsBytes, getPayloadSize, isEstimatedPayloadSizeAllowedAfterAdding, isPayloadSizeEstimatedWhenAdding, setCharacterEncoding, setExpiry, setPayloadSizeEstimatedWhenAdding, toString
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

PushNotificationPayload

public PushNotificationPayload()
Create a default payload with a blank "aps" dictionary.


PushNotificationPayload

public PushNotificationPayload(String rawJSON)
                        throws JSONException
Construct a Payload object from a JSON-formatted string. If an aps dictionary is not included, one will be created automatically.

Parameters:
rawJSON - a JSON-formatted string (ex: {"aps":{"alert":"Hello World!"}} )
Throws:
JSONException - thrown if a exception occurs while parsing the JSON string

PushNotificationPayload

public PushNotificationPayload(String alert,
                               int badge,
                               String sound)
                        throws JSONException
Create a payload and immediately add an alert message, a badge and a sound.

Parameters:
alert - the alert message
badge - the badge
sound - the name of the sound
Throws:
JSONException
Method Detail

alert

public static PushNotificationPayload alert(String message)
Create a pre-defined payload with a simple alert message.

Parameters:
message - the alert's message
Returns:
a ready-to-send payload

badge

public static PushNotificationPayload badge(int badge)
Create a pre-defined payload with a badge.

Parameters:
badge - the badge
Returns:
a ready-to-send payload

sound

public static PushNotificationPayload sound(String sound)
Create a pre-defined payload with a sound name.

Parameters:
sound - the name of the sound
Returns:
a ready-to-send payload

combined

public static PushNotificationPayload combined(String message,
                                               int badge,
                                               String sound)
Create a pre-defined payload with a simple alert message, a badge and a sound.

Parameters:
message - the alert message
badge - the badge
sound - the name of the sound
Returns:
a ready-to-send payload

test

public static PushNotificationPayload test()
Create a special payload with a useful debugging alert message.

Returns:
a ready-to-send payload

complex

public static PushNotificationPayload complex()
Create an empty payload which you can configure later. This method is usually used to create complex or custom payloads. Note: the payload actually contains the default "aps" dictionary required by APNS.

Returns:
a blank payload that can be customized

fromJSON

public static PushNotificationPayload fromJSON(String rawJSON)
                                        throws JSONException
Create a PushNotificationPayload object from a preformatted JSON payload.

Parameters:
rawJSON - a JSON-formatted string representing a payload (ex: {"aps":{"alert":"Hello World!"}} )
Returns:
a ready-to-send payload
Throws:
JSONException - if any exception occurs parsing the JSON string

addBadge

public void addBadge(int badge)
              throws JSONException
Add a badge.

Parameters:
badge - a badge number
Throws:
JSONException

addSound

public void addSound(String sound)
              throws JSONException
Add a sound.

Parameters:
sound - the name of a sound
Throws:
JSONException

addAlert

public void addAlert(String alertMessage)
              throws JSONException
Add a simple alert message. Note: you cannot add a simple and a custom alert in the same payload.

Parameters:
alertMessage - the alert's message
Throws:
JSONException

addCategory

public void addCategory(String category)
                 throws JSONException
Add a category.

Parameters:
alertMessage - the category
Throws:
JSONException

addCustomAlertBody

public void addCustomAlertBody(String body)
                        throws JSONException
Create a custom alert (if none exist) and add a body to the custom alert.

Parameters:
body - the body of the alert
Throws:
JSONException - if the custom alert cannot be added because a simple alert already exists

addCustomAlertActionLocKey

public void addCustomAlertActionLocKey(String actionLocKey)
                                throws JSONException
Create a custom alert (if none exist) and add a custom text for the right button of the popup.

Parameters:
actionLocKey - the title of the alert's right button, or null to remove the button
Throws:
JSONException - if the custom alert cannot be added because a simple alert already exists

addCustomAlertLocKey

public void addCustomAlertLocKey(String locKey)
                          throws JSONException
Create a custom alert (if none exist) and add a loc-key parameter.

Parameters:
locKey -
Throws:
JSONException - if the custom alert cannot be added because a simple alert already exists

addCustomAlertLocArgs

public void addCustomAlertLocArgs(List args)
                           throws JSONException
Create a custom alert (if none exist) and add sub-parameters for the loc-key parameter.

Parameters:
args -
Throws:
JSONException - if the custom alert cannot be added because a simple alert already exists

addCustomAlertLaunchImage

public void addCustomAlertLaunchImage(String launchImage)
                               throws JSONException
Create a custom alert (if none exist) and add a launch-image parameter.

Parameters:
launchImage -
Throws:
JSONException - if the custom alert cannot be added because a simple alert already exists

getMaximumPayloadSize

public int getMaximumPayloadSize()
Return the maximum payload size in bytes. For APNS payloads, this method returns 256.

Overrides:
getMaximumPayloadSize in class Payload
Returns:
the maximum payload size in bytes (256)