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

Added support for multiple senders. #52

Open
wants to merge 1 commit 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
14 changes: 14 additions & 0 deletions src/Request/BaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,18 @@ public function build()
'json' => $this->buildBody(),
];
}

/**
* Overwrites the server_key and sender_id that is in the .env file.
* This allows for multiple senders. If your application supports multiple users/clients, and each client sends its
* own notifications, each one has it's own sender_id (and key), so you can save it in the database.
*
* @param $server_key
* @param $sender_id
*/
public function overwriteServerKeyAndSenderId($server_key, $sender_id)
{
$this->config['server_key'] = $server_key;
$this->config['sender_id'] = $sender_id;
}
}
10 changes: 9 additions & 1 deletion src/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ class Request extends BaseRequest
* @param PayloadNotification $notification
* @param PayloadData $data
* @param Topics|null $topic
* @param string|null $server_key // overwrites the one in .env file
* @param string|null $sender_id // overwrites the one in .env file
*/
public function __construct($to, Options $options = null, PayloadNotification $notification = null, PayloadData $data = null, Topics $topic = null)
public function __construct($to, Options $options = null, PayloadNotification $notification = null, PayloadData $data = null, Topics $topic = null, $server_key = null, $sender_id = null)
{
parent::__construct();

Expand All @@ -65,6 +67,12 @@ public function __construct($to, Options $options = null, PayloadNotification $n
$this->notification = $notification;
$this->data = $data;
$this->topic = $topic;

// If $server_key and $sender_id are passed, overwrite the ones in config
// allows for multiple senders
if ($server_key && $sender_id) {
$this->overwriteServerKeyAndSenderId($server_key, $sender_id);
}
}

/**
Expand Down
10 changes: 7 additions & 3 deletions src/Sender/FCMSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,26 @@ class FCMSender extends HTTPSender
*
* - a unique device with is registration Token
* - or to multiples devices with an array of registrationIds
* - added support to multiple senders, by allowing to overwrite the sender_id and server_key that is in the .env file
* for one that comes from the database.
*
* @param string|array $to
* @param Options|null $options
* @param PayloadNotification|null $notification
* @param PayloadData|null $data
* @param string|null $server_key // overwrites the one in .env file
* @param string|null $sender_id // overwrites the one in .env file
*
* @return DownstreamResponse|null
*/
public function sendTo($to, Options $options = null, PayloadNotification $notification = null, PayloadData $data = null)
public function sendTo($to, Options $options = null, PayloadNotification $notification = null, PayloadData $data = null, $server_key = null, $sender_id = null)
{
$response = null;

if (is_array($to) && !empty($to)) {
$partialTokens = array_chunk($to, self::MAX_TOKEN_PER_REQUEST, false);
foreach ($partialTokens as $tokens) {
$request = new Request($tokens, $options, $notification, $data);
$request = new Request($tokens, $options, $notification, $data, null, $server_key, $sender_id);

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

Expand All @@ -51,7 +55,7 @@ public function sendTo($to, Options $options = null, PayloadNotification $notifi
}
}
} else {
$request = new Request($to, $options, $notification, $data);
$request = new Request($to, $options, $notification, $data, null, $server_key, $sender_id);
$responseGuzzle = $this->post($request);

$response = new DownstreamResponse($responseGuzzle, $to);
Expand Down