From 3ee51b6ec8b84ee64d3294408b7b1a106f88b6da Mon Sep 17 00:00:00 2001 From: brozot Date: Thu, 30 Jun 2016 08:59:15 +0200 Subject: [PATCH] Fix bug with Missing registration id --- README.md | 11 +++++++++-- src/Downstream/Response.php | 14 ++++++++++---- tests/DownstreamTest.php | 8 ++++++-- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 440eb02f7..fe8a7e8c0 100644 --- a/README.md +++ b/README.md @@ -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" } } ``` @@ -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 @@ -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. diff --git a/src/Downstream/Response.php b/src/Downstream/Response.php index d4adb991e..690038256 100644 --- a/src/Downstream/Response.php +++ b/src/Downstream/Response.php @@ -32,6 +32,7 @@ class Response { protected $numberSuccess = 0; protected $numberFailure = 0; protected $numberCanonicalId = 0; + protected $hasMissingRegistrationIds = false; protected $tokensToDelete = []; protected $tokensToModify = []; @@ -111,6 +112,11 @@ public function numberModifiedToken() return $this->numberCanonicalId; } + public function hasMissingRegistrationIds() + { + return $this->hasMissingRegistrationIds; + } + public function tokenToDelete() { return $this->tokensToDelete; @@ -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)) { @@ -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 ]); } @@ -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); } } @@ -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 {} diff --git a/tests/DownstreamTest.php b/tests/DownstreamTest.php index a5e91d335..fab11fe42 100644 --- a/tests/DownstreamTest.php +++ b/tests/DownstreamTest.php @@ -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]); @@ -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, [], "{ @@ -115,7 +117,8 @@ public function it_construct_a_response_with_multiple_failures() \"results\": [ { \"error\": \"NotRegistered\" }, { \"error\": \"InvalidRegistration\" }, - { \"error\": \"NotRegistered\" } + { \"error\": \"NotRegistered\" }, + { \"error\": \"MissingRegistration\"} ] }" ); @@ -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());