apns-php
 All Data Structures Files Functions Variables Groups Pages
ApnsPHP_Message Class Reference

The Push Notification Message. More...

Inheritance diagram for ApnsPHP_Message:
Inheritance graph
Collaboration diagram for ApnsPHP_Message:
Collaboration graph

Public Member Functions

void __construct (string $sDeviceToken=null)
 Constructor. More...
 
string __toString ()
 PHP Magic Method. More...
 
void addRecipient (string $sDeviceToken)
 Add a recipient device token. More...
 
boolean getAutoAdjustLongPayload ()
 Get the auto-adjust long payload value. More...
 
integer getBadge ()
 Get the number to badge the application icon with. More...
 
string getCategory ()
 Get the category of notification. More...
 
boolean getContentAvailable ()
 Get if should initiates the Newsstand background download. More...
 
mixed getCustomIdentifier ()
 Get the custom message identifier. More...
 
string getCustomProperty (string $sName)
 Get the custom property value. More...
 
string getCustomPropertyName ()
 Get the first custom property name. More...
 
array getCustomPropertyNames ()
 Get all custom properties names. More...
 
mixed getCustomPropertyValue ()
 Get the first custom property value. More...
 
integer getExpiry ()
 Get the expiry value. More...
 
string getPayload ()
 Convert the message in a JSON-encoded payload. More...
 
string getRecipient (integer $nRecipient=0)
 Get a recipient. More...
 
array getRecipients ()
 Get all recipients. More...
 
integer getRecipientsNumber ()
 Get the number of recipients. More...
 
string getSound ()
 Get the sound to play. More...
 
string getText ()
 Get the alert message to display to the user. More...
 
void setAutoAdjustLongPayload (boolean $bAutoAdjust)
 Set the auto-adjust long payload value. More...
 
void setBadge (integer $nBadge)
 Set the number to badge the application icon with. More...
 
void setCategory (string $sCategory= '')
 Set the category of notification. More...
 
void setContentAvailable (boolean $bContentAvailable=true)
 Initiates the Newsstand background download. More...
 
void setCustomIdentifier (mixed $mCustomIdentifier)
 Set the custom message identifier. More...
 
void setCustomProperty (string $sName, mixed $mValue)
 Set a custom property. More...
 
void setExpiry (integer $nExpiryValue)
 Set the expiry value. More...
 
void setSound (string $sSound= 'default')
 Set the sound to play. More...
 
void setText (string $sText)
 Set the alert message to display to the user. More...
 

Data Fields

const APPLE_RESERVED_NAMESPACE = 'aps'
 string The Apple-reserved aps namespace. More...
 
const PAYLOAD_MAXIMUM_SIZE = 2048
 integer The maximum size allowed for a notification payload. More...
 

Protected Member Functions

array _getPayload ()
 Get the payload dictionary. More...
 

Protected Attributes

mixed $_aCustomProperties
 Custom properties container. More...
 
array $_aDeviceTokens = array()
 Recipients device tokens. More...
 
boolean $_bAutoAdjustLongPayload = true
 If the JSON payload is longer than maximum allowed size, shorts message text. More...
 
boolean $_bContentAvailable
 True to initiates the Newsstand background download. More...
 
mixed $_mCustomIdentifier
 Custom message identifier. More...
 
integer $_nBadge
 Number to badge the application icon with. More...
 
integer $_nExpiryValue = 604800
 That message will expire in 604800 seconds (86400 * 7, 7 days) if not successful delivered. More...
 
string $_sCategory
 notification category. More...
 
string $_sSound
 Sound to play. More...
 
string $_sText
 Alert message to display to the user. More...
 

Detailed Description

The Push Notification Message.

The class represents a message to be delivered to an end user device. Notification Service.

See Also
http://tinyurl.com/ApplePushNotificationPayload

Definition at line 34 of file Message.php.

Member Function Documentation

void __construct ( string  $sDeviceToken = null)

Constructor.

Parameters
$sDeviceToken[optional] Recipients device token.

Definition at line 60 of file Message.php.

References addRecipient().

