From f362548c4f713f902f60b23e9f836558f0a709e4 Mon Sep 17 00:00:00 2001 From: Marco Rocca Date: Thu, 28 Jul 2016 19:12:49 +0200 Subject: [PATCH 1/2] Added support for encrypted payload. --- src/PHP_GCM/Message.php | 53 +++++++++++++++++++++++++++++++++++++++++ src/PHP_GCM/Sender.php | 10 +++++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/PHP_GCM/Message.php b/src/PHP_GCM/Message.php index c11c4d9..8d88e43 100755 --- a/src/PHP_GCM/Message.php +++ b/src/PHP_GCM/Message.php @@ -27,6 +27,7 @@ class Message { const NOTIFICATION_TITLE_LOC_ARGS = 'title_loc_args'; const CONTENT_AVAILABLE = 'content_available'; const PRIORITY = 'priority'; + const RAW_DATA = 'raw_data'; private $collapseKey; private $delayWhileIdle; @@ -37,6 +38,9 @@ class Message { private $notification; private $contentAvailable; private $priority; + private $rawData; + private $publicKey; + private $salt; /** * Message Constructor @@ -196,6 +200,51 @@ public function getPriority() { return $this->priority; } + /** + * Sets the rawData property + * + * @param $rawData + * @return $this + */ + public function rawData($rawData) { + $this->rawData = $rawData; + return $this; + } + + public function getRawData() { + return $this->rawData; + } + + /** + * Sets the publicKey property + * + * @param $publicKey + * @return $this + */ + public function publicKey($publicKey) { + $this->publicKey = $publicKey; + return $this; + } + + public function getPublicKey() { + return $this->publicKey; + } + + /** + * Sets the salt property + * + * @param $salt + * @return $this + */ + public function salt($salt) { + $this->salt = $salt; + return $this; + } + + public function getSalt() { + return $this->salt; + } + public function build($recipients) { $message = array(); @@ -251,6 +300,10 @@ public function build($recipients) { $message[self::NOTIFICATION][self::NOTIFICATION_TITLE_LOC_KEY] = $this->notification->getTitleLocKey(); } + if (!empty($this->rawData)) { + $message[self::RAW_DATA] = $this->rawData; + } + return json_encode($message); } } diff --git a/src/PHP_GCM/Sender.php b/src/PHP_GCM/Sender.php index 3450fe9..f242241 100755 --- a/src/PHP_GCM/Sender.php +++ b/src/PHP_GCM/Sender.php @@ -259,7 +259,15 @@ private function performDeviceGroupOperation($senderId, $notificationKeyName, $n private function makeRequest(Message $message, array $registrationIds) { $ch = $this->getCurlRequest(); curl_setopt($ch, CURLOPT_URL, self::SEND_ENDPOINT); - curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: key=' . $this->key)); + $headers = array('Content-Type: application/json', 'Authorization: key=' . $this->key); + if(!empty($message->getSalt()) && !empty($message->getPublicKey())) { + $headers = array_merge($headers, array( + 'Encryption: salt=' . $message->getSalt(), + 'Crypto-Key: dh=' . $message->getPublicKey(), + 'Content-Encoding: aesgcm' + )); + } + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS, $message->build($registrationIds)); $response = curl_exec($ch); $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); From 384dfa0836ace764dedec9b0ef5ead35f1c64281 Mon Sep 17 00:00:00 2001 From: Marco Rocca Date: Thu, 28 Jul 2016 19:33:42 +0200 Subject: [PATCH 2/2] A little fix. --- src/PHP_GCM/Sender.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PHP_GCM/Sender.php b/src/PHP_GCM/Sender.php index f242241..a5bd6b9 100755 --- a/src/PHP_GCM/Sender.php +++ b/src/PHP_GCM/Sender.php @@ -260,7 +260,7 @@ private function makeRequest(Message $message, array $registrationIds) { $ch = $this->getCurlRequest(); curl_setopt($ch, CURLOPT_URL, self::SEND_ENDPOINT); $headers = array('Content-Type: application/json', 'Authorization: key=' . $this->key); - if(!empty($message->getSalt()) && !empty($message->getPublicKey())) { + if(!is_null($message->getSalt()) && !is_null($message->getPublicKey())) { $headers = array_merge($headers, array( 'Encryption: salt=' . $message->getSalt(), 'Crypto-Key: dh=' . $message->getPublicKey(),