Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
Fix bug with Missing registration id
Browse files Browse the repository at this point in the history
  • Loading branch information
brozot committed Jun 30, 2016
1 parent 8091b97 commit 3ee51b6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ or you can add it directly in your composer.json file:
```
{
"require": {
"brozot/laravel-fcm": "^0.9.2"
"brozot/laravel-fcm": "^0.9.3"
}
}
```
Expand Down Expand Up @@ -437,7 +437,6 @@ The response contains information about token validity and sending state.

If your application want to process more of different case of error, you may catching Followed Exceptions

- MissingRegistrationException
- InvalidPackageException
- InvalidNotificationException
- InvalidSenderIdException
Expand All @@ -463,6 +462,14 @@ Return the number of token modified

--

**hasMissingRegistrationIds()**

Return a boolean with true if some token is missing.

If this response is true, clean your database to be sure than not token is empty.

--

**tokenToDelete()**

Return an array of token that are invalid and that should be removed from your datbase.
Expand Down
14 changes: 10 additions & 4 deletions src/Downstream/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Response {
protected $numberSuccess = 0;
protected $numberFailure = 0;
protected $numberCanonicalId = 0;
protected $hasMissingRegistrationIds = false;

protected $tokensToDelete = [];
protected $tokensToModify = [];
Expand Down Expand Up @@ -111,6 +112,11 @@ public function numberModifiedToken()
return $this->numberCanonicalId;
}

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

public function tokenToDelete()
{
return $this->tokensToDelete;
Expand Down Expand Up @@ -157,7 +163,7 @@ private function detectCommonErrors() {
if (array_key_exists(self::ERROR, $result)) {

if (in_array(self::MISSING_REGISTRATION, $result)) {
throw new MissingRegistrationException();
$this->hasMissingRegistrationIds = true;
}

if (in_array(self::INVALID_PACKAGE_NAME, $result)) {
Expand Down Expand Up @@ -195,7 +201,7 @@ private function parseMultipleDevices()
}
else {
if (array_key_exists(self::ERROR, $result) && $this->to[ $index ]) {
if (in_array(self::NOT_REGISTERED, $result) || in_array(self::INVALID_REGISTRATION, $result) || in_array(self::MISSING_REGISTRATION, $result)) {
if (in_array(self::NOT_REGISTERED, $result) || in_array(self::INVALID_REGISTRATION, $result)) {
array_push($this->tokensToDelete, $this->to[ $index ]);
}

Expand Down Expand Up @@ -227,7 +233,7 @@ private function parseUniqueDevice()
}
else {
if (array_key_exists(self::ERROR, $result) && $this->to) {
if (in_array(self::NOT_REGISTERED, $result) || in_array(self::INVALID_REGISTRATION, $result) || in_array(self::MISSING_REGISTRATION, $result)) {
if (in_array(self::NOT_REGISTERED, $result) || in_array(self::INVALID_REGISTRATION, $result)) {
array_push($this->tokensToDelete, $this->to);
}
}
Expand Down Expand Up @@ -266,7 +272,7 @@ private function isLoginResult()
return app('config')['fcm.log_enabled'];
}
}
class MissingRegistrationException extends Exception {}

class InvalidPackageException extends Exception {}
class InvalidNotificationException extends Exception {}
class InvalidSenderIdException extends Exception {}
Expand Down
8 changes: 6 additions & 2 deletions tests/DownstreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public function it_construct_a_response_with_a_failure()
$this->assertEquals(0, $downstreamResponse->numberSuccess());
$this->assertEquals(1, $downstreamResponse->numberFailure());
$this->assertEquals(0, $downstreamResponse->numberModifiedToken());
$this->assertFalse($downstreamResponse->hasMissingRegistrationIds());

$this->assertCount(1, $downstreamResponse->tokenToDelete());
$this->assertEquals($token, $downstreamResponse->tokenToDelete()[0]);
Expand All @@ -104,7 +105,8 @@ public function it_construct_a_response_with_multiple_failures()
$tokens = [
"first_token",
"second_token",
"third_token"
"third_token",
"fourth_token"
];

$response = new Response(200, [], "{
Expand All @@ -115,7 +117,8 @@ public function it_construct_a_response_with_multiple_failures()
\"results\": [
{ \"error\": \"NotRegistered\" },
{ \"error\": \"InvalidRegistration\" },
{ \"error\": \"NotRegistered\" }
{ \"error\": \"NotRegistered\" },
{ \"error\": \"MissingRegistration\"}
]
}" );

Expand All @@ -124,6 +127,7 @@ public function it_construct_a_response_with_multiple_failures()
$this->assertEquals(0, $downstreamResponse->numberSuccess());
$this->assertEquals(3, $downstreamResponse->numberFailure());
$this->assertEquals(0, $downstreamResponse->numberModifiedToken());
$this->assertTrue($downstreamResponse->hasMissingRegistrationIds());


$this->assertCount(3, $downstreamResponse->tokenToDelete());
Expand Down

0 comments on commit 3ee51b6

Please sign in to comment.