61  {
62  if (isset($sDeviceToken)) {
63  $this->addRecipient($sDeviceToken);
64  }
65  }
void addRecipient(string $sDeviceToken)
Add a recipient device token.
Definition: Message.php:74

Here is the call graph for this function:

string __toString ( )

PHP Magic Method.

When an object is "converted" to a string, JSON-encoded payload is returned.

Returns
JSON-encoded payload.

Definition at line 346 of file Message.php.

References getPayload().

347  {
348  try {
349  $sJSONPayload = $this->getPayload();
350  } catch (ApnsPHP_Message_Exception $e) {
351  $sJSONPayload = '';
352  }
353  return $sJSONPayload;
354  }
string getPayload()
Convert the message in a JSON-encoded payload.
Definition: Message.php:397

Here is the call graph for this function:

array _getPayload ( )
protected

Get the payload dictionary.

Returns
The payload dictionary.

Definition at line 361 of file Message.php.

Referenced by getPayload().

362  {
363  $aPayload[self::APPLE_RESERVED_NAMESPACE] = array();
364 
365  if (isset($this->_sText)) {
366  $aPayload[self::APPLE_RESERVED_NAMESPACE]['alert'] = (string)$this->_sText;
367  }
368  if (isset($this->_nBadge) && $this->_nBadge >= 0) {
369  $aPayload[self::APPLE_RESERVED_NAMESPACE]['badge'] = (int)$this->_nBadge;
370  }
371  if (isset($this->_sSound)) {
372  $aPayload[self::APPLE_RESERVED_NAMESPACE]['sound'] = (string)$this->_sSound;
373  }
374  if (isset($this->_bContentAvailable)) {
375  $aPayload[self::APPLE_RESERVED_NAMESPACE]['content-available'] = (int)$this->_bContentAvailable;
376  }
377  if (isset($this->_sCategory)) {
378  $aPayload[self::APPLE_RESERVED_NAMESPACE]['category'] = (string)$this->_sCategory;
379  }
380 
381  if (is_array($this->_aCustomProperties)) {
382  foreach($this->_aCustomProperties as $sPropertyName => $mPropertyValue) {
383  $aPayload[$sPropertyName] = $mPropertyValue;
384  }
385  }
386 
387  return $aPayload;
388  }

Here is the caller graph for this function:

void addRecipient ( string  $sDeviceToken)

Add a recipient device token.

Parameters
$sDeviceTokenRecipients device token.
Exceptions
ApnsPHP_Message_Exceptionif the device token is not well formed.

Definition at line 74 of file Message.php.

Referenced by __construct().

75  {
76  if (!preg_match('~^[a-f0-9]{64}$~i', $sDeviceToken)) {
77  throw new ApnsPHP_Message_Exception(
78  "Invalid device token '{$sDeviceToken}'"
79  );
80  }
81  $this->_aDeviceTokens[] = $sDeviceToken;
82  }

Here is the caller graph for this function:

boolean getAutoAdjustLongPayload ( )

Get the auto-adjust long payload value.

Returns
The auto-adjust long payload value.

Definition at line 335 of file Message.php.

References $_bAutoAdjustLongPayload.

336  {
338  }
boolean $_bAutoAdjustLongPayload
If the JSON payload is longer than maximum allowed size, shorts message text.
Definition: Message.php:39
integer getBadge ( )

Get the number to badge the application icon with.

Returns
The number to badge the application icon with.

Definition at line 164 of file Message.php.

References $_nBadge.

165  {
166  return $this->_nBadge;
167  }
integer $_nBadge
Number to badge the application icon with.
Definition: Message.php:44
string getCategory ( )

Get the category of notification.

Returns
The notification category

Definition at line 205 of file Message.php.

References $_sCategory.

206  {
207  return $this->_sCategory;
208  }
string $_sCategory
notification category.
Definition: Message.php:46
boolean getContentAvailable ( )

Get if should initiates the Newsstand background download.

Returns
Initiates the Newsstand background download property.

Definition at line 233 of file Message.php.

References $_bContentAvailable.

234  {
236  }
boolean $_bContentAvailable
True to initiates the Newsstand background download.
Definition: Message.php:47
mixed getCustomIdentifier ( )

Get the custom message identifier.

Returns
The custom message identifier.

