62 if (isset($sDeviceToken)) {
76 if (!preg_match(
'~^[a-f0-9]{64}$~i', $sDeviceToken)) {
78 "Invalid device token '{$sDeviceToken}'"
81 $this->_aDeviceTokens[] = $sDeviceToken;
94 if (!isset($this->_aDeviceTokens[$nRecipient])) {
96 "No recipient at index '{$nRecipient}'"
99 return $this->_aDeviceTokens[$nRecipient];
109 return count($this->_aDeviceTokens);
129 $this->_sText = $sText;
151 if (!is_int($nBadge)) {
153 "Invalid badge number '{$nBadge}'"
156 $this->_nBadge = $nBadge;
177 $this->_sSound = $sSound;
197 $this->_sCategory = $sCategory;
220 if (!is_bool($bContentAvailable)) {
222 "Invalid content-available value '{$bContentAvailable}'"
225 $this->_bContentAvailable = $bContentAvailable ?
true : null;
248 if (trim($sName) == self::APPLE_RESERVED_NAMESPACE) {
250 "Property name '" . self::APPLE_RESERVED_NAMESPACE .
"' can not be used for custom property."
253 $this->_aCustomProperties[trim($sName)] = $mValue;
265 if (!is_array($this->_aCustomProperties)) {
268 $aKeys = array_keys($this->_aCustomProperties);
281 if (!is_array($this->_aCustomProperties)) {
284 $aKeys = array_keys($this->_aCustomProperties);
285 return $this->_aCustomProperties[$aKeys[0]];
295 if (!is_array($this->_aCustomProperties)) {
298 return array_keys($this->_aCustomProperties);
311 if (!array_key_exists($sName, $this->_aCustomProperties)) {
313 "No property exists with the specified name '{$sName}'."
316 return $this->_aCustomProperties[$sName];
327 $this->_bAutoAdjustLongPayload = (boolean)$bAutoAdjust;
353 return $sJSONPayload;
363 $aPayload[self::APPLE_RESERVED_NAMESPACE] = array();
365 if (isset($this->_sText)) {
366 $aPayload[self::APPLE_RESERVED_NAMESPACE][
'alert'] = (string)$this->_sText;
368 if (isset($this->_nBadge) && $this->_nBadge >= 0) {
369 $aPayload[self::APPLE_RESERVED_NAMESPACE][
'badge'] = (int)$this->_nBadge;
371 if (isset($this->_sSound)) {
372 $aPayload[self::APPLE_RESERVED_NAMESPACE][
'sound'] = (string)$this->_sSound;
374 if (isset($this->_bContentAvailable)) {
375 $aPayload[self::APPLE_RESERVED_NAMESPACE][
'content-available'] = (int)$this->_bContentAvailable;
377 if (isset($this->_sCategory)) {
378 $aPayload[self::APPLE_RESERVED_NAMESPACE][
'category'] = (string)$this->_sCategory;
381 if (is_array($this->_aCustomProperties)) {
382 foreach($this->_aCustomProperties as $sPropertyName => $mPropertyValue) {
383 $aPayload[$sPropertyName] = $mPropertyValue;
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");'),
407 $sJSONPayload = str_replace(
408 '"' . self::APPLE_RESERVED_NAMESPACE .
'":[]',
409 '"' . self::APPLE_RESERVED_NAMESPACE .
'":{}',
412 $nJSONPayloadLen = strlen($sJSONPayload);
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);
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."
428 "JSON Payload is too long: {$nJSONPayloadLen} bytes. Maximum size is " .
429 self::PAYLOAD_MAXIMUM_SIZE .
" bytes"
434 return $sJSONPayload;
445 if (!is_int($nExpiryValue)) {
447 "Invalid seconds number '{$nExpiryValue}'"
450 $this->_nExpiryValue = $nExpiryValue;
477 $this->_mCustomIdentifier = $mCustomIdentifier;
mixed $_mCustomIdentifier
Custom message identifier.
mixed $_aCustomProperties
Custom properties container.
void setExpiry(integer $nExpiryValue)
Set the expiry value.
const PAYLOAD_MAXIMUM_SIZE
integer The maximum size allowed for a notification payload.
string $_sText
Alert message to display to the user.
integer getBadge()
Get the number to badge the application icon with.
string getCustomPropertyName()
Get the first custom property name.
string $_sCategory
notification category.
void setText(string $sText)
Set the alert message to display to the user.
integer getRecipientsNumber()
Get the number of recipients.
void setCustomProperty(string $sName, mixed $mValue)
Set a custom property.
array getRecipients()
Get all recipients.
string getPayload()
Convert the message in a JSON-encoded payload.
string getCustomProperty(string $sName)
Get the custom property value.
void setBadge(integer $nBadge)
Set the number to badge the application icon with.
void __construct(string $sDeviceToken=null)
Constructor.
const APPLE_RESERVED_NAMESPACE
string The Apple-reserved aps namespace.
string getSound()
Get the sound to play.
mixed getCustomPropertyValue()
Get the first custom property value.
string getRecipient(integer $nRecipient=0)
Get a recipient.
integer $_nBadge
Number to badge the application icon with.
array $_aDeviceTokens
Recipients device tokens.
void setCustomIdentifier(mixed $mCustomIdentifier)
Set the custom message identifier.
void addRecipient(string $sDeviceToken)
Add a recipient device token.
string $_sSound
Sound to play.
void setSound(string $sSound= 'default')
Set the sound to play.
integer getExpiry()
Get the expiry value.
string getText()
Get the alert message to display to the user.
array getCustomPropertyNames()
Get all custom properties names.
boolean $_bContentAvailable
True to initiates the Newsstand background download.
array _getPayload()
Get the payload dictionary.
void setAutoAdjustLongPayload(boolean $bAutoAdjust)
Set the auto-adjust long payload value.
void setContentAvailable(boolean $bContentAvailable=true)
Initiates the Newsstand background download.
string __toString()
PHP Magic Method.
boolean getAutoAdjustLongPayload()
Get the auto-adjust long payload value.
integer $_nExpiryValue
That message will expire in 604800 seconds (86400 * 7, 7 days) if not successful delivered.
The Push Notification Message.
boolean getContentAvailable()
Get if should initiates the Newsstand background download.
boolean $_bAutoAdjustLongPayload
If the JSON payload is longer than maximum allowed size, shorts message text.
mixed getCustomIdentifier()
Get the custom message identifier.
void setCategory(string $sCategory= '')
Set the category of notification.
string getCategory()
Get the category of notification.