Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ vendor
.project
.buildpath
.settings
.idea
72 changes: 68 additions & 4 deletions src/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ class Notification implements \JsonSerializable
private $sound;
private $clickAction;
private $tag;
private $bodyLocKey;
private $bodyLocArgs;
private $titleLocKey;
private $titleLocArgs;

public function __construct($title, $body)
public function __construct($title = null, $body = null)
{
$this->title = $title;
$this->body = $body;
Expand Down Expand Up @@ -128,16 +132,64 @@ public function setSound($sound)
return $this;
}

/**
* android/ios: The key to the body string in the app's string resources to use to localize the body
* text to the user's current localization.
* @see https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support
*
* @param string $bodyLocKey
*/
public function setBodyLocKey($bodyLocKey)
{
$this->bodyLocKey = $bodyLocKey;
}

/**
* android/ios: Variable string values to be used in place of the format specifiers in body_loc_key to
* use to localize the body text to the user's current localization.
* @see https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support
*
* @param array $bodyLocArgs
*/
public function setBodyLocArgs(array $bodyLocArgs)
{
$this->bodyLocArgs = $bodyLocArgs;
}

/**
* android/ios: The key to the title string in the app's string resources to use to localize the title
* text to the user's current localization.
* @see https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support
*
* @param string $titleLocKey
*/
public function setTitleLocKey($titleLocKey)
{
$this->titleLocKey = $titleLocKey;
}

/**
* android/ios: Variable string values to be used in place of the format specifiers in title_loc_key
* to use to localize the title text to the user's current localization.
* @see https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support
*
* @param array $titleLocArgs
*/
public function setTitleLocArgs(array $titleLocArgs)
{
$this->titleLocArgs = $titleLocArgs;
}

public function jsonSerialize()
{
$jsonData = array();

if ($this->title) {
$jsonData['title'] = $this->title;
}

$jsonData['body'] = $this->body;

if ($this->body) {
$jsonData['body'] = $this->body;
}
if ($this->badge) {
$jsonData['badge'] = $this->badge;
}
Expand All @@ -156,6 +208,18 @@ public function jsonSerialize()
if ($this->tag) {
$jsonData['tag'] = $this->tag;
}
if ($this->bodyLocKey) {
$jsonData['body_loc_key'] = $this->bodyLocKey;
}
if ($this->bodyLocArgs) {
$jsonData['body_loc_args'] = json_encode($this->bodyLocArgs);
}
if ($this->titleLocKey) {
$jsonData['title_loc_key'] = $this->titleLocKey;
}
if ($this->titleLocArgs) {
$jsonData['title_loc_args'] = json_encode($this->titleLocArgs);
}

return $jsonData;
}
Expand Down
38 changes: 31 additions & 7 deletions tests/NotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,66 @@ protected function setUp()

public function testJsonSerializeWithMinimalConfigurations()
{
$this->assertEquals(array('title' => 'foo', 'body' =>'bar'), $this->fixture->jsonSerialize());
$this->assertEquals(array('title' => 'foo', 'body' => 'bar'), $this->fixture->jsonSerialize());
}

public function testJsonSerializeWithBadge()
{
$this->fixture->setBadge(1);
$this->assertEquals(array('title' => 'foo', 'body' =>'bar', 'badge' => 1), $this->fixture->jsonSerialize());
$this->assertEquals(array('title' => 'foo', 'body' => 'bar', 'badge' => 1), $this->fixture->jsonSerialize());
}

public function testJsonSerializeWithIcon()
{
$this->fixture->setIcon('name');
$this->assertEquals(array('title' => 'foo', 'body' =>'bar', 'icon' => 'name'), $this->fixture->jsonSerialize());
$this->assertEquals(array('title' => 'foo', 'body' => 'bar', 'icon' => 'name'), $this->fixture->jsonSerialize());
}

public function testJsonSerializeWithClickAction()
{
$this->fixture->setClickAction('INTENT_NAME');
$this->assertEquals(array('title' => 'foo', 'body' =>'bar', 'click_action' => 'INTENT_NAME'), $this->fixture->jsonSerialize());
$this->assertEquals(array('title' => 'foo', 'body' => 'bar', 'click_action' => 'INTENT_NAME'), $this->fixture->jsonSerialize());
}

public function testJsonSerializeWithSound()
{
$this->fixture->setSound('mySound.mp3');
$this->assertEquals(array('title' => 'foo', 'body' =>'bar', 'sound' => 'mySound.mp3'), $this->fixture->jsonSerialize());
$this->assertEquals(array('title' => 'foo', 'body' => 'bar', 'sound' => 'mySound.mp3'), $this->fixture->jsonSerialize());
}

public function testJsonSerializeWithColor()
{
$this->fixture->setColor('#ffffff');
$this->assertEquals(array('title' => 'foo', 'body' =>'bar', 'color' => '#ffffff'), $this->fixture->jsonSerialize());
$this->assertEquals(array('title' => 'foo', 'body' => 'bar', 'color' => '#ffffff'), $this->fixture->jsonSerialize());
}

public function testJsonSerializeWithTag()
{
$this->fixture->setTag('foo');
$this->assertEquals(array('title' => 'foo', 'body' =>'bar', 'tag' => 'foo'), $this->fixture->jsonSerialize());
$this->assertEquals(array('title' => 'foo', 'body' => 'bar', 'tag' => 'foo'), $this->fixture->jsonSerialize());
}

public function testJsonSerializeWithTitleLoc()
{
$locKey = 'app_name';
$locArgs = [
'justApp'
];

$this->fixture->setTitleLocKey($locKey);
$this->fixture->setTitleLocArgs($locArgs);
$this->assertEquals(array('title' => 'foo', 'body' => 'bar', 'title_loc_key' => $locKey, 'title_loc_args' => json_encode($locArgs)), $this->fixture->jsonSerialize());
}

public function testJsonSerializeWithBodyLoc()
{
$locKey = 'sb_has_sent_a_voice_message';
$locArgs = [
'Jacky'
];

$this->fixture->setBodyLocKey($locKey);
$this->fixture->setBodyLocArgs($locArgs);
$this->assertEquals(array('title' => 'foo', 'body' => 'bar', 'body_loc_key' => $locKey, 'body_loc_args' => json_encode($locArgs)), $this->fixture->jsonSerialize());
}
}