Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows messages with empty "aps" property. #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions ApnsPHP/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
22 changes: 11 additions & 11 deletions ApnsPHP/Message/Custom.php
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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;
}
Expand Down