Definition at line 485 of file Message.php.

References $_mCustomIdentifier.

486  {
488  }
mixed $_mCustomIdentifier
Custom message identifier.
Definition: Message.php:53
string getCustomProperty ( string  $sName)

Get the custom property value.

Parameters
$sNameCustom property name.
Exceptions
ApnsPHP_Message_Exceptionif no property exists with the specified name.
Returns
The custom property value.

Definition at line 309 of file Message.php.

310  {
311  if (!array_key_exists($sName, $this->_aCustomProperties)) {
312  throw new ApnsPHP_Message_Exception(
313  "No property exists with the specified name '{$sName}'."
314  );
315  }
316  return $this->_aCustomProperties[$sName];
317  }
string getCustomPropertyName ( )

Get the first custom property name.

Deprecated:
Use getCustomPropertyNames() instead.
Returns
The first custom property name.

Definition at line 263 of file Message.php.

264  {
265  if (!is_array($this->_aCustomProperties)) {
266  return;
267  }
268  $aKeys = array_keys($this->_aCustomProperties);
269  return $aKeys[0];
270  }
array getCustomPropertyNames ( )

Get all custom properties names.

Returns
All properties names.

Definition at line 293 of file Message.php.

294  {
295  if (!is_array($this->_aCustomProperties)) {
296  return array();
297  }
298  return array_keys($this->_aCustomProperties);
299  }
mixed getCustomPropertyValue ( )

Get the first custom property value.

Deprecated:
Use getCustomProperty() instead.
Returns
The first custom property value.

Definition at line 279 of file Message.php.

280  {
281  if (!is_array($this->_aCustomProperties)) {
282  return;
283  }
284  $aKeys = array_keys($this->_aCustomProperties);
285  return $this->_aCustomProperties[$aKeys[0]];
286  }
integer getExpiry ( )

Get the expiry value.

Returns
The expire message value (in seconds).

Definition at line 458 of file Message.php.

References $_nExpiryValue.

Referenced by ApnsPHP_Push::add().

459  {
460  return $this->_nExpiryValue;
461  }
integer $_nExpiryValue
That message will expire in 604800 seconds (86400 * 7, 7 days) if not successful delivered.
Definition: Message.php:51

Here is the caller graph for this function:

string getPayload ( )

Convert the message in a JSON-encoded payload.

Exceptions
ApnsPHP_Message_Exceptionif payload is longer than maximum allowed size and AutoAdjustLongPayload is disabled.
Returns
JSON-encoded payload.

Definition at line 397 of file Message.php.

References _getPayload().

Referenced by __toString(), and ApnsPHP_Push::add().

398  {
399  $sJSON = json_encode($this->_getPayload(), defined('JSON_UNESCAPED_UNICODE') ? JSON_UNESCAPED_UNICODE : 0);
400  if (!defined('JSON_UNESCAPED_UNICODE') && function_exists('mb_convert_encoding')) {
401  $sJSON = preg_replace_callback(
402  '~\\\\u([0-9a-f]{4})~i',
403  create_function('$aMatches', 'return mb_convert_encoding(pack("H*", $aMatches[1]), "UTF-8", "UTF-16");'),
404  $sJSON);
405  }
406 
407  $sJSONPayload = str_replace(
408  '"' . self::APPLE_RESERVED_NAMESPACE . '":[]',
409  '"' . self::APPLE_RESERVED_NAMESPACE . '":{}',
410  $sJSON
411  );
412  $nJSONPayloadLen = strlen($sJSONPayload);
413 
414  if ($nJSONPayloadLen > self::PAYLOAD_MAXIMUM_SIZE) {
415  if ($this->_bAutoAdjustLongPayload) {
416  $nMaxTextLen = $nTextLen = strlen($this->_sText) - ($nJSONPayloadLen - self::PAYLOAD_MAXIMUM_SIZE);
417  if ($nMaxTextLen > 0) {
418  while (strlen($this->_sText = mb_substr($this->_sText, 0, --$nTextLen, 'UTF-8')) > $nMaxTextLen);
419  return $this->getPayload();
420  } else {
421  throw new ApnsPHP_Message_Exception(
422  "JSON Payload is too long: {$nJSONPayloadLen} bytes. Maximum size is " .
423  self::PAYLOAD_MAXIMUM_SIZE . " bytes. The message text can not be auto-adjusted."
424  );
425  }
426  } else {
427  throw new ApnsPHP_Message_Exception(
428  "JSON Payload is too long: {$nJSONPayloadLen} bytes. Maximum size is " .
429  self::PAYLOAD_MAXIMUM_SIZE . " bytes"
430  );
431  }
432  }
433 
434  return $sJSONPayload;
435  }
string getPayload()
Convert the message in a JSON-encoded payload.
Definition: Message.php:397
array _getPayload()
Get the payload dictionary.
Definition: Message.php:361

