WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
Open
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
d98b7f3
Add files via upload
dommccarty Jan 8, 2017
8a5f0f5
Update README.md
dommccarty Jan 8, 2017
f0b18bf
Update README.md
dommccarty Jan 8, 2017
da554d7
Add files via upload
dommccarty Jan 8, 2017
60abcd7
Update Exception.php
dommccarty Jan 8, 2017
8f74f3e
Update README.md
dommccarty Jan 8, 2017
530f345
Update Response.php
dommccarty Jan 8, 2017
bf1d7e5
Stop storing raw response
dommccarty Jan 8, 2017
266823c
Update Response.php
dommccarty Jan 8, 2017
4e5bc71
Update Response.php
dommccarty Jan 8, 2017
bc6b5a5
Update README.md
dommccarty Jan 10, 2017
705fc9b
Update README.md
dommccarty Jan 10, 2017
b758c7c
Update README.md
dommccarty Jan 10, 2017
042ebb0
Update README.md
dommccarty Jan 10, 2017
cc44eb9
Respond to pull request remarks
dommccarty Jan 10, 2017
4b27d4e
Indentation
dommccarty Jan 10, 2017
712ea7c
Text editor problems ...
dommccarty Jan 10, 2017
235a667
Add files via upload
dommccarty Jan 10, 2017
8c4f381
Update Exception.php
dommccarty Jan 10, 2017
48d6ab3
Indentation gremlins
dommccarty Jan 10, 2017
7e85a44
Update Response.php
dommccarty Jan 10, 2017
ca976f3
Update Response.php
dommccarty Jan 10, 2017
93a27a2
Update Sender.php
dommccarty Jan 10, 2017
7e68f27
Update Message.php
dommccarty Jan 10, 2017
e428154
Update Message.php
dommccarty Jan 10, 2017
1761917
Update Response.php
dommccarty Jan 10, 2017
3bbc5ed
Update README.md
dommccarty Jan 10, 2017
2868047
Indentation ...
dommccarty Jan 10, 2017
418efd3
Indentation
dommccarty Jan 10, 2017
ec55ec9
Indentation
dommccarty Jan 10, 2017
ea3a222
Update Response.php
dommccarty Jan 10, 2017
1635a89
Indentation
dommccarty Jan 10, 2017
2e763b3
Respond to comments
dommccarty Jan 11, 2017
90c63ab
Delete Exception.php
dommccarty Jan 11, 2017
af89559
Delete Message.php
dommccarty Jan 11, 2017
79cf163
Delete Response.php
dommccarty Jan 11, 2017
a5cdd5e
Delete Sender.php
dommccarty Jan 11, 2017
5470a0c
Respond to comments
dommccarty Jan 11, 2017
c8fb000
Add files via upload
dommccarty Jan 11, 2017
3190ea4
Add files via upload
dommccarty Jan 11, 2017
268de47
Add files via upload
dommccarty Jan 11, 2017
68eea46
Strip out "\r" from headers too
dommccarty Jan 14, 2017
bcb07c9
Includes colon now.
dommccarty Jan 14, 2017
8fecb99
change ->notification to ->setNotification
dommccarty Jan 14, 2017
4b33453
Add files via upload
dommccarty Jan 14, 2017
d05d88f
Add files via upload
dommccarty Jan 14, 2017
ddfa5cf
Slimming down
dommccarty Jan 14, 2017
019602e
Add files via upload
dommccarty Jan 14, 2017
b978100
Add files via upload
dommccarty Jan 14, 2017
03db6b7
Add files via upload
dommccarty Jan 14, 2017
13a0bc2
Add files via upload
dommccarty Jan 14, 2017
c0e9cc9
Add files via upload
dommccarty Jan 14, 2017
033a9cc
Add files via upload
dommccarty Jan 14, 2017
45eb6ed
Add files via upload
dommccarty Jan 14, 2017
1d942c8
Add files via upload
dommccarty Jan 15, 2017
9e9dc81
Add files via upload
dommccarty Jan 15, 2017
2fa1df9
Rename travis.yml to .travis.yml
dommccarty Jan 15, 2017
4c2b90a
Add files via upload
dommccarty Jan 15, 2017
daa3bd4
Update README.md
dommccarty Jan 15, 2017
c1dce41
Add files via upload
dommccarty Jan 15, 2017
6caf321
Add files via upload
dommccarty Jan 15, 2017
bc3723c
Added Travis CI build status icon
dommccarty Jan 15, 2017
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
50 changes: 48 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ $message
try {
$response = $sender->send($message);

if ($response->getMustRetry() {
$waitSeconds = $response->getWaitSeconds();
//Try again after that many seconds, and use exponential backoff subsequently, as needed.
//TODO
}

if ($response->getNewRegistrationIdsCount() > 0) {
$newRegistrationIds = $response->getNewRegistrationIds();
foreach ($newRegistrationIds as $oldRegistrationId => $newRegistrationId){
Expand All @@ -40,7 +46,21 @@ try {
}

if ($response->getFailureCount() > 0) {
$invalidRegistrationIds = $GCMresponse->getInvalidRegistrationIds();

if ($response->getExistsInvalidDataKey()) {
//You used a reserved data key
$error_msg = 'Invalid data key in payload. ' . json_encode($message->getNotification());
throw new Exception($error_msg, Exception::INVALID_DATA_KEY);
}

if ($response->getExistsMismatchSenderId()) {
//A client sent the wrong senderId when it registered for pushes
$error_msg = 'Mismatch senderId. Problem clients are '
. json_encode($response->getMismatchSenderIdIds());
throw new Exception($error_msg, Exception::MISMATCH_SENDER_ID);
}

$invalidRegistrationIds = $response->getInvalidRegistrationIds();
foreach($invalidRegistrationIds as $invalidRegistrationId) {
//Remove $invalidRegistrationId from DB
//TODO
Expand All @@ -58,6 +78,9 @@ try {
case GCM\Exception::MALFORMED_REQUEST:
case GCM\Exception::UNKNOWN_ERROR:
case GCM\Exception::MALFORMED_RESPONSE:
case GCM\Exception::INVALID_DATA_KEY: //you used a forbidden key in the notification
case GCM\Exception::CURL_ERROR: //problem posting to gcm server
case GCM\Exception::MISMATCH_SENDER_ID; //a client sent the wrong senderId when it registered for pushes
//Deal with it
break;
}
Expand All @@ -80,6 +103,12 @@ try {
"collapse_key"
);

if ($response->getMustRetry() {
$waitSeconds = $response->getWaitSeconds();
//Try again after that many seconds, and use exponential backoff subsequently, as needed.
//TODO
}

if ($response->getNewRegistrationIdsCount() > 0) {
$newRegistrationIds = $response->getNewRegistrationIds();
foreach ($newRegistrationIds as $oldRegistrationId => $newRegistrationId){
Expand All @@ -89,7 +118,21 @@ try {
}

if ($response->getFailureCount() > 0) {
$invalidRegistrationIds = $GCMresponse->getInvalidRegistrationIds();

if ($response->getExistsInvalidDataKey()) {
//You used a reserved data key
$error_msg = 'Invalid data key in payload. ' . json_encode($message->getNotification());
throw new Exception($error_msg, Exception::INVALID_DATA_KEY);
}

if ($response->getExistsMismatchSenderId()) {
//A client sent the wrong senderId when it registered for pushes
$error_msg = 'Mismatch senderId. Problem clients are '
. json_encode($response->getMismatchSenderIdIds());
throw new Exception($error_msg, Exception::MISMATCH_SENDER_ID);
}

$invalidRegistrationIds = $response->getInvalidRegistrationIds();
foreach($invalidRegistrationIds as $invalidRegistrationId) {
//Remove $invalidRegistrationId from DB
//TODO
Expand All @@ -107,6 +150,9 @@ try {
case GCM\Exception::MALFORMED_REQUEST:
case GCM\Exception::UNKNOWN_ERROR:
case GCM\Exception::MALFORMED_RESPONSE:
case GCM\Exception::INVALID_DATA_KEY: //you used a forbidden key in the notification
case GCM\Exception::CURL_ERROR: //problem posting to gcm server
case GCM\Exception::MISMATCH_SENDER_ID; //a client sent the wrong senderId when it registered for pushes
//Deal with it
break;
}
Expand Down
6 changes: 4 additions & 2 deletions library/CodeMonkeysRu/GCM/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ class Exception extends \Exception
const MALFORMED_REQUEST = 3;
const UNKNOWN_ERROR = 4;
const MALFORMED_RESPONSE = 5;

}
const INVALID_DATA_KEY = 6;
const CURL_ERROR = 7;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we should not expose our transport layer.
There should be no mentions of CURL

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, folded into UNKNOWN_ERROR exception.

const MISMATCH_SENDER_ID = 8;
}
2 changes: 1 addition & 1 deletion library/CodeMonkeysRu/GCM/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,4 @@ public function setContentAvailable($contentAvailable)
$this->contentAvailable = $contentAvailable;
return $this;
}
}
}
135 changes: 127 additions & 8 deletions library/CodeMonkeysRu/GCM/Response.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<?php
namespace CodeMonkeysRu\GCM;

/**
* @author Vladimir Savenkov <[email protected]>
*/
class Response
{

Expand Down Expand Up @@ -34,6 +31,42 @@ class Response
* @var integer
*/
private $canonicalIds = null;

/**
* Response headers.
*
* @var string[]
*/
private $responseHeaders = [];

/**
* Did Google demand that we try again.
*
* @var boolean
*/
private $mustRetry = false;

/**
* Number of seconds to wait.
*
* @var integer
*/
private $waitSeconds = null;

/**
* Did you use a reserved data key?
*
* @var boolean
*/
private $existsInvalidDataKey = false;

/**
* Did one of your clients register with the wrong senderId?
* If one of them did, then presumably they all did.
*
* @var boolean
*/
private $existsMismatchSenderId = false;

/**
* Array of objects representing the status of the messages processed.
Expand All @@ -53,8 +86,20 @@ class Response
*/
private $results = array();

public function __construct(Message $message, $responseBody)
public function __construct(Message $message, $responseBody, $responseHeaders)
{
$this->responseHeaders = $responseHeaders;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls remove this newline

$this->mustRetry = false;

foreach($responseHeaders as $header) {
if (strpos($header, 'Retry-After') !== false) {
$this->mustRetry = true;
$this->waitSeconds = (int) explode(" ", $header)[1];
break;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls check indentation of the section above


$data = \json_decode($responseBody, true);
if ($data === null) {
throw new Exception("Malformed reponse body. ".$responseBody, Exception::MALFORMED_RESPONSE);
Expand All @@ -63,26 +108,72 @@ public function __construct(Message $message, $responseBody)
$this->failure = $data['failure'];
$this->success = $data['success'];
$this->canonicalIds = $data['canonical_ids'];
$this->existsInvalidDataKey = false;
$this->existsMismatchSenderId = false;
$this->results = array();

foreach ($message->getRegistrationIds() as $key => $registrationId) {
$this->results[$registrationId] = $data['results'][$key];
$result = $data['results'][$key];
if (isset($result['error'])) {
switch ($result['error']) {
case "InvalidDataKey":
$this->existsInvalidDataKey = true;
break;
case "MismatchSenderId":
$this->existsMismatchSenderId = true;
break;
default:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls remove empty default block

break;
}
}
$this->results[$registrationId] = $result;
}
$result = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do we need this line for?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use that variable in the foreach, and if a developer wants to add to the constructor and use the variable $result, it should be empty.

}


public function getResponseHeaders() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls move this curly bracket to a new line

return $this->responseHeaders;
}

public function getMulticastId()
{
return $this->multicastId;
}

public function getMustRetry()
{
return $this->mustRetry;
}

public function getWaitSeconds()
{
return $this->waitSeconds;
}

public function getSuccessCount()
{
return $this->success;
}


/**
* Both implementation errors and server errors are included here.
*
* @return integer
*/
public function getFailureCount()
{
return $this->failure;
}

public function getExistsInvalidDataKey()
{
return $this->existsInvalidDataKey;
}

public function getExistsMismatchSenderId()
{
return $this->existsMismatchSenderId;
}

public function getNewRegistrationIdsCount()
{
Expand Down Expand Up @@ -144,6 +235,7 @@ function($result) {
return array_keys($filteredResults);
}


/**
* Returns an array of registration ids for which you must resend a message (?),
* cause devices aren't available now.
Expand All @@ -162,11 +254,38 @@ function($result) {
return (
isset($result['error'])
&&
(
($result['error'] == "Unavailable")
||
($result['error'] == "InternalServerError")
||
($result['error'] == "DeviceMessageRateExceeded")
)
);
});

return array_keys($filteredResults);
}

/**
* Returns an array of registration ids who registered
* for pushes using the wrong senderId.
*
* @return array
*/
public function getMismatchSenderIdIds() {
if ($this->getFailureCount() == 0) {
return array();
}
$filteredResults = array_filter($this->results,
function($result) {
return (
isset($result['error'])
&&
($result['error'] == "MismatchSenderId")
);
});

}
return array_keys($filteredResults);
}
}
20 changes: 15 additions & 5 deletions library/CodeMonkeysRu/GCM/Sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ public function send(Message $message)
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $this->gcmUrl);

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1); // return HTTP headers with response

if ($this->caInfoPath !== false) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
Expand All @@ -104,7 +104,18 @@ public function send(Message $message)

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$resultBody = curl_exec($ch);
$resp = curl_exec($ch);

if ($resp === FALSE) {
throw new Exception('cURL error: '. curl_error($ch), Exception::CURL_ERROR);
}

list($responseHeaders, $resultBody) = explode("\r\n\r\n", $resp, 2);
// $headers now has a string of the HTTP headers
// $resultBody is the body of the HTTP response

$responseHeaders = explode("\n", $responseHeaders);

$resultHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);
Expand All @@ -128,7 +139,7 @@ public function send(Message $message)
break;
}

return new Response($message, $resultBody);
return new Response($message, $resultBody, $responseHeaders);
}

/**
Expand All @@ -152,7 +163,7 @@ private function formMessageData(Message $message)
'time_to_live' => 'getTtl',
'restricted_package_name' => 'getRestrictedPackageName',
'dry_run' => 'getDryRun',
'content_available' => 'getContentAvailable',
'content_available' => 'getContentAvailable'
);

foreach ($dataFields as $fieldName => $getter) {
Expand Down Expand Up @@ -183,5 +194,4 @@ private function validatePayloadSize(array $rawData, $fieldName, $maxSize)
);
}
}

}