com.notnoop.apns
Class PayloadBuilder

java.lang.Object
  extended by com.notnoop.apns.PayloadBuilder

public final class PayloadBuilder
extends Object

Represents a builder for constructing Payload requests, as specified by Apple Push Notification Programming Guide.


Method Summary
 PayloadBuilder actionKey(String actionKey)
          Sets the value of action button (the right button to be displayed).
 PayloadBuilder alertBody(String alert)
          Sets the alert body text, the text the appears to the user, to the passed value
 PayloadBuilder badge(int badge)
          Sets the notification badge to be displayed next to the application icon.
 String build()
          Returns the JSON String representation of the payload according to Apple APNS specification
 byte[] buildBytes()
          Returns the bytes representation of the payload according to Apple APNS specification
 PayloadBuilder clearBadge()
          Requests clearing of the badge number next to the application icon.
 PayloadBuilder copy()
          Returns a copy of this builder
 PayloadBuilder customField(String key, Object value)
          Sets any application-specific custom fields.
 PayloadBuilder customFields(Map<String,? extends Object> values)
          Set any application-specific custom fields.
 PayloadBuilder forNewsstand()
          Sets the notification type to be a 'newstand' notification.
 boolean isTooLong()
          Returns true if the payload built so far is larger than the size permitted by Apple (which is 256 bytes).
 PayloadBuilder launchImage(String launchImage)
          Sets the launch image file for the push notification
 int length()
          Returns the length of payload bytes once marshaled to bytes
 PayloadBuilder localizedArguments(Collection<String> arguments)
          Sets the arguments for the alert message localizable message.
 PayloadBuilder localizedArguments(String... arguments)
          Sets the arguments for the alert message localizable message.
 PayloadBuilder localizedKey(String key)
          Set the notification localized key for the alert body message.
 PayloadBuilder mdm(String s)
           
static PayloadBuilder newPayload()
           
 PayloadBuilder noActionButton()
          Set the notification view to display an action button.
 PayloadBuilder resizeAlertBody(int payloadLength)
          Shrinks the alert message body so that the resulting payload message fits within the passed expected payload length.
 PayloadBuilder resizeAlertBody(int payloadLength, String postfix)
          Shrinks the alert message body so that the resulting payload message fits within the passed expected payload length.
 PayloadBuilder shrinkBody()
          Shrinks the alert message body so that the resulting payload message fits within require Apple specification (256 bytes).
 PayloadBuilder shrinkBody(String postfix)
          Shrinks the alert message body so that the resulting payload message fits within require Apple specification (256 bytes).
 PayloadBuilder sound(String sound)
          Sets the alert sound to be played.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

alertBody

public PayloadBuilder alertBody(String alert)
Sets the alert body text, the text the appears to the user, to the passed value

Parameters:
alert - the text to appear to the user
Returns:
this

sound

public PayloadBuilder sound(String sound)
Sets the alert sound to be played. Passing null disables the notification sound.

Parameters:
sound - the file name or song name to be played when receiving the notification
Returns:
this

badge

public PayloadBuilder badge(int badge)
Sets the notification badge to be displayed next to the application icon. The passed value is the value that should be displayed (it will be added to the previous badge number), and a badge of 0 clears the badge indicator.

Parameters:
badge - the badge number to be displayed
Returns:
this

clearBadge

public PayloadBuilder clearBadge()
Requests clearing of the badge number next to the application icon. This is an alias to badge(0).

Returns:
this

actionKey

public PayloadBuilder actionKey(String actionKey)
Sets the value of action button (the right button to be displayed). The default value is "View". The value can be either the simple String to be displayed or a localizable key, and the iPhone will show the appropriate localized message. A null actionKey indicates no additional button is displayed, just the Cancel button.

Parameters:
actionKey - the title of the additional button
Returns:
this

noActionButton

public PayloadBuilder noActionButton()
Set the notification view to display an action button. This is an alias to actionKey(null)

Returns:
this

forNewsstand

