From ef064d82fe106ea72db97315ef5987466669a1ef Mon Sep 17 00:00:00 2001 From: Kleis Auke Wolthuizen Date: Mon, 12 Sep 2016 13:02:21 +0200 Subject: [PATCH] Support for FCM (#20) - 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 --- library/Gcm/Client.php | 2 +- library/Gcm/Message.php | 104 +++++++++++++++++++++++++++++++++++++- tests/Gcm/MessageTest.php | 15 ++++++ 3 files changed, 118 insertions(+), 3 deletions(-) diff --git a/library/Gcm/Client.php b/library/Gcm/Client.php index be7b003..519c2f6 100644 --- a/library/Gcm/Client.php +++ b/library/Gcm/Client.php @@ -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 diff --git a/library/Gcm/Message.php b/library/Gcm/Message.php index 49c28b7..c36d6d5 100644 --- a/library/Gcm/Message.php +++ b/library/Gcm/Message.php @@ -33,11 +33,21 @@ class Message */ protected $collapseKey; + /** + * @var string + */ + protected $priority = 'normal'; + /** * @var array */ protected $data = []; + /** + * @var array + */ + protected $notification = []; + /** * @var bool */ @@ -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 * @@ -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 * @@ -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; } diff --git a/tests/Gcm/MessageTest.php b/tests/Gcm/MessageTest.php index f648627..f9729a4 100644 --- a/tests/Gcm/MessageTest.php +++ b/tests/Gcm/MessageTest.php @@ -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);