Here is the call graph for this function:

Here is the caller graph for this function:

string getRecipient ( integer  $nRecipient = 0)

Get a recipient.

Parameters
$nRecipient[optional] Recipient number to return.
Exceptions
ApnsPHP_Message_Exceptionif no recipient number exists.
Returns
The recipient token at index $nRecipient.

Definition at line 92 of file Message.php.

Referenced by ApnsPHP_Push::add().

93  {
94  if (!isset($this->_aDeviceTokens[$nRecipient])) {
95  throw new ApnsPHP_Message_Exception(
96  "No recipient at index '{$nRecipient}'"
97  );
98  }
99  return $this->_aDeviceTokens[$nRecipient];
100  }

Here is the caller graph for this function:

array getRecipients ( )

Get all recipients.

Returns
Array of all recipients device token.

Definition at line 117 of file Message.php.

References $_aDeviceTokens.

118  {
119  return $this->_aDeviceTokens;
120  }
array $_aDeviceTokens
Recipients device tokens.
Definition: Message.php:41
integer getRecipientsNumber ( )

Get the number of recipients.

Returns
Recipient's number.

Definition at line 107 of file Message.php.

Referenced by ApnsPHP_Push::add().

108  {
109  return count($this->_aDeviceTokens);
110  }

Here is the caller graph for this function:

string getSound ( )

Get the sound to play.

Returns
The sound to play.

Definition at line 185 of file Message.php.

References $_sSound.

186  {
187  return $this->_sSound;
188  }
string $_sSound
Sound to play.
Definition: Message.php:45
string getText ( )

Get the alert message to display to the user.

Returns
The alert message to display to the user.

Definition at line 137 of file Message.php.

References $_sText.

138  {
139  return $this->_sText;
140  }
string $_sText
Alert message to display to the user.
Definition: Message.php:43
void setAutoAdjustLongPayload ( boolean  $bAutoAdjust)

Set the auto-adjust long payload value.

Parameters
$bAutoAdjustIf true a long payload is shorted cutting long text value.

Definition at line 325 of file Message.php.

326  {
327  $this->_bAutoAdjustLongPayload = (boolean)$bAutoAdjust;
328  }
void setBadge ( integer  $nBadge)

Set the number to badge the application icon with.

Parameters
$nBadgeA number to badge the application icon with.
Exceptions
ApnsPHP_Message_Exceptionif badge is not an integer.

Definition at line 149 of file Message.php.

150  {
151  if (!is_int($nBadge)) {
152  throw new ApnsPHP_Message_Exception(
153  "Invalid badge number '{$nBadge}'"
154  );
155  }
156  $this->_nBadge = $nBadge;
157  }
void setCategory ( string  $sCategory = '')

Set the category of notification.

Parameters
$sCategory[optional] A category for ios8 notification actions.

Definition at line 195 of file Message.php.

196  {
197  $this->_sCategory = $sCategory;
198  }
void setContentAvailable ( boolean  $bContentAvailable = true)

Initiates the Newsstand background download.

See Also
http://tinyurl.com/ApplePushNotificationNewsstand
Parameters
$bContentAvailableTrue to initiates the Newsstand background download.
Exceptions
ApnsPHP_Message_Exceptionif ContentAvailable is not a boolean.

Definition at line 218 of file Message.php.

