Skip to content
This repository has been archived by the owner on Dec 7, 2019. It is now read-only.

Commit

Permalink
Support for FCM (#20)
Browse files Browse the repository at this point in the history
- Change GCM end-point to FCM
- Make Travis happy
- Test PHP 7.0 (#22)
- Add new parameters from FCM docs (#21)
- Some PHP doc fixes
  • Loading branch information
kleisauke authored and mwillbanks committed Nov 1, 2016
1 parent 629dab7 commit ef064d8
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 3 deletions.
2 changes: 1 addition & 1 deletion library/Gcm/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Client
/**
* @const string Server URI
*/
const SERVER_URI = 'https://gcm-http.googleapis.com/gcm/send';
const SERVER_URI = 'https://fcm.googleapis.com/fcm/send';

/**
* @var \Zend\Http\Client
Expand Down
104 changes: 102 additions & 2 deletions library/Gcm/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,21 @@ class Message
*/
protected $collapseKey;

/**
* @var string
*/
protected $priority = 'normal';

/**
* @var array
*/
protected $data = [];

/**
* @var array
*/
protected $notification = [];

/**
* @var bool
*/
Expand Down Expand Up @@ -150,7 +160,33 @@ public function setCollapseKey($key)
}

/**
* Set Data.
* Get priority
*
* @return string
*/
public function getPriority()
{
return $this->priority;
}

/**
* Set priority
*
* @param string $priority
* @return Message
* @throws Exception\InvalidArgumentException
*/
public function setPriority($priority)
{
if (!is_null($priority) && !(is_string($priority) && strlen($priority) > 0)) {
throw new Exception\InvalidArgumentException('$priority must be null or a non-empty string');
}
$this->priority = $priority;
return $this;
}

/**
* Set Data
*
* @param array $data
*
Expand Down Expand Up @@ -215,7 +251,65 @@ public function clearData()
}

/**
* Set Delay While Idle.
* Set notification
*
* @param array $data
* @return Message
*/
public function setNotification(array $data)
{
$this->clearNotification();
foreach ($data as $k => $v) {
$this->addNotification($k, $v);
}
return $this;
}

/**
* Get notification
*
* @return array
*/
public function getNotification()
{
return $this->notification;
}

/**
* Add notification data
*
* @param string $key
* @param mixed $value
* @return Message
* @throws Exception\InvalidArgumentException
* @throws Exception\RuntimeException
*/
public function addNotification($key, $value)
{
if (!is_string($key) || empty($key)) {
throw new Exception\InvalidArgumentException('$key must be a non-empty string');
}
if (array_key_exists($key, $this->notification)) {
throw new Exception\RuntimeException('$key conflicts with current set data');
}
$this->notification[$key] = $value;
return $this;
}

/**
* Clear notification
*
* @return Message
*/
public function clearNotification()
{
$this->notification = [];

return $this;
}

/**
* Set Delay While Idle
*
* @param bool $delay
*
Expand Down Expand Up @@ -331,9 +425,15 @@ public function toJson()
if ($this->collapseKey) {
$json['collapse_key'] = $this->collapseKey;
}
if ($this->priority) {
$json['priority'] = $this->priority;
}
if ($this->data) {
$json['data'] = $this->data;
}
if ($this->notification) {
$json['notification'] = $this->notification;
}
if ($this->delayWhileIdle) {
$json['delay_while_idle'] = $this->delayWhileIdle;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Gcm/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,21 @@ public function testExpectedDataBehavior()
self::assertContains('data', $this->m->toJson());
}

public function testExpectedNotificationBehavior()
{
$this->assertEquals($this->m->getNotification(), array());
$this->assertNotContains('notification', $this->m->toJson());
$this->m->setNotification($this->validData);
$this->assertEquals($this->m->getNotification(), $this->validData);
$this->assertContains('notification', $this->m->toJson());
$this->m->clearNotification();
$this->assertEquals($this->m->getNotification(), array());
$this->assertNotContains('notification', $this->m->toJson());
$this->m->addNotification('mykey', 'myvalue');
$this->assertEquals($this->m->getNotification(), array('mykey' => 'myvalue'));
$this->assertContains('notification', $this->m->toJson());
}

public function testInvalidDataThrowsException()
{
$this->setExpectedException(\InvalidArgumentException::class);
Expand Down

0 comments on commit ef064d8

Please sign in to comment.