public PayloadBuilder forNewsstand()
Sets the notification type to be a 'newstand' notification. A Newstand Notification targets the Newstands app so that the app updates the subscription info and content.

Returns:
this

localizedKey

public PayloadBuilder localizedKey(String key)
Set the notification localized key for the alert body message.

Parameters:
key - the localizable message body key
Returns:
this

localizedArguments

public PayloadBuilder localizedArguments(Collection<String> arguments)
Sets the arguments for the alert message localizable message. The iPhone doesn't localize the arguments.

Parameters:
arguments - the arguments to the localized alert message
Returns:
this

localizedArguments

public PayloadBuilder localizedArguments(String... arguments)
Sets the arguments for the alert message localizable message. The iPhone doesn't localize the arguments.

Parameters:
arguments - the arguments to the localized alert message
Returns:
this

launchImage

public PayloadBuilder launchImage(String launchImage)
Sets the launch image file for the push notification

Parameters:
launchImage - the filename of the image file in the application bundle.
Returns:
this

customField

public PayloadBuilder customField(String key,
                                  Object value)
Sets any application-specific custom fields. The values are presented to the application and the iPhone doesn't display them automatically. This can be used to pass specific values (urls, ids, etc) to the application in addition to the notification message itself.

Parameters:
key - the custom field name
value - the custom field value
Returns:
this

mdm

public PayloadBuilder mdm(String s)

customFields

public PayloadBuilder customFields(Map<String,? extends Object> values)
Set any application-specific custom fields. These values are presented to the application and the iPhone doesn't display them automatically. This method *adds* the custom fields in the map to the payload, and subsequent calls add but doesn't reset the custom fields.

Parameters:
map - the custom map
Returns:
this

length

public int length()
Returns the length of payload bytes once marshaled to bytes

Returns:
the length of the payload

isTooLong

public boolean isTooLong()
Returns true if the payload built so far is larger than the size permitted by Apple (which is 256 bytes).

Returns:
true if the result payload is too long

resizeAlertBody

public PayloadBuilder resizeAlertBody(int payloadLength)
Shrinks the alert message body so that the resulting payload message fits within the passed expected payload length. This method performs best-effort approach, and its behavior is unspecified when handling alerts where the payload without body is already longer than the permitted size, or if the break occurs within word.

Parameters:
payloadLength - the expected max size of the payload
Returns:
this

resizeAlertBody

public PayloadBuilder resizeAlertBody(int payloadLength,
                                      String postfix)
Shrinks the alert message body so that the resulting payload message fits within the passed expected payload length. This method performs best-effort approach, and its behavior is unspecified when handling alerts where the payload without body is already longer than the permitted size, or if the break occurs within word.

Parameters:
payloadLength - the expected max size of the payload
postfix - for the truncated body, e.g. "..."
Returns:
this

shrinkBody

public PayloadBuilder shrinkBody()
Shrinks the alert message body so that the resulting payload message fits within require Apple specification (256 bytes). This method performs best-effort approach, and its behavior is unspecified when handling alerts where the payload without body is already longer than the permitted size, or if the break occurs within word.

Returns:
this

shrinkBody

public PayloadBuilder shrinkBody(String postfix)
Shrinks the alert message body so that the resulting payload message fits within require Apple specification (256 bytes). This method performs best-effort approach, and its behavior is unspecified when handling alerts where the payload without body is already longer than the permitted size, or if the break occurs within word.

Parameters:
postfix - for the truncated body, e.g. "..."
Returns:
this

build

public String build()
Returns the JSON String representation of the payload according to Apple APNS specification

Returns:
the String representation as expected by Apple

buildBytes

public byte[] buildBytes()
Returns the bytes representation of the payload according to Apple APNS specification

Returns:
the bytes as expected by Apple

toString

public String toString()
Overrides:
toString in class Object

copy

public PayloadBuilder copy()
Returns a copy of this builder

Returns:
a copy of this builder

newPayload

public static PayloadBuilder newPayload()
Returns:
a new instance of Payload Builder


Copyright © 2013. All Rights Reserved.