Skip to content

Commit

Permalink
Added setMutableContent to the payload of the notification. (#26)
Browse files Browse the repository at this point in the history
* Added setMutableContent to the right layer of the notification.

* Removed setMutableContent from Alert
Added mutable content test
  • Loading branch information
remcoanker authored and edamov committed Oct 5, 2017
1 parent c437651 commit d6aa452
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 47 deletions.
36 changes: 36 additions & 0 deletions src/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Payload implements \JsonSerializable
const PAYLOAD_BADGE_KEY = 'badge';
const PAYLOAD_SOUND_KEY = 'sound';
const PAYLOAD_CONTENT_AVAILABLE_KEY = 'content-available';
const PAYLOAD_MUTABLE_CONTENT_KEY = 'mutable-content';
const PAYLOAD_CATEGORY_KEY = 'category';
const PAYLOAD_THREAD_ID_KEY = 'thread-id';

Expand Down Expand Up @@ -62,6 +63,13 @@ class Payload implements \JsonSerializable
*/
private $contentAvailable;

/**
* Include this key with a value of true to configure a mutable content notification.
*
* @var bool
*/
private $mutableContent;

/**
* Provide this key with a string value that represents the notification’s type.
*
Expand Down Expand Up @@ -187,6 +195,30 @@ public function isContentAvailable()
return $this->contentAvailable;
}

/**
* Set the mutable-content key for Notification Service Extensions on iOS10.
* @see http://bit.ly/mutable-content
*
* @param bool $value
* @return Payload
*/
public function setMutableContent(bool $value): Payload
{
$this->mutableContent = $value;

return $this;
}

/**
* Is content mutable.
*
* @return bool|null
*/
public function hasMutableContent()
{
return $this->mutableContent;
}

/**
* Set category.
*
Expand Down Expand Up @@ -304,6 +336,10 @@ public function jsonSerialize()
$payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_CONTENT_AVAILABLE_KEY] = (int)$this->contentAvailable;
}

if (is_bool($this->mutableContent)) {
$payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_MUTABLE_CONTENT_KEY] = (int)$this->mutableContent;
}

if (is_string($this->category)) {
$payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_CATEGORY_KEY] = $this->category;
}
Expand Down
36 changes: 0 additions & 36 deletions src/Payload/Alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class Alert implements \JsonSerializable
const ALERT_LOC_KEY = 'loc-key';
const ALERT_LOC_ARGS_KEY = 'loc-args';
const ALERT_LAUNCH_IMAGE_KEY = 'launch-image';
const ALERT_MUTABLE_CONTENT_KEY = 'mutable-content';

/**
* A short string describing the purpose of the notification.
Expand Down Expand Up @@ -93,13 +92,6 @@ class Alert implements \JsonSerializable
*/
private $launchImage;

/**
* Access to modify the content of remote notifications before they are delivered to the user.
*
* @var bool
*/
private $mutableContent;

protected function __construct()
{
}
Expand Down Expand Up @@ -316,30 +308,6 @@ public function getLaunchImage()
return $this->launchImage;
}

/**
* Set the mutable-content key for Notification Service Extensions on iOS10.
* @see http://bit.ly/mutable-content
*
* @param bool $value
* @return Alert
*/
public function setMutableContent(bool $value): Alert
{
$this->mutableContent = $value;

return $this;
}

/**
* Is content mutable.
*
* @return bool|null
*/
public function hasMutableContent()
{
return $this->mutableContent;
}

/**
* Convert Alert to JSON.
*
Expand Down Expand Up @@ -392,10 +360,6 @@ public function jsonSerialize()
$alert[self::ALERT_LAUNCH_IMAGE_KEY] = $this->launchImage;
}

if (is_bool($this->mutableContent)) {
$alert[self::ALERT_MUTABLE_CONTENT_KEY] = (int)$this->mutableContent;
}

return $alert;
}
}
12 changes: 2 additions & 10 deletions tests/Payload/AlertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,6 @@ public function testSetLaunchImage()
$this->assertEquals('launch-image', $alert->getLaunchImage());
}

public function testSetMutableContent()
{
$alert = Alert::create()->setMutableContent(true);

$this->assertEquals(true, $alert->hasMutableContent());
}

public function testAlertConvertingToJson()
{
$alert = Alert::create()
Expand All @@ -89,13 +82,12 @@ public function testAlertConvertingToJson()
->setActionLocKey('action-loc-key')
->setLocKey('loc-key')
->setLocArgs(['loc-arg'])
->setLaunchImage('launch-image')
->setMutableContent(true);
->setLaunchImage('launch-image');

$this->assertJsonStringEqualsJsonString(
'{"title":"title","body":"body","loc-key":"loc-key","loc-args":["loc-arg"],' .
'"action-loc-key":"action-loc-key","loc-key":"loc-key","loc-args":["loc-arg"],' .
'"launch-image":"launch-image","mutable-content":1}',
'"launch-image":"launch-image"}',
$alert->toJson()
);
}
Expand Down
10 changes: 9 additions & 1 deletion tests/PayloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ public function testSetContentAvailability()
$this->assertTrue($payload->isContentAvailable());
}

public function testSetMutableContent()
{
$payload = Payload::create()->setMutableContent(true);

$this->assertEquals(true, $payload->hasMutableContent());
}

public function testSetCustomValue()
{
$payload = Payload::create()->setCustomValue('key', 'value');
Expand All @@ -78,11 +85,12 @@ public function testConvertToJSon()
->setCategory('category')
->setThreadId('tread-id')
->setContentAvailability(true)
->setMutableContent(true)
->setCustomValue('key', 'value');

$this->assertJsonStringEqualsJsonString(
'{"aps": {"alert": {"title": "title"}, "badge": 1, "sound": "sound", "category": "category", ' .
' "thread-id": "tread-id", "content-available": 1}, "key": "value"}',
' "thread-id": "tread-id", "mutable-content": 1, "content-available": 1}, "key": "value"}',
$payload->toJson()
);
}
Expand Down

0 comments on commit d6aa452

Please sign in to comment.