Skip to content
This repository was archived by the owner on Oct 27, 2019. It is now read-only.

Support for encrypted payload #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
53 changes: 53 additions & 0 deletions src/PHP_GCM/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,6 +38,9 @@ class Message {
private $notification;
private $contentAvailable;
private $priority;
private $rawData;
private $publicKey;
private $salt;

/**
* Message Constructor
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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);
}
}
10 changes: 9 additions & 1 deletion src/PHP_GCM/Sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(!is_null($message->getSalt()) && !is_null($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);
Expand Down