219  {
220  if (!is_bool($bContentAvailable)) {
221  throw new ApnsPHP_Message_Exception(
222  "Invalid content-available value '{$bContentAvailable}'"
223  );
224  }
225  $this->_bContentAvailable = $bContentAvailable ? true : null;
226  }
void setCustomIdentifier ( mixed  $mCustomIdentifier)

Set the custom message identifier.

The custom message identifier is useful to associate a push notification to a DB record or an User entry for example. The custom message identifier can be retrieved in case of error using the getCustomIdentifier() method of an entry retrieved by the getErrors() method. This custom identifier, if present, is also used in all status message by the ApnsPHP_Push class.

Parameters
$mCustomIdentifierThe custom message identifier.

Definition at line 475 of file Message.php.

476  {
477  $this->_mCustomIdentifier = $mCustomIdentifier;
478  }
void setCustomProperty ( string  $sName,
mixed  $mValue 
)

Set a custom property.

Parameters
$sNameCustom property name.
$mValueCustom property value.
Exceptions
ApnsPHP_Message_Exceptionif custom property name is not outside the Apple-reserved 'aps' namespace.

Definition at line 246 of file Message.php.

247  {
248  if (trim($sName) == self::APPLE_RESERVED_NAMESPACE) {
249  throw new ApnsPHP_Message_Exception(
250  "Property name '" . self::APPLE_RESERVED_NAMESPACE . "' can not be used for custom property."
251  );
252  }
253  $this->_aCustomProperties[trim($sName)] = $mValue;
254  }
void setExpiry ( integer  $nExpiryValue)

Set the expiry value.

Parameters
$nExpiryValueThis message will expire in N seconds if not successful delivered.

Definition at line 443 of file Message.php.

444  {
445  if (!is_int($nExpiryValue)) {
446  throw new ApnsPHP_Message_Exception(
447  "Invalid seconds number '{$nExpiryValue}'"
448  );
449  }
450  $this->_nExpiryValue = $nExpiryValue;
451  }
void setSound ( string  $sSound = 'default')

Set the sound to play.

Parameters
$sSound[optional] A sound to play ('default sound' is the default sound).

Definition at line 175 of file Message.php.

176  {
177  $this->_sSound = $sSound;
178  }
void setText ( string  $sText)

Set the alert message to display to the user.

Parameters
$sTextAn alert message to display to the user.

Definition at line 127 of file Message.php.

128  {
129  $this->_sText = $sText;
130  }

Field Documentation

mixed $_aCustomProperties
protected

Custom properties container.

Definition at line 49 of file Message.php.

array $_aDeviceTokens = array()
protected

Recipients device tokens.

Definition at line 41 of file Message.php.

Referenced by getRecipients().

boolean $_bAutoAdjustLongPayload = true
protected

If the JSON payload is longer than maximum allowed size, shorts message text.

Definition at line 39 of file Message.php.

Referenced by getAutoAdjustLongPayload().

boolean $_bContentAvailable
protected

True to initiates the Newsstand background download.

See Also
http://tinyurl.com/ApplePushNotificationNewsstand

Definition at line 47 of file Message.php.

Referenced by getContentAvailable().

mixed $_mCustomIdentifier
protected

Custom message identifier.

Definition at line 53 of file Message.php.

Referenced by getCustomIdentifier().

integer $_nBadge
protected

Number to badge the application icon with.

Definition at line 44 of file Message.php.

Referenced by getBadge().

integer $_nExpiryValue = 604800
protected

That message will expire in 604800 seconds (86400 * 7, 7 days) if not successful delivered.

Definition at line 51 of file Message.php.

Referenced by getExpiry().

string $_sCategory
protected

notification category.

Definition at line 46 of file Message.php.

Referenced by getCategory().

string $_sSound
protected

Sound to play.

Definition at line 45 of file Message.php.

Referenced by getSound().

string $_sText
protected

Alert message to display to the user.

Definition at line 43 of file Message.php.

Referenced by getText().

const APPLE_RESERVED_NAMESPACE = 'aps'

string The Apple-reserved aps namespace.

Definition at line 37 of file Message.php.

const PAYLOAD_MAXIMUM_SIZE = 2048

integer The maximum size allowed for a notification payload.

Definition at line 36 of file Message.php.


The documentation for this class was generated from the following file: