Skip to content
This repository was archived by the owner on Jun 28, 2024. It is now read-only.

Multiple FCM Account Support #21

Merged
merged 3 commits into from
Jan 16, 2023
Merged
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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Laravel-FCM (maintained version of the abandonned "official" repository)
# Laravel-FCM (added support for multiple FCM account on code-lts/Laravel-FCM repository)

![Run tests](https://github.com/code-lts/Laravel-FCM/workflows/Run%20tests/badge.svg?branch=main)
[![Coverage Status](https://codecov.io/gh/code-lts/laravel-fcm/branch/main/graph/badge.svg)](https://codecov.io/gh/code-lts/laravel-fcm)
Expand Down Expand Up @@ -157,7 +157,7 @@ $data = $dataBuilder->build();

$token = "a_registration_from_your_database";

$downstreamResponse = FCM::sendTo($token, $option, $notification, $data);
$downstreamResponse = FCM::sendTo($token, $option, $notification, $data, $senderId, $serverKey);

$downstreamResponse->numberSuccess();
$downstreamResponse->numberFailure();
Expand Down Expand Up @@ -196,7 +196,7 @@ $data = $dataBuilder->build();
// You must change it to get your tokens
$tokens = MYDATABASE::pluck('fcm_token')->toArray();

$downstreamResponse = FCM::sendTo($tokens, $option, $notification, $data);
$downstreamResponse = FCM::sendTo($tokens, $option, $notification, $data, $senderId, $serverKey);

$downstreamResponse->numberSuccess();
$downstreamResponse->numberFailure();
Expand Down Expand Up @@ -241,7 +241,7 @@ $notification = $notificationBuilder->build();
$topic = new Topics();
$topic->topic('news');

$topicResponse = FCM::sendToTopic($topic, null, $notification, null);
$topicResponse = FCM::sendToTopic($topic, null, $notification, null, $senderId, $serverKey);

$topicResponse->isSuccess();
$topicResponse->shouldRetry();
Expand Down Expand Up @@ -271,7 +271,7 @@ $topic->topic('news')->andTopic(function($condition) {

});

$topicResponse = FCM::sendToTopic($topic, null, $notification, null);
$topicResponse = FCM::sendToTopic($topic, null, $notification, null, $senderId, $serverKey);

$topicResponse->isSuccess();
$topicResponse->shouldRetry();
Expand Down Expand Up @@ -320,7 +320,7 @@ $notification = $notificationBuilder->build();

$notificationKey = ['a_notification_key'];

$groupResponse = FCM::sendToGroup($notificationKey, null, $notification, null);
$groupResponse = FCM::sendToGroup($notificationKey, null, $notification, null, $senderId, $serverKey);

$groupResponse->numberSuccess();
$groupResponse->numberFailure();
Expand Down
12 changes: 11 additions & 1 deletion src/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ class Request extends BaseRequest
* @param PayloadNotification $notification
* @param PayloadData $data
* @param Topics|null $topic
* @param string $senderId
* @param string $serverKey
*/
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, $senderId = null, $serverKey = null)
{
parent::__construct();

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

if ($senderId !== null) {
$this->config['sender_id'] = $senderId;
}

if ($serverKey !== null) {
$this->config['server_key'] = $serverKey;
}
}

/**
Expand Down
18 changes: 12 additions & 6 deletions src/Sender/FCMSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ class FCMSender extends HTTPSender
* @param Options|null $options
* @param PayloadNotification|null $notification
* @param PayloadData|null $data
* @param string $senderId
* @param string $serverKey
*
* @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, $senderId = null, $serverKey = null)
{
$response = null;

Expand All @@ -45,7 +47,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, $senderId, $serverKey);
$responseGuzzle = $this->post($request);

$response = new DownstreamResponse($responseGuzzle, $to, $this->logger);
Expand All @@ -61,12 +63,14 @@ public function sendTo($to, Options $options = null, PayloadNotification $notifi
* @param Options|null $options
* @param PayloadNotification|null $notification
* @param PayloadData|null $data
* @param string $senderId
* @param string $serverKey
*
* @return GroupResponse
*/
public function sendToGroup($notificationKey, Options $options = null, PayloadNotification $notification = null, PayloadData $data = null)
public function sendToGroup($notificationKey, Options $options = null, PayloadNotification $notification = null, PayloadData $data = null, $senderId = null, $serverKey = null)
{
$request = new Request($notificationKey, $options, $notification, $data);
$request = new Request($notificationKey, $options, $notification, $data, null, $senderId, $serverKey);

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

Expand All @@ -80,12 +84,14 @@ public function sendToGroup($notificationKey, Options $options = null, PayloadNo
* @param Options|null $options
* @param PayloadNotification|null $notification
* @param PayloadData|null $data
* @param string $senderId
* @param string $serverKey
*
* @return TopicResponse
*/
public function sendToTopic(Topics $topics, Options $options = null, PayloadNotification $notification = null, PayloadData $data = null)
public function sendToTopic(Topics $topics, Options $options = null, PayloadNotification $notification = null, PayloadData $data = null, $senderId = null, $serverKey = null)
{
$request = new Request(null, $options, $notification, $data, $topics);
$request = new Request(null, $options, $notification, $data, $topics, $senderId, $serverKey);

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

Expand Down