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

Added event support #100

Open
wants to merge 9 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
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,34 @@ $topics->topic('TopicA')
$condition->topic('TopicB')->orTopic('TopicC');
});
```
## Events
Instead of reading the response result you can listen to FCM response events:

first add a listener for the events you want to your `App\Providers\EventServiceProvider`
```php
//....
protected $listen = [
//....
//Token that must be deleted
\LaravelFCM\Events\DeleteToken::class=>[
'App\Listeners\DeleteTokenListener',
],
//Token that must be updated
\LaravelFCM\Events\UpdateToken::class=>[
'App\Listeners\UpdateTokenListener',
],
//Message that has to be resended
\LaravelFCM\Events\Resend::class=>[
'App\Listeners\ResendFCMListener',
],
//Messages with errors, in production you should delete this tokens
\LaravelFCM\Events\WithErrors::class=>[
'App\Listeners\FCMWithErrorsListener',
],
//....
]
//....
```

## Testing

Expand Down
7 changes: 7 additions & 0 deletions config/fcm.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@
'server_group_url' => 'https://android.googleapis.com/gcm/notification',
'timeout' => 30.0, // in second
],

'events'=>[
'updateToken' => \LaravelFCM\Events\UpdateToken::class,
'deleteToken' => \LaravelFCM\Events\DeleteToken::class,
'resend' => \LaravelFCM\Events\Resend::class,
'withErrors' => \LaravelFCM\Events\WithErrors::class,
],
];
22 changes: 22 additions & 0 deletions src/Events/DeleteToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
namespace LaravelFCM\Events;

class DeleteToken
{
public
/**
* Token that should be deleted
* @var string*
*/
$tokenToBeDeleted;


/**
* DeleteToken constructor.
* @param string $token Token to be deleted
*/
public function __construct($token)
{
$this->tokenToBeDeleted=$token;
}
}
31 changes: 31 additions & 0 deletions src/Events/Resend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
namespace LaravelFCM\Events;

use LaravelFCM\Message\Options;
use LaravelFCM\Message\PayloadData;
use LaravelFCM\Message\PayloadNotification;

class Resend
{
public
/**@var string**/
$token,
$options = null,
$notification = null,
$data = null;

/**
* Resend constructor.
* @param string $token Destination token that should be resended
* @param Options|null $options
* @param PayloadNotification|null $notification
* @param PayloadData|null $data
*/
public function __construct($token, Options $options = null, PayloadNotification $notification = null, PayloadData $data = null)
{
$this->token = $token;
$this->options = $options;
$this->notification = $notification;
$this->data = $data;
}
}
29 changes: 29 additions & 0 deletions src/Events/UpdateToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace LaravelFCM\Events;

class UpdateToken
{
public
/**
* Original token to be replaced
* @var string*
*/
$originalToken,
/**
* New token that should be used
* @var string*
*/
$newToken;


/**
* UpdateToken constructor.
* @param string $originalToken Original token (to be replaced with $newToken)
* @param string $newToken Updated token
*/
public function __construct($originalToken,$newToken)
{
$this->originalToken=$originalToken;
$this->newToken=$newToken;
}
}
29 changes: 29 additions & 0 deletions src/Events/WithErrors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace LaravelFCM\Events;

class WithErrors
{
public
/**
* Token that generated the errors
* @var string
**/
$token,
/**
* Errors
* @var mixed
*/
$errors;


/**
* WithErrors constructor.
* @param string $token Token with errors
* @param mixed $errors Errors encountered
*/
public function __construct($token,$errors)
{
$this->token = $token;
$this->errors = $errors;
}
}
56 changes: 51 additions & 5 deletions src/Sender/FCMSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use LaravelFCM\Request\Request;
use LaravelFCM\Message\Options;
use LaravelFCM\Message\PayloadData;
use LaravelFCM\Response\BaseResponse;
use LaravelFCM\Response\GroupResponse;
use LaravelFCM\Response\TopicResponse;
use GuzzleHttp\Exception\ClientException;
Expand Down Expand Up @@ -56,7 +57,7 @@ public function sendTo($to, Options $options = null, PayloadNotification $notifi

$response = new DownstreamResponse($responseGuzzle, $to);
}

$this->dispatchEvents($response,$options,$notification,$data);
return $response;
}

Expand All @@ -75,8 +76,9 @@ public function sendToGroup($notificationKey, Options $options = null, PayloadNo
$request = new Request($notificationKey, $options, $notification, $data);

$responseGuzzle = $this->post($request);

return new GroupResponse($responseGuzzle, $notificationKey);
$response = new GroupResponse($responseGuzzle, $notificationKey);
$this->dispatchEvents($response,$options,$notification,$data);
return $response;
}

/**
Expand All @@ -94,8 +96,9 @@ public function sendToTopic(Topics $topics, Options $options = null, PayloadNoti
$request = new Request(null, $options, $notification, $data, $topics);

$responseGuzzle = $this->post($request);

return new TopicResponse($responseGuzzle, $topics);
$response = new TopicResponse($responseGuzzle, $topics);
$this->dispatchEvents($response,$options,$notification,$data);
return $response;
}

/**
Expand All @@ -115,4 +118,47 @@ private function post($request)

return $responseGuzzle;
}


/**
* Dispatch the events
**/
protected function dispatchEvents(BaseResponse $response,Options $options = null, PayloadNotification $notification = null, PayloadData $data = null){
//Only implemented on downstreamresponse
if($response instanceof DownstreamResponse){

// To be deleted
$cl = config('fcm.events.deleteToken');
if($cl){
foreach($response->tokensToDelete() AS $token){
event(new $cl($token));
}
}

// To be updated
$cl = config('fcm.events.updateToken');
if($cl){
foreach($response->tokensToModify() AS $oldToken=>$newToken){
event(new $cl($oldToken,$newToken));
}
}

// To be resended
$cl = config('fcm.events.resend');
if($cl){
foreach($response->tokensToRetry() AS $token){
event(new $cl($token,$options,$notification,$data));
}
}

// With errors
$cl = config('fcm.events.withErrors');
if($cl){
foreach($response->tokensWithError() AS $token=>$errors){
event(new $cl($token,$errors));
}
}
}

}
}