From f00a26198bbafe4de3238d46288cdd74cac9b9bb Mon Sep 17 00:00:00 2001 From: Jean-Charles BERTIN Date: Tue, 4 Feb 2014 21:27:26 +0100 Subject: [PATCH] Allows messages with empty "aps" property. --- ApnsPHP/Message.php | 26 ++++++++++++++------------ ApnsPHP/Message/Custom.php | 22 +++++++++++----------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/ApnsPHP/Message.php b/ApnsPHP/Message.php index 70735c17..f9139c25 100644 --- a/ApnsPHP/Message.php +++ b/ApnsPHP/Message.php @@ -339,21 +339,24 @@ public function __toString() */ protected function _getPayload() { - $aPayload[self::APPLE_RESERVED_NAMESPACE] = array(); - + $aPayload = array(); + + $aAppleReserved = array(); if (isset($this->_sText)) { - $aPayload[self::APPLE_RESERVED_NAMESPACE]['alert'] = (string)$this->_sText; + $aAppleReserved['alert'] = (string)$this->_sText; } if (isset($this->_nBadge) && $this->_nBadge >= 0) { - $aPayload[self::APPLE_RESERVED_NAMESPACE]['badge'] = (int)$this->_nBadge; + $aAppleReserved['badge'] = (int)$this->_nBadge; } if (isset($this->_sSound)) { - $aPayload[self::APPLE_RESERVED_NAMESPACE]['sound'] = (string)$this->_sSound; + $aAppleReserved['sound'] = (string)$this->_sSound; } if (isset($this->_bContentAvailable)) { - $aPayload[self::APPLE_RESERVED_NAMESPACE]['content-available'] = (int)$this->_bContentAvailable; + $aAppleReserved['content-available'] = (int)$this->_bContentAvailable; } - + if (count($aAppleReserved)) + $aPayload[self::APPLE_RESERVED_NAMESPACE] = $aAppleReserved; + if (is_array($this->_aCustomProperties)) { foreach($this->_aCustomProperties as $sPropertyName => $mPropertyValue) { $aPayload[$sPropertyName] = $mPropertyValue; @@ -380,11 +383,10 @@ public function getPayload() $sJSON); } - $sJSONPayload = str_replace( - '"' . self::APPLE_RESERVED_NAMESPACE . '":[]', - '"' . self::APPLE_RESERVED_NAMESPACE . '":{}', - $sJSON - ); + if ($sJSON === '[]') + $sJSONPayload = '{}'; + else + $sJSONPayload = $sJSON; $nJSONPayloadLen = strlen($sJSONPayload); if ($nJSONPayloadLen > self::PAYLOAD_MAXIMUM_SIZE) { diff --git a/ApnsPHP/Message/Custom.php b/ApnsPHP/Message/Custom.php index d329060e..e501ec7e 100644 --- a/ApnsPHP/Message/Custom.php +++ b/ApnsPHP/Message/Custom.php @@ -28,6 +28,8 @@ */ class ApnsPHP_Message_Custom extends ApnsPHP_Message { + const APPLE_ALERT_PROPERTY = 'alert'; /**< @type string The alert child property. */ + protected $_sActionLocKey; /**< @type string The "View" button title. */ protected $_sLocKey; /**< @type string A key to an alert-message string in a Localizable.strings file */ protected $_aLocArgs; /**< @type array Variable string values to appear in place of the format specifiers in loc-key. */ @@ -142,28 +144,26 @@ protected function _getPayload() { $aPayload = parent::_getPayload(); - $aPayload['aps']['alert'] = array(); - + $aAlert = array(); + if (isset($this->_sText) && !isset($this->_sLocKey)) { - $aPayload['aps']['alert']['body'] = (string)$this->_sText; + $aAlert['body'] = (string)$this->_sText; } - if (isset($this->_sActionLocKey)) { - $aPayload['aps']['alert']['action-loc-key'] = $this->_sActionLocKey == '' ? + $aAlert['action-loc-key'] = $this->_sActionLocKey == '' ? null : (string)$this->_sActionLocKey; } - if (isset($this->_sLocKey)) { - $aPayload['aps']['alert']['loc-key'] = (string)$this->_sLocKey; + $aAlert['loc-key'] = (string)$this->_sLocKey; } - if (isset($this->_aLocArgs)) { - $aPayload['aps']['alert']['loc-args'] = $this->_aLocArgs; + $aAlert['loc-args'] = $this->_aLocArgs; } - if (isset($this->_sLaunchImage)) { - $aPayload['aps']['alert']['launch-image'] = (string)$this->_sLaunchImage; + $aAlert['launch-image'] = (string)$this->_sLaunchImage; } + if (count($aAlert)) + $aPayload[parent::APPLE_RESERVED_NAMESPACE][self::APPLE_ALERT_PROPERTY] = $aAlert; return $aPayload; }