update v1.0.7.9 R.C.
This is a Release Candidate. We are still testing.
This commit is contained in:
21
vendor/brozot/laravel-fcm/src/FCMManager.php
vendored
Normal file
21
vendor/brozot/laravel-fcm/src/FCMManager.php
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php namespace LaravelFCM;
|
||||
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use Illuminate\Support\Manager;
|
||||
|
||||
class FCMManager extends Manager {
|
||||
|
||||
public function getDefaultDriver()
|
||||
{
|
||||
return $this->app['config']['fcm.driver'];
|
||||
}
|
||||
|
||||
protected function createHttpDriver()
|
||||
{
|
||||
$config = $this->app['config']->get('fcm.http', []);
|
||||
return new Client([
|
||||
'timeout' => $config['timeout'],
|
||||
]);
|
||||
}
|
||||
}
|
45
vendor/brozot/laravel-fcm/src/FCMServiceProvider.php
vendored
Normal file
45
vendor/brozot/laravel-fcm/src/FCMServiceProvider.php
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php namespace LaravelFCM;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use LaravelFCM\Sender\FCMSender;
|
||||
use LaravelFCM\Sender\FCMGroup;
|
||||
|
||||
class FCMServiceProvider extends ServiceProvider {
|
||||
|
||||
public function boot()
|
||||
{
|
||||
if (str_contains($this->app->version(), 'Lumen')) {
|
||||
$this->app->configure('fcm');
|
||||
}
|
||||
else {
|
||||
$this->publishes([
|
||||
__DIR__."/../config/fcm.php" => config_path('fcm.php')
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function register()
|
||||
{
|
||||
$this->app->bind('fcm.group', function($app) {
|
||||
return new FCMGroup();
|
||||
});
|
||||
|
||||
$this->app->bind('fcm.sender', function($app) {
|
||||
return new FCMSender();
|
||||
});
|
||||
|
||||
$this->registerClient();
|
||||
}
|
||||
|
||||
public function registerClient()
|
||||
{
|
||||
$this->app->singleton('fcm.client', function($app) {
|
||||
return (new FCMManager($app))->driver();
|
||||
});
|
||||
}
|
||||
|
||||
protected function provide()
|
||||
{
|
||||
return [ 'fcm', 'fcm.client' ];
|
||||
}
|
||||
}
|
12
vendor/brozot/laravel-fcm/src/Facades/FCM.php
vendored
Normal file
12
vendor/brozot/laravel-fcm/src/Facades/FCM.php
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php namespace LaravelFCM\Facades;
|
||||
|
||||
use LaravelFCM\Message;
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
class FCM extends Facade {
|
||||
|
||||
protected static function getFacadeAccessor()
|
||||
{
|
||||
return 'fcm.sender';
|
||||
}
|
||||
}
|
12
vendor/brozot/laravel-fcm/src/Facades/FCMGroup.php
vendored
Normal file
12
vendor/brozot/laravel-fcm/src/Facades/FCMGroup.php
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php namespace LaravelFCM\Facades;
|
||||
|
||||
use LaravelFCM\Message;
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
class FCMGroup extends Facade {
|
||||
|
||||
protected static function getFacadeAccessor()
|
||||
{
|
||||
return 'fcm.group';
|
||||
}
|
||||
}
|
13
vendor/brozot/laravel-fcm/src/Message/Exceptions/InvalidOptionsException.php
vendored
Normal file
13
vendor/brozot/laravel-fcm/src/Message/Exceptions/InvalidOptionsException.php
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php namespace LaravelFCM\Message\Exceptions;
|
||||
|
||||
use Exception;
|
||||
use GuzzleHttp\Psr7\Response as GuzzleResponse;
|
||||
|
||||
/**
|
||||
* Class InvalidOptionsException
|
||||
*
|
||||
* @package LaravelFCM\Response\Exceptions
|
||||
*/
|
||||
class InvalidOptionsException extends Exception {
|
||||
|
||||
}
|
13
vendor/brozot/laravel-fcm/src/Message/Exceptions/NoTopicProvidedException.php
vendored
Normal file
13
vendor/brozot/laravel-fcm/src/Message/Exceptions/NoTopicProvidedException.php
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php namespace LaravelFCM\Message\Exceptions;
|
||||
|
||||
use Exception;
|
||||
use GuzzleHttp\Psr7\Response as GuzzleResponse;
|
||||
|
||||
/**
|
||||
* Class NoTopicProvidedException
|
||||
*
|
||||
* @package LaravelFCM\Response\Exceptions
|
||||
*/
|
||||
class NoTopicProvidedException extends Exception {
|
||||
|
||||
}
|
93
vendor/brozot/laravel-fcm/src/Message/Options.php
vendored
Normal file
93
vendor/brozot/laravel-fcm/src/Message/Options.php
vendored
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php namespace LaravelFCM\Message;
|
||||
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
|
||||
/**
|
||||
* Class Options
|
||||
*
|
||||
* @package LaravelFCM\Message
|
||||
*/
|
||||
class Options implements Arrayable {
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|string
|
||||
*/
|
||||
protected $collapseKey;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|string
|
||||
*/
|
||||
protected $priority;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var bool
|
||||
*/
|
||||
protected $contentAvailable;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var bool
|
||||
*/
|
||||
protected $delayWhileIdle;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var int|null
|
||||
*/
|
||||
protected $timeToLive;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|string
|
||||
*/
|
||||
protected $restrictedPackageName;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var bool
|
||||
*/
|
||||
protected $isDryRun = false;
|
||||
|
||||
/**
|
||||
* Options constructor.
|
||||
*
|
||||
* @param OptionsBuilder $builder
|
||||
*/
|
||||
public function __construct(OptionsBuilder $builder)
|
||||
{
|
||||
$this->collapseKey = $builder->getCollapseKey();
|
||||
$this->priority = $builder->getPriority();
|
||||
$this->contentAvailable = $builder->isContentAvailable();
|
||||
$this->delayWhileIdle = $builder->isDelayWhileIdle();
|
||||
$this->timeToLive = $builder->getTimeToLive();
|
||||
$this->restrictedPackageName = $builder->getRestrictedPackageName();
|
||||
$this->isDryRun = $builder->isDryRun();
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform Option to array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function toArray()
|
||||
{
|
||||
$contentAvailable = $this->contentAvailable ? true : null;
|
||||
$delayWhileIdle = $this->delayWhileIdle ? true : null;
|
||||
$dryRun = $this->isDryRun ? true : null;
|
||||
|
||||
$options = [
|
||||
'collapse_key' => $this->collapseKey,
|
||||
'priority' => $this->priority,
|
||||
'content_available' => $contentAvailable,
|
||||
'delay_while_idle' => $delayWhileIdle,
|
||||
'time_to_live' => $this->timeToLive,
|
||||
'restricted_package_name' => $this->restrictedPackageName,
|
||||
'dry_run' => $dryRun
|
||||
];
|
||||
|
||||
return array_filter($options);
|
||||
}
|
||||
}
|
298
vendor/brozot/laravel-fcm/src/Message/OptionsBuilder.php
vendored
Normal file
298
vendor/brozot/laravel-fcm/src/Message/OptionsBuilder.php
vendored
Normal file
@@ -0,0 +1,298 @@
|
||||
<?php namespace LaravelFCM\Message;
|
||||
|
||||
use Exception;
|
||||
use LaravelFCM\Message\Exceptions\InvalidOptionsException;
|
||||
use ReflectionClass;
|
||||
|
||||
/**
|
||||
* Builder for creation of options used by FCM
|
||||
*
|
||||
* Class OptionsBuilder
|
||||
*
|
||||
* @package LaravelFCM\Message\OptionsBuilder
|
||||
*
|
||||
* Form more information about options, please refer to google official documentation :
|
||||
* @link http://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream-http-messages-json
|
||||
*/
|
||||
class OptionsBuilder {
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var string
|
||||
*/
|
||||
protected $collapseKey;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var string
|
||||
*
|
||||
*/
|
||||
protected $priority;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var boolean
|
||||
*/
|
||||
protected $contentAvailable = false;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var boolean
|
||||
*/
|
||||
protected $delayWhileIdle = false;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var string
|
||||
*/
|
||||
protected $timeToLive;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var string
|
||||
*/
|
||||
protected $restrictedPackageName;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var boolean
|
||||
*/
|
||||
protected $dryRun = false;
|
||||
|
||||
/**
|
||||
* This parameter identifies a group of messages
|
||||
* A maximum of 4 different collapse keys is allowed at any given time.
|
||||
*
|
||||
* @param String $collapseKey
|
||||
*
|
||||
* @return \LaravelFCM\Message\OptionsBuilder
|
||||
*/
|
||||
public function setCollapseKey($collapseKey)
|
||||
{
|
||||
$this->collapseKey = $collapseKey;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the priority of the message. Valid values are "normal" and "high."
|
||||
* By default, messages are sent with normal priority
|
||||
*
|
||||
* @param String $priority
|
||||
*
|
||||
* @return \LaravelFCM\Message\OptionsBuilder
|
||||
*
|
||||
* @throws InvalidOptionsException
|
||||
*/
|
||||
public function setPriority($priority)
|
||||
{
|
||||
if (!OptionsPriorities::isValid($priority)) {
|
||||
throw new InvalidOptionsException('priority is not valid, please refer to the documentation or use the constants of the class "OptionsPriorities"');
|
||||
}
|
||||
$this->priority = $priority;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* support only Android and Ios
|
||||
*
|
||||
* An inactive client app is awoken.
|
||||
* On iOS, use this field to represent content-available in the APNS payload.
|
||||
* On Android, data messages wake the app by default.
|
||||
* On Chrome, currently not supported.
|
||||
*
|
||||
* @param boolean $contentAvailable
|
||||
*
|
||||
* @return \LaravelFCM\Message\OptionsBuilder
|
||||
*/
|
||||
public function setContentAvailable($contentAvailable)
|
||||
{
|
||||
$this->contentAvailable = $contentAvailable;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* When this parameter is set to true, it indicates that the message should not be sent until the device becomes active.
|
||||
*
|
||||
* @param boolean $delayWhileIdle
|
||||
*
|
||||
* @return \LaravelFCM\Message\OptionsBuilder
|
||||
*/
|
||||
public function setDelayWhileIdle($delayWhileIdle)
|
||||
{
|
||||
$this->delayWhileIdle = $delayWhileIdle;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* This parameter specifies how long the message should be kept in FCM storage if the device is offline
|
||||
*
|
||||
* @param int $timeToLive (in second) min:0 max:2419200
|
||||
*
|
||||
* @return \LaravelFCM\Message\OptionsBuilder
|
||||
*
|
||||
* @throws InvalidOptionException
|
||||
*/
|
||||
public function setTimeToLive($timeToLive)
|
||||
{
|
||||
if ($timeToLive < 0 || $timeToLive > 2419200) {
|
||||
throw new InvalidOptionException("time to live must be between 0 and 2419200, current value is: {$timeToLive}");
|
||||
}
|
||||
$this->timeToLive = $timeToLive;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* This parameter specifies the package name of the application where the registration tokens must match in order to receive the message.
|
||||
*
|
||||
* @param string $restrictedPackageName
|
||||
*
|
||||
* @return \LaravelFCM\Message\OptionsBuilder
|
||||
*/
|
||||
public function setRestrictedPackageName($restrictedPackageName)
|
||||
{
|
||||
$this->restrictedPackageName = $restrictedPackageName;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* This parameter, when set to true, allows developers to test a request without actually sending a message.
|
||||
* It should only be used for the development
|
||||
*
|
||||
* @param boolean $isDryRun
|
||||
*
|
||||
* @return \LaravelFCM\Message\OptionsBuilder
|
||||
*/
|
||||
public function setDryRun($isDryRun)
|
||||
{
|
||||
$this->dryRun = $isDryRun;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the collapseKey
|
||||
*
|
||||
* @return null|string
|
||||
*/
|
||||
public function getCollapseKey()
|
||||
{
|
||||
return $this->collapseKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the priority
|
||||
*
|
||||
* @return null|string
|
||||
*/
|
||||
public function getPriority()
|
||||
{
|
||||
return $this->priority;
|
||||
}
|
||||
|
||||
/**
|
||||
* is content available
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isContentAvailable()
|
||||
{
|
||||
return $this->contentAvailable;
|
||||
}
|
||||
|
||||
/**
|
||||
* is delay white idle
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isDelayWhileIdle()
|
||||
{
|
||||
return $this->delayWhileIdle;
|
||||
}
|
||||
|
||||
/**
|
||||
* get time to live
|
||||
*
|
||||
* @return null|int
|
||||
*/
|
||||
public function getTimeToLive()
|
||||
{
|
||||
return $this->timeToLive;
|
||||
}
|
||||
|
||||
/**
|
||||
* get restricted package name
|
||||
*
|
||||
* @return null|string
|
||||
*/
|
||||
public function getRestrictedPackageName()
|
||||
{
|
||||
return $this->restrictedPackageName;
|
||||
}
|
||||
|
||||
/**
|
||||
* is dry run
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isDryRun()
|
||||
{
|
||||
return $this->dryRun;
|
||||
}
|
||||
|
||||
/**
|
||||
* build an instance of Options
|
||||
*
|
||||
* @return Options
|
||||
*/
|
||||
public function build()
|
||||
{
|
||||
return new Options($this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Class OptionsPriorities
|
||||
*
|
||||
* @package LaravelFCM\Message\OptionsPriorities
|
||||
*/
|
||||
final class OptionsPriorities
|
||||
{
|
||||
|
||||
/**
|
||||
* @const high priority : iOS, these correspond to APNs priorities 10.
|
||||
*/
|
||||
const high = "high";
|
||||
|
||||
/**
|
||||
* @const normal priority : iOS, these correspond to APNs priorities 5
|
||||
*/
|
||||
const normal = "normal";
|
||||
|
||||
/**
|
||||
* @return array priorities available in fcm
|
||||
*/
|
||||
static function getPriorities()
|
||||
{
|
||||
$class = new ReflectionClass(__CLASS__);
|
||||
return $class->getConstants();
|
||||
}
|
||||
|
||||
/**
|
||||
* check if this priority is supported by fcm
|
||||
*
|
||||
* @param $priority
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
static function isValid($priority)
|
||||
{
|
||||
return in_array($priority, static::getPriorities());
|
||||
}
|
||||
}
|
38
vendor/brozot/laravel-fcm/src/Message/PayloadData.php
vendored
Normal file
38
vendor/brozot/laravel-fcm/src/Message/PayloadData.php
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php namespace LaravelFCM\Message;
|
||||
|
||||
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
|
||||
/**
|
||||
* Class PayloadData
|
||||
*
|
||||
* @package LaravelFCM\Message
|
||||
*/
|
||||
class PayloadData implements Arrayable{
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var array
|
||||
*/
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* PayloadData constructor.
|
||||
*
|
||||
* @param PayloadDataBuilder $builder
|
||||
*/
|
||||
public function __construct(PayloadDataBuilder $builder)
|
||||
{
|
||||
$this->data = $builder->getData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform payloadData to array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function toArray()
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
}
|
77
vendor/brozot/laravel-fcm/src/Message/PayloadDataBuilder.php
vendored
Normal file
77
vendor/brozot/laravel-fcm/src/Message/PayloadDataBuilder.php
vendored
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php namespace LaravelFCM\Message;
|
||||
|
||||
/**
|
||||
* Class PayloadDataBuilder
|
||||
*
|
||||
* Official google documentation :
|
||||
* @link http://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream-http-messages-json
|
||||
*
|
||||
* @package LaravelFCM\Message
|
||||
*/
|
||||
class PayloadDataBuilder {
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var array
|
||||
*/
|
||||
protected $data;
|
||||
|
||||
|
||||
/**
|
||||
* add data to existing data
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return PayloadDataBuilder
|
||||
*/
|
||||
public function addData(array $data)
|
||||
{
|
||||
$this->data = $this->data ?: [];
|
||||
|
||||
$this->data = array_merge($data, $this->data);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* erase data with new data
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return PayloadDataBuilder
|
||||
*/
|
||||
public function setData(array $data)
|
||||
{
|
||||
$this->data = $data;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all data
|
||||
*/
|
||||
public function removeAllData()
|
||||
{
|
||||
$this->data = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* return data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getData()
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* generate a PayloadData
|
||||
*
|
||||
* @return PayloadData new PayloadData instance
|
||||
*/
|
||||
public function build()
|
||||
{
|
||||
return new PayloadData($this);
|
||||
}
|
||||
}
|
134
vendor/brozot/laravel-fcm/src/Message/PayloadNotification.php
vendored
Normal file
134
vendor/brozot/laravel-fcm/src/Message/PayloadNotification.php
vendored
Normal file
@@ -0,0 +1,134 @@
|
||||
<?php namespace LaravelFCM\Message;
|
||||
|
||||
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
|
||||
/**
|
||||
* Class PayloadNotification
|
||||
*
|
||||
* @package LaravelFCM\Message
|
||||
*/
|
||||
class PayloadNotification implements Arrayable {
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|String
|
||||
*/
|
||||
protected $title;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|String
|
||||
*/
|
||||
protected $body;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|String
|
||||
*/
|
||||
protected $icon;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|String
|
||||
*/
|
||||
protected $sound;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|String
|
||||
*/
|
||||
protected $badge;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|String
|
||||
*/
|
||||
protected $tag;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|String
|
||||
*/
|
||||
protected $color;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|String
|
||||
*/
|
||||
protected $clickAction;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|String
|
||||
*/
|
||||
protected $bodyLocationKey;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|String
|
||||
*/
|
||||
protected $bodyLocationArgs;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|String
|
||||
*/
|
||||
protected $titleLocationKey;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|String
|
||||
*/
|
||||
protected $titleLocationArgs;
|
||||
|
||||
/**
|
||||
* PayloadNotification constructor.
|
||||
*
|
||||
* @param PayloadNotificationBuilder $builder
|
||||
*/
|
||||
public function __construct(PayloadNotificationBuilder $builder)
|
||||
{
|
||||
$this->title = $builder->getTitle();
|
||||
$this->body = $builder->getBody();
|
||||
$this->icon = $builder->getIcon();
|
||||
$this->sound = $builder->getSound();
|
||||
$this->badge = $builder->getBadge();
|
||||
$this->tag = $builder->getTag();
|
||||
$this->color = $builder->getColor();
|
||||
$this->clickAction = $builder->getClickAction();
|
||||
$this->bodyLocationKey = $builder->getBodyLocationKey();
|
||||
$this->bodyLocationArgs = $builder->getBodyLocationArgs();
|
||||
$this->titleLocationKey = $builder->getTitleLocationKey();
|
||||
$this->titleLocationArgs = $builder->getTitleLocationArgs();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* convert PayloadNotification to array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function toArray()
|
||||
{
|
||||
$notification = [
|
||||
'title' => $this->title,
|
||||
'body' => $this->body,
|
||||
'icon' => $this->icon,
|
||||
'sound' => $this->sound,
|
||||
'badge' => $this->badge,
|
||||
'tag' => $this->tag,
|
||||
'color' => $this->color,
|
||||
'click_action' => $this->clickAction,
|
||||
'body_loc_key' => $this->bodyLocationKey,
|
||||
'body_loc_args' => $this->bodyLocationArgs,
|
||||
'title_loc_key' => $this->titleLocationKey,
|
||||
'title_loc_args' => $this->titleLocationArgs,
|
||||
];
|
||||
|
||||
// remove null values
|
||||
$notification = array_filter($notification);
|
||||
|
||||
return $notification;
|
||||
}
|
||||
}
|
401
vendor/brozot/laravel-fcm/src/Message/PayloadNotificationBuilder.php
vendored
Normal file
401
vendor/brozot/laravel-fcm/src/Message/PayloadNotificationBuilder.php
vendored
Normal file
@@ -0,0 +1,401 @@
|
||||
<?php namespace LaravelFCM\Message;
|
||||
|
||||
/**
|
||||
* Class PayloadNotificationBuilder
|
||||
*
|
||||
* Official google documentation :
|
||||
* @link http://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream-http-messages-json
|
||||
*
|
||||
* @package LaravelFCM\Message
|
||||
*/
|
||||
class PayloadNotificationBuilder {
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|String
|
||||
*/
|
||||
protected $title;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|String
|
||||
*/
|
||||
protected $body;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|String
|
||||
*/
|
||||
protected $icon;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|String
|
||||
*/
|
||||
protected $sound;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|String
|
||||
*/
|
||||
protected $badge;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|String
|
||||
*/
|
||||
protected $tag;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|String
|
||||
*/
|
||||
protected $color;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|String
|
||||
*/
|
||||
protected $clickAction;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|String
|
||||
*/
|
||||
protected $bodyLocationKey;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|String
|
||||
*/
|
||||
protected $bodyLocationArgs;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|String
|
||||
*/
|
||||
protected $titleLocationKey;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var null|String
|
||||
*/
|
||||
protected $titleLocationArgs;
|
||||
|
||||
/**
|
||||
* Title must be present on android notification and ios (watch) notification
|
||||
*
|
||||
* @param String $title
|
||||
*/
|
||||
public function __construct($title = null)
|
||||
{
|
||||
$this->title = $title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates notification title. This field is not visible on iOS phones and tablets.
|
||||
* but it is required for android
|
||||
*
|
||||
* @param String $title
|
||||
*
|
||||
* @return PayloadNotificationBuilder current instance of the builder
|
||||
*/
|
||||
public function setTitle($title) {
|
||||
$this->title = $title;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates notification body text.
|
||||
*
|
||||
* @param String $body
|
||||
*
|
||||
* @return PayloadNotificationBuilder current instance of the builder
|
||||
*/
|
||||
public function setBody($body)
|
||||
{
|
||||
$this->body = $body;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Supported Android
|
||||
* Indicates notification icon. example : Sets value to myicon for drawable resource myicon.
|
||||
*
|
||||
* @param String $icon
|
||||
*
|
||||
* @return PayloadNotificationBuilder current instance of the builder
|
||||
*/
|
||||
public function setIcon($icon)
|
||||
{
|
||||
$this->icon = $icon;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates a sound to play when the device receives a notification.
|
||||
* Supports default or the filename of a sound resource bundled in the app.
|
||||
*
|
||||
* @param String $sound
|
||||
*
|
||||
* @return PayloadNotificationBuilder current instance of the builder
|
||||
*/
|
||||
public function setSound($sound)
|
||||
{
|
||||
$this->sound = $sound;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Supported Ios
|
||||
*
|
||||
* Indicates the badge on the client app home icon.
|
||||
*
|
||||
* @param String $badge
|
||||
*
|
||||
* @return PayloadNotificationBuilder current instance of the builder
|
||||
*/
|
||||
public function setBadge($badge)
|
||||
{
|
||||
$this->badge = $badge;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Supported Android
|
||||
*
|
||||
* Indicates whether each notification results in a new entry in the notification drawer on Android.
|
||||
* If not set, each request creates a new notification.
|
||||
* If set, and a notification with the same tag is already being shown, the new notification replaces the existing one in the notification drawer.
|
||||
*
|
||||
* @param String $tag
|
||||
*
|
||||
* @return PayloadNotificationBuilder current instance of the builder
|
||||
*/
|
||||
public function setTag($tag)
|
||||
{
|
||||
$this->tag = $tag;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Supported Android
|
||||
*
|
||||
* Indicates color of the icon, expressed in #rrggbb format
|
||||
*
|
||||
* @param String $color
|
||||
*
|
||||
* @return PayloadNotificationBuilder current instance of the builder
|
||||
*/
|
||||
public function setColor($color) {
|
||||
$this->color = $color;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates the action associated with a user click on the notification
|
||||
*
|
||||
* @param String $action
|
||||
*
|
||||
* @return PayloadNotificationBuilder current instance of the builder
|
||||
*/
|
||||
public function setClickAction($action)
|
||||
{
|
||||
$this->clickAction = $action;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates the key to the title string for localization.
|
||||
*
|
||||
* @param String $titleKey
|
||||
*
|
||||
* @return PayloadNotificationBuilder current instance of the builder
|
||||
*/
|
||||
public function setTitleLocationKey($titleKey)
|
||||
{
|
||||
$this->titleLocationKey = $titleKey;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates the string value to replace format specifiers in the title string for localization.
|
||||
*
|
||||
* @param mixed $titleArgs
|
||||
*
|
||||
* @return PayloadNotificationBuilder current instance of the builder
|
||||
*/
|
||||
public function setTitleLocationArgs($titleArgs)
|
||||
{
|
||||
$this->titleLocationArgs = $titleArgs;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates the key to the body string for localization.
|
||||
*
|
||||
* @param String $bodyKey
|
||||
*
|
||||
* @return PayloadNotificationBuilder current instance of the builder
|
||||
*/
|
||||
public function setBodyLocationKey($bodyKey)
|
||||
{
|
||||
$this->bodyLocationKey = $bodyKey;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates the string value to replace format specifiers in the body string for localization.
|
||||
*
|
||||
* @param mixed $bodyArgs
|
||||
*
|
||||
* @return PayloadNotificationBuilder current instance of the builder
|
||||
*/
|
||||
public function setBodyLocationArgs($bodyArgs)
|
||||
{
|
||||
$this->bodyLocationArgs = $bodyArgs;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get title
|
||||
*
|
||||
* @return null|String
|
||||
*/
|
||||
public function getTitle()
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get body
|
||||
*
|
||||
* @return null|String
|
||||
*/
|
||||
public function getBody()
|
||||
{
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Icon
|
||||
*
|
||||
* @return null|String
|
||||
*/
|
||||
public function getIcon()
|
||||
{
|
||||
return $this->icon;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Sound
|
||||
*
|
||||
* @return null|String
|
||||
*/
|
||||
public function getSound()
|
||||
{
|
||||
return $this->sound;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Badge
|
||||
*
|
||||
* @return null|String
|
||||
*/
|
||||
public function getBadge()
|
||||
{
|
||||
return $this->badge;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Tag
|
||||
*
|
||||
* @return null|String
|
||||
*/
|
||||
public function getTag()
|
||||
{
|
||||
return $this->tag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Color
|
||||
*
|
||||
* @return null|String
|
||||
*/
|
||||
public function getColor()
|
||||
{
|
||||
return $this->color;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get ClickAction
|
||||
*
|
||||
* @return null|String
|
||||
*/
|
||||
public function getClickAction()
|
||||
{
|
||||
return $this->clickAction;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get BodyLocationKey
|
||||
*
|
||||
* @return null|String
|
||||
*/
|
||||
public function getBodyLocationKey()
|
||||
{
|
||||
return $this->bodyLocationKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get BodyLocationArgs
|
||||
*
|
||||
* @return null|String|array
|
||||
*/
|
||||
public function getBodyLocationArgs()
|
||||
{
|
||||
return $this->bodyLocationArgs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get TitleLocationKey
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTitleLocationKey()
|
||||
{
|
||||
return $this->titleLocationKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* GetTitleLocationArgs
|
||||
*
|
||||
* @return null|String|array
|
||||
*/
|
||||
public function getTitleLocationArgs()
|
||||
{
|
||||
return $this->titleLocationArgs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build an PayloadNotification
|
||||
*
|
||||
* @return PayloadNotification
|
||||
*/
|
||||
public function build()
|
||||
{
|
||||
return new PayloadNotification($this);
|
||||
}
|
||||
}
|
227
vendor/brozot/laravel-fcm/src/Message/Topics.php
vendored
Normal file
227
vendor/brozot/laravel-fcm/src/Message/Topics.php
vendored
Normal file
@@ -0,0 +1,227 @@
|
||||
<?php namespace LaravelFCM\Message;
|
||||
|
||||
|
||||
use Closure;
|
||||
use LaravelFCM\Message\Exceptions\NoTopicProvidedException;
|
||||
|
||||
/**
|
||||
* Class Topics
|
||||
*
|
||||
* Create topic or a topic condition
|
||||
*
|
||||
* @package LaravelFCM\Message
|
||||
*/
|
||||
class Topics {
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var array of element in the condition
|
||||
*/
|
||||
public $conditions = [];
|
||||
|
||||
/**
|
||||
* Add a topic, this method should be called before any conditional topic
|
||||
*
|
||||
* @param string $first topicName
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function topic($first)
|
||||
{
|
||||
$this->conditions[] = compact('first');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a or condition to the precedent topic set
|
||||
*
|
||||
* Parenthesis is a closure
|
||||
*
|
||||
* Equivalent of this: **'TopicA' in topic' || 'TopicB' in topics**
|
||||
*
|
||||
* ```
|
||||
* $topic = new Topics();
|
||||
* $topic->topic('TopicA')
|
||||
* ->orTopic('TopicB');
|
||||
* ```
|
||||
*
|
||||
* Equivalent of this: **'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)**
|
||||
*
|
||||
* ```
|
||||
* $topic = new Topics();
|
||||
* $topic->topic('TopicA')
|
||||
* ->andTopic(function($condition) {
|
||||
* $condition->topic('TopicB')->orTopic('TopicC');
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* > Note: Only two operators per expression are supported by fcm
|
||||
*
|
||||
* @param string|Closure $first topicName or closure
|
||||
*
|
||||
* @return Topics
|
||||
*/
|
||||
public function orTopic($first)
|
||||
{
|
||||
return $this->on($first, ' || ');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a and condition to the precedent topic set
|
||||
*
|
||||
* Parenthesis is a closure
|
||||
*
|
||||
* Equivalent of this: **'TopicA' in topic' && 'TopicB' in topics**
|
||||
*
|
||||
* ```
|
||||
* $topic = new Topics();
|
||||
* $topic->topic('TopicA')
|
||||
* ->anTopic('TopicB');
|
||||
* ```
|
||||
*
|
||||
* Equivalent of this: **'TopicA' in topics || ('TopicB' in topics && 'TopicC' in topics)**
|
||||
*
|
||||
* ```
|
||||
* $topic = new Topics();
|
||||
* $topic->topic('TopicA')
|
||||
* ->orTopic(function($condition) {
|
||||
* $condition->topic('TopicB')->AndTopic('TopicC');
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* > Note: Only two operators per expression are supported by fcm
|
||||
*
|
||||
* @param string|Closure $first topicName or closure
|
||||
*
|
||||
* @return Topics
|
||||
*/
|
||||
public function andTopic($first)
|
||||
{
|
||||
return $this->on($first, ' && ');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @param $first
|
||||
* @param $condition
|
||||
*
|
||||
* @return $this|Topics
|
||||
*/
|
||||
private function on($first, $condition)
|
||||
{
|
||||
|
||||
if ($first instanceof Closure) {
|
||||
return $this->nest($first, $condition);
|
||||
}
|
||||
|
||||
$this->conditions[] = compact('condition', 'first');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @param Closure $callback
|
||||
* @param $condition
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function nest(Closure $callback, $condition)
|
||||
{
|
||||
$topic = new static();
|
||||
|
||||
$callback($topic);
|
||||
if (count($topic->conditions)) {
|
||||
|
||||
$open_parenthesis = '(';
|
||||
$topic = $topic->conditions;
|
||||
$close_parenthesis = ')';
|
||||
|
||||
$this->conditions[] = compact('condition', 'open_parenthesis', 'topic', 'close_parenthesis');
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform to array
|
||||
*
|
||||
* @return array|string
|
||||
* @throws NoTopicProvided
|
||||
*/
|
||||
public function build()
|
||||
{
|
||||
$this->checkIfOneTopicExist();
|
||||
|
||||
if ($this->hasOnlyOneTopic()) {
|
||||
foreach ($this->conditions[ 0] as $topic) {
|
||||
return '/topics/'.$topic;
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'condition' => $this->topicsForFcm($this->conditions)
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @param $conditions
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function topicsForFcm($conditions)
|
||||
{
|
||||
$condition = "";
|
||||
foreach ($conditions as $partial) {
|
||||
if (array_key_exists('condition', $partial)) {
|
||||
$condition .= $partial['condition'];
|
||||
}
|
||||
|
||||
if (array_key_exists('first', $partial)) {
|
||||
$topic = $partial['first'];
|
||||
$condition .= "'$topic' in topics";
|
||||
}
|
||||
|
||||
if (array_key_exists('open_parenthesis', $partial)) {
|
||||
$condition .= $partial['open_parenthesis'];
|
||||
}
|
||||
|
||||
if (array_key_exists('topic', $partial)) {
|
||||
$condition .= $this->topicsForFcm($partial['topic']);
|
||||
}
|
||||
|
||||
if (array_key_exists('close_parenthesis', $partial)) {
|
||||
$condition .= $partial['close_parenthesis'];
|
||||
}
|
||||
}
|
||||
|
||||
return $condition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if only one topic was set
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasOnlyOneTopic()
|
||||
{
|
||||
return count($this->conditions) == 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @throws NoTopicProvidedException
|
||||
*/
|
||||
private function checkIfOneTopicExist()
|
||||
{
|
||||
if (!count($this->conditions)) {
|
||||
throw new NoTopicProvidedException('At least one topic must be provided');
|
||||
}
|
||||
}
|
||||
}
|
63
vendor/brozot/laravel-fcm/src/Request/BaseRequest.php
vendored
Normal file
63
vendor/brozot/laravel-fcm/src/Request/BaseRequest.php
vendored
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php namespace LaravelFCM\Request;
|
||||
|
||||
/**
|
||||
* Class BaseRequest
|
||||
*
|
||||
* @package LaravelFCM\Request
|
||||
*/
|
||||
abstract class BaseRequest {
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var \GuzzleHttp\Client
|
||||
*/
|
||||
protected $client;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var array
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* BaseRequest constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->config = app('config')->get('fcm.http', []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the header for the request
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function buildRequestHeader()
|
||||
{
|
||||
return [
|
||||
'Authorization' => "key=".$this->config['server_key'],
|
||||
'Content-Type' => "application/json",
|
||||
'project_id' => $this->config['sender_id']
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the body of the request
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected abstract function buildBody();
|
||||
|
||||
/**
|
||||
* Return the request in array form
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function build()
|
||||
{
|
||||
return [
|
||||
'headers' => $this->buildRequestHeader(),
|
||||
'json' => $this->buildBody()
|
||||
];
|
||||
}
|
||||
}
|
66
vendor/brozot/laravel-fcm/src/Request/GroupRequest.php
vendored
Normal file
66
vendor/brozot/laravel-fcm/src/Request/GroupRequest.php
vendored
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php namespace LaravelFCM\Request;
|
||||
|
||||
/**
|
||||
* Class GroupRequest
|
||||
*
|
||||
* @package LaravelFCM\Request
|
||||
*/
|
||||
class GroupRequest extends BaseRequest{
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var string
|
||||
*/
|
||||
protected $operation;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var string
|
||||
*/
|
||||
protected $notificationKeyName;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var string
|
||||
*/
|
||||
protected $notificationKey;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var array
|
||||
*/
|
||||
protected $registrationIds;
|
||||
|
||||
/**
|
||||
* GroupRequest constructor.
|
||||
*
|
||||
* @param $operation
|
||||
* @param $notificationKeyName
|
||||
* @param $notificationKey
|
||||
* @param $registrationIds
|
||||
*/
|
||||
public function __construct($operation, $notificationKeyName, $notificationKey, $registrationIds)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->operation = $operation;
|
||||
$this->notificationKeyName = $notificationKeyName;
|
||||
$this->notificationKey = $notificationKey;
|
||||
$this->registrationIds = $registrationIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the header for the request
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function buildBody()
|
||||
{
|
||||
return [
|
||||
'operation' => $this->operation,
|
||||
'notification_key_name' => $this->notificationKeyName,
|
||||
'notification_key' => $this->notificationKey,
|
||||
'registration_ids' => $this->registrationIds
|
||||
];
|
||||
}
|
||||
}
|
148
vendor/brozot/laravel-fcm/src/Request/Request.php
vendored
Normal file
148
vendor/brozot/laravel-fcm/src/Request/Request.php
vendored
Normal file
@@ -0,0 +1,148 @@
|
||||
<?php namespace LaravelFCM\Request;
|
||||
|
||||
use LaravelFCM\Message\Options;
|
||||
use LaravelFCM\Message\PayloadData;
|
||||
use LaravelFCM\Message\PayloadNotification;
|
||||
use LaravelFCM\Message\Topics;
|
||||
|
||||
/**
|
||||
* Class Request
|
||||
*
|
||||
* @package LaravelFCM\Request
|
||||
*/
|
||||
class Request extends BaseRequest{
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var string|array
|
||||
*/
|
||||
protected $to;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var Options
|
||||
*/
|
||||
protected $options;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var PayloadNotification
|
||||
*/
|
||||
protected $notification;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var PayloadData
|
||||
*/
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var Topics|null
|
||||
*/
|
||||
protected $topic;
|
||||
|
||||
/**
|
||||
* Request constructor.
|
||||
*
|
||||
* @param $to
|
||||
* @param Options $options
|
||||
* @param PayloadNotification $notification
|
||||
* @param PayloadData $data
|
||||
* @param Topics|null $topic
|
||||
*/
|
||||
public function __construct($to, Options $options = null, PayloadNotification $notification = null, PayloadData $data = null, Topics $topic = null)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->to = $to;
|
||||
$this->options = $options;
|
||||
$this->notification = $notification;
|
||||
$this->data = $data;
|
||||
$this->topic = $topic;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the body for the request
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function buildBody()
|
||||
{
|
||||
$message = [
|
||||
'to' => $this->getTo(),
|
||||
'registration_ids' => $this->getRegistrationIds(),
|
||||
'notification' => $this->getNotification(),
|
||||
'data' => $this->getData()
|
||||
];
|
||||
|
||||
$message = array_merge($message, $this->getOptions());
|
||||
|
||||
// remove null entries
|
||||
return array_filter($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* get to key transformed
|
||||
*
|
||||
* @return array|null|string
|
||||
*/
|
||||
protected function getTo()
|
||||
{
|
||||
$to = is_array($this->to) ? null : $this->to;
|
||||
|
||||
if ($this->topic && $this->topic->hasOnlyOneTopic()) {
|
||||
$to = $this->topic->build();
|
||||
}
|
||||
|
||||
return $to;
|
||||
}
|
||||
|
||||
/**
|
||||
* get registrationIds transformed
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
protected function getRegistrationIds()
|
||||
{
|
||||
return is_array($this->to) ? $this->to : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* get Options transformed
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getOptions()
|
||||
{
|
||||
$options = $this->options ? $this->options->toArray() : [];
|
||||
|
||||
if ($this->topic && !$this->topic->hasOnlyOneTopic()) {
|
||||
|
||||
$options = array_merge($options, $this->topic->build());
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* get notification transformed
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
protected function getNotification()
|
||||
{
|
||||
return $this->notification ? $this->notification->toArray() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* get data transformed
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
protected function getData()
|
||||
{
|
||||
return $this->data ? $this->data->toArray() : null;
|
||||
}
|
||||
|
||||
}
|
71
vendor/brozot/laravel-fcm/src/Response/BaseResponse.php
vendored
Normal file
71
vendor/brozot/laravel-fcm/src/Response/BaseResponse.php
vendored
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php namespace LaravelFCM\Response;
|
||||
|
||||
use GuzzleHttp\Psr7\Response as GuzzleResponse;
|
||||
use LaravelFCM\Response\Exceptions\ServerResponseException;
|
||||
use LaravelFCM\Response\Exceptions\InvalidRequestException;
|
||||
use LaravelFCM\Response\Exceptions\UnauthorizedRequestException;
|
||||
|
||||
/**
|
||||
* Class BaseResponse
|
||||
*
|
||||
* @package LaravelFCM\Response
|
||||
*/
|
||||
abstract class BaseResponse {
|
||||
|
||||
const SUCCESS = 'success';
|
||||
const FAILURE = 'failure';
|
||||
const ERROR = "error";
|
||||
const MESSAGE_ID = "message_id";
|
||||
|
||||
/**
|
||||
* BaseResponse constructor.
|
||||
*
|
||||
* @param GuzzleResponse $response
|
||||
*/
|
||||
public function __construct(GuzzleResponse $response)
|
||||
{
|
||||
$this->isJsonResponse($response);
|
||||
|
||||
$responseInJson = json_decode($response->getBody(), true);
|
||||
$this->parseResponse($responseInJson);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the response given by fcm is parsable
|
||||
*
|
||||
* @param GuzzleResponse $response
|
||||
*
|
||||
* @throws InvalidRequestException
|
||||
* @throws ServerResponseException
|
||||
* @throws UnauthorizedRequestException
|
||||
*/
|
||||
private function isJsonResponse(GuzzleResponse $response)
|
||||
{
|
||||
if ($response->getStatusCode() == 200) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($response->getStatusCode() == 400) {
|
||||
throw new InvalidRequestException($response);
|
||||
}
|
||||
|
||||
if ($response->getStatusCode() == 401) {
|
||||
throw new UnauthorizedRequestException($response);
|
||||
}
|
||||
|
||||
throw new ServerResponseException($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* parse the response
|
||||
*
|
||||
* @param array $responseInJson
|
||||
*/
|
||||
protected abstract function parseResponse($responseInJson);
|
||||
|
||||
/**
|
||||
* Log the response
|
||||
*/
|
||||
protected abstract function logResponse();
|
||||
|
||||
}
|
410
vendor/brozot/laravel-fcm/src/Response/DownstreamResponse.php
vendored
Normal file
410
vendor/brozot/laravel-fcm/src/Response/DownstreamResponse.php
vendored
Normal file
@@ -0,0 +1,410 @@
|
||||
<?php namespace LaravelFCM\Response;
|
||||
|
||||
use Monolog\Logger;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use GuzzleHttp\Psr7\Response as GuzzleResponse;
|
||||
|
||||
/**
|
||||
* Class DownstreamResponse
|
||||
*
|
||||
* @package LaravelFCM\Response
|
||||
*/
|
||||
class DownstreamResponse extends BaseResponse {
|
||||
|
||||
const MULTICAST_ID = 'multicast_id';
|
||||
const CANONICAL_IDS = "canonical_ids";
|
||||
const RESULTS = "results";
|
||||
|
||||
const MISSING_REGISTRATION = "MissingRegistration";
|
||||
const MESSAGE_ID = "message_id";
|
||||
const REGISTRATION_ID = "registration_id";
|
||||
const NOT_REGISTERED = "NotRegistered";
|
||||
const INVALID_REGISTRATION = "InvalidRegistration";
|
||||
const UNAVAILABLE = "Unavailable";
|
||||
const DEVICE_MESSAGE_RATE_EXCEEDED = "DeviceMessageRateExceeded";
|
||||
const INTERNAL_SERVER_ERROR = "InternalServerError";
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $numberTokensSuccess = 0;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $numberTokensFailure = 0;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $numberTokenModify = 0;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @var
|
||||
*/
|
||||
protected $messageId;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $tokensToDelete = [];
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
|
||||
protected $tokensToModify = [];
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
|
||||
protected $tokensToRetry = [];
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $tokensWithError = [];
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var bool
|
||||
*/
|
||||
protected $hasMissingToken = false;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $tokens;
|
||||
|
||||
/**
|
||||
* DownstreamResponse constructor.
|
||||
*
|
||||
* @param GuzzleResponse $response
|
||||
* @param $tokens
|
||||
*/
|
||||
public function __construct(GuzzleResponse $response, $tokens)
|
||||
{
|
||||
$this->tokens = is_string($tokens) ? [$tokens] : $tokens;
|
||||
|
||||
parent::__construct($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the response
|
||||
*
|
||||
* @param $responseInJson
|
||||
*/
|
||||
protected function parseResponse($responseInJson)
|
||||
{
|
||||
$this->parse($responseInJson);
|
||||
|
||||
if ($this->needResultParsing($responseInJson)) {
|
||||
$this->parseResult($responseInJson);
|
||||
}
|
||||
|
||||
$this->logResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @param $responseInJson
|
||||
*/
|
||||
private function parse($responseInJson)
|
||||
{
|
||||
if (array_key_exists(self::MULTICAST_ID, $responseInJson)) {
|
||||
$this->messageId;
|
||||
}
|
||||
|
||||
if (array_key_exists(self::SUCCESS, $responseInJson)) {
|
||||
$this->numberTokensSuccess = $responseInJson[self::SUCCESS];
|
||||
}
|
||||
|
||||
if (array_key_exists(self::FAILURE, $responseInJson)) {
|
||||
$this->numberTokensFailure = $responseInJson[self::FAILURE];
|
||||
}
|
||||
|
||||
if (array_key_exists(self::CANONICAL_IDS, $responseInJson)) {
|
||||
$this->numberTokenModify = $responseInJson[self::CANONICAL_IDS];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @param $responseInJson
|
||||
*/
|
||||
private function parseResult($responseInJson)
|
||||
{
|
||||
foreach ($responseInJson[self::RESULTS] as $index => $result) {
|
||||
|
||||
if (!$this->isSent($result)) {
|
||||
if (!$this->needToBeModify($index, $result)) {
|
||||
if (!$this->needToBeDeleted($index, $result) && !$this->needToResend($index, $result) && !$this->checkMissingToken($result)) {
|
||||
$this->needToAddError($index, $result);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @param $responseInJson
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function needResultParsing($responseInJson)
|
||||
{
|
||||
return array_key_exists(self::RESULTS, $responseInJson) && ($this->numberTokensFailure > 0 || $this->numberTokenModify > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @param $results
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isSent($results)
|
||||
{
|
||||
return (array_key_exists(self::MESSAGE_ID, $results) && !array_key_exists(self::REGISTRATION_ID, $results));
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @param $index
|
||||
* @param $result
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function needToBeModify($index, $result)
|
||||
{
|
||||
if (array_key_exists(self::MESSAGE_ID, $result) && array_key_exists(self::REGISTRATION_ID, $result)) {
|
||||
if ($this->tokens[$index]) {
|
||||
$this->tokensToModify[$this->tokens[$index]] = $result[self::REGISTRATION_ID];
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @param $index
|
||||
* @param $result
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function needToBeDeleted($index, $result)
|
||||
{
|
||||
if (array_key_exists(self::ERROR, $result) &&
|
||||
(in_array(self::NOT_REGISTERED, $result) || in_array(self::INVALID_REGISTRATION, $result))) {
|
||||
|
||||
if ($this->tokens[$index]) {
|
||||
$this->tokensToDelete[] = $this->tokens[$index];
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @param $index
|
||||
* @param $result
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function needToResend($index, $result)
|
||||
{
|
||||
if (array_key_exists(self::ERROR, $result) && (in_array(self::UNAVAILABLE, $result) || in_array(self::DEVICE_MESSAGE_RATE_EXCEEDED, $result) || in_array(self::INTERNAL_SERVER_ERROR, $result))) {
|
||||
if ($this->tokens[$index]) {
|
||||
$this->tokensToRetry[] = $this->tokens[$index];
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @param $result
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function checkMissingToken($result)
|
||||
{
|
||||
$hasMissingToken = (array_key_exists(self::ERROR, $result) && in_array(self::MISSING_REGISTRATION, $result));
|
||||
|
||||
$this->hasMissingToken = (boolean) ($this->hasMissingToken | $hasMissingToken);
|
||||
|
||||
return $hasMissingToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @param $index
|
||||
* @param $result
|
||||
*/
|
||||
private function needToAddError($index, $result)
|
||||
{
|
||||
if (array_key_exists(self::ERROR, $result)) {
|
||||
if ($this->tokens[$index]) {
|
||||
$this->tokensWithError[$this->tokens[$index]] = $result[self::ERROR];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
protected function logResponse()
|
||||
{
|
||||
$logger = new Logger('Laravel-FCM');
|
||||
$logger->pushHandler(new StreamHandler(storage_path('logs/laravel-fcm.log')));
|
||||
|
||||
$logMessage = "notification send to ".count($this->tokens)." devices".PHP_EOL;
|
||||
$logMessage .= "success: ".$this->numberTokensSuccess.PHP_EOL;
|
||||
$logMessage .= "failures: ".$this->numberTokensFailure.PHP_EOL;
|
||||
$logMessage .= "number of modified token : ".$this->numberTokenModify.PHP_EOL;
|
||||
|
||||
$logger->info($logMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge two response
|
||||
*
|
||||
* @param DownstreamResponse $response
|
||||
*/
|
||||
public function merge(DownstreamResponse $response)
|
||||
{
|
||||
$this->numberTokensSuccess += $response->numberSuccess();
|
||||
$this->numberTokensFailure += $response->numberFailure();
|
||||
$this->numberTokenModify += $response->numberModification();
|
||||
|
||||
$this->tokensToDelete = array_merge($this->tokensToDelete, $response->tokensToDelete());
|
||||
$this->tokensToModify = array_merge($this->tokensToModify, $response->tokensToModify());
|
||||
$this->tokensToRetry = array_merge($this->tokensToRetry, $response->tokensToRetry());
|
||||
$this->tokensWithError = array_merge($this->tokensWithError, $response->tokensWithError());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of device reached with success
|
||||
* @return int
|
||||
*/
|
||||
public function numberSuccess()
|
||||
{
|
||||
return $this->numberTokensSuccess;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of device which thrown an error
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function numberFailure()
|
||||
{
|
||||
return $this->numberTokensFailure;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of device that you need to modify their token
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function numberModification()
|
||||
{
|
||||
return $this->numberTokenModify;
|
||||
}
|
||||
|
||||
/**
|
||||
* get token to delete
|
||||
*
|
||||
* remove all tokens returned by this method in your database
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function tokensToDelete()
|
||||
{
|
||||
return $this->tokensToDelete;
|
||||
}
|
||||
|
||||
/**
|
||||
* get token to modify
|
||||
*
|
||||
* key: oldToken
|
||||
* value: new token
|
||||
*
|
||||
* find the old token in your database and replace it with the new one
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function tokensToModify()
|
||||
{
|
||||
return $this->tokensToModify;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get tokens that you should resend using exponential backoof
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function tokensToRetry()
|
||||
{
|
||||
return $this->tokensToRetry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get tokens that thrown an error
|
||||
*
|
||||
* key : token
|
||||
* value : error
|
||||
*
|
||||
* In production, remove these tokens from you database
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function tokensWithError()
|
||||
{
|
||||
return $this->tokensWithError;
|
||||
}
|
||||
|
||||
/**
|
||||
* check if missing tokens was given to the request
|
||||
* If true, remove all the empty token in your database
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasMissingToken()
|
||||
{
|
||||
return $this->hasMissingToken;
|
||||
}
|
||||
}
|
25
vendor/brozot/laravel-fcm/src/Response/Exceptions/InvalidRequestException.php
vendored
Normal file
25
vendor/brozot/laravel-fcm/src/Response/Exceptions/InvalidRequestException.php
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php namespace LaravelFCM\Response\Exceptions;
|
||||
|
||||
use Exception;
|
||||
use GuzzleHttp\Psr7\Response as GuzzleResponse;
|
||||
|
||||
/**
|
||||
* Class InvalidRequestException
|
||||
*
|
||||
* @package LaravelFCM\Response\Exceptions
|
||||
*/
|
||||
class InvalidRequestException extends Exception {
|
||||
|
||||
/**
|
||||
* InvalidRequestException constructor.
|
||||
*
|
||||
* @param GuzzleResponse $response
|
||||
*/
|
||||
public function __construct(GuzzleResponse $response)
|
||||
{
|
||||
$code = $response->getStatusCode();
|
||||
$responseBody = $response->getBody()->getContents();
|
||||
|
||||
parent::__construct($responseBody, $code);
|
||||
}
|
||||
}
|
36
vendor/brozot/laravel-fcm/src/Response/Exceptions/ServerResponseException.php
vendored
Normal file
36
vendor/brozot/laravel-fcm/src/Response/Exceptions/ServerResponseException.php
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php namespace LaravelFCM\Response\Exceptions;
|
||||
|
||||
use Exception;
|
||||
use GuzzleHttp\Psr7\Response as GuzzleResponse;
|
||||
|
||||
/**
|
||||
* Class ServerResponseException
|
||||
*
|
||||
* @package LaravelFCM\Response\Exceptions
|
||||
*/
|
||||
class ServerResponseException extends Exception {
|
||||
|
||||
/**
|
||||
* retry after
|
||||
* @var int
|
||||
*/
|
||||
public $retryAfter;
|
||||
|
||||
/**
|
||||
* ServerResponseException constructor.
|
||||
*
|
||||
* @param GuzzleResponse $response
|
||||
*/
|
||||
public function __construct(GuzzleResponse $response)
|
||||
{
|
||||
$code = $response->getStatusCode();
|
||||
$responseHeader = $response->getHeaders();
|
||||
$responseBody = $response->getBody()->getContents();
|
||||
|
||||
if (array_keys($responseHeader, "Retry-After")) {
|
||||
$this->retryAfter = $responseHeader["Retry-After"];
|
||||
}
|
||||
|
||||
parent::__construct($responseBody, $code);
|
||||
}
|
||||
}
|
24
vendor/brozot/laravel-fcm/src/Response/Exceptions/UnauthorizedRequestException.php
vendored
Normal file
24
vendor/brozot/laravel-fcm/src/Response/Exceptions/UnauthorizedRequestException.php
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php namespace LaravelFCM\Response\Exceptions;
|
||||
|
||||
use Exception;
|
||||
use GuzzleHttp\Psr7\Response as GuzzleResponse;
|
||||
|
||||
/**
|
||||
* Class UnauthorizedRequestException
|
||||
*
|
||||
* @package LaravelFCM\Response\Exceptions
|
||||
*/
|
||||
class UnauthorizedRequestException extends Exception {
|
||||
|
||||
/**
|
||||
* UnauthorizedRequestException constructor.
|
||||
*
|
||||
* @param GuzzleResponse $response
|
||||
*/
|
||||
public function __construct(GuzzleResponse $response)
|
||||
{
|
||||
$code = $response->getStatusCode();
|
||||
|
||||
parent::__construct('FCM_SENDER_ID or FCM_SERVER_KEY are invalid', $code);
|
||||
}
|
||||
}
|
137
vendor/brozot/laravel-fcm/src/Response/GroupResponse.php
vendored
Normal file
137
vendor/brozot/laravel-fcm/src/Response/GroupResponse.php
vendored
Normal file
@@ -0,0 +1,137 @@
|
||||
<?php namespace LaravelFCM\Response;
|
||||
|
||||
use Monolog\Logger;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use GuzzleHttp\Psr7\Response as GuzzleResponse;
|
||||
|
||||
/**
|
||||
* Class GroupResponse
|
||||
*
|
||||
* @package LaravelFCM\Response
|
||||
*/
|
||||
class GroupResponse extends BaseResponse {
|
||||
|
||||
const FAILED_REGISTRATION_IDS = "failed_registration_ids";
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var int
|
||||
*/
|
||||
protected $numberTokensSuccess = 0;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var int
|
||||
*/
|
||||
protected $numberTokensFailure = 0;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var array
|
||||
*/
|
||||
protected $tokensFailed = [];
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var string
|
||||
*/
|
||||
protected $to;
|
||||
|
||||
/**
|
||||
* GroupResponse constructor.
|
||||
*
|
||||
* @param GuzzleResponse $response
|
||||
* @param $to
|
||||
*/
|
||||
public function __construct(GuzzleResponse $response, $to)
|
||||
{
|
||||
$this->to = $to;
|
||||
parent::__construct($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* parse the response
|
||||
*
|
||||
* @param $responseInJson
|
||||
*/
|
||||
protected function parseResponse($responseInJson)
|
||||
{
|
||||
if ($this->parse($responseInJson)) {
|
||||
$this->parseFailed($responseInJson);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Log the response
|
||||
*/
|
||||
protected function logResponse()
|
||||
{
|
||||
$logger = new Logger('Laravel-FCM');
|
||||
$logger->pushHandler(new StreamHandler(storage_path('logs/laravel-fcm.log')));
|
||||
|
||||
$logMessage = "notification send to group: $this->to";
|
||||
$logMessage .= "with $this->numberTokensSuccess success and $this->Failure failure";
|
||||
|
||||
$logger->info($logMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @param $responseInJson
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function parse($responseInJson)
|
||||
{
|
||||
if (array_key_exists(self::SUCCESS, $responseInJson)) {
|
||||
$this->numberTokensSuccess = $responseInJson[self::SUCCESS];
|
||||
}
|
||||
if (array_key_exists(self::FAILURE, $responseInJson)) {
|
||||
$this->numberTokensFailure = $responseInJson[self::FAILURE];
|
||||
}
|
||||
|
||||
return $this->numberTokensFailure > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @param $responseInJson
|
||||
*/
|
||||
private function parseFailed($responseInJson)
|
||||
{
|
||||
if (array_key_exists(self::FAILED_REGISTRATION_IDS, $responseInJson)) {
|
||||
foreach ($responseInJson[self::FAILED_REGISTRATION_IDS] as $registrationId) {
|
||||
$this->tokensFailed[] = $registrationId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of device reached with success
|
||||
* @return int
|
||||
*/
|
||||
public function numberSuccess()
|
||||
{
|
||||
return $this->numberTokensSuccess;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of device which thrown an error
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function numberFailure()
|
||||
{
|
||||
return $this->numberTokensFailure;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all token in group that fcm cannot reach
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function tokensFailed()
|
||||
{
|
||||
return $this->tokensFailed;
|
||||
}
|
||||
}
|
142
vendor/brozot/laravel-fcm/src/Response/TopicResponse.php
vendored
Normal file
142
vendor/brozot/laravel-fcm/src/Response/TopicResponse.php
vendored
Normal file
@@ -0,0 +1,142 @@
|
||||
<?php namespace LaravelFCM\Response;
|
||||
|
||||
use LaravelFCM\Message\Topics;
|
||||
use Monolog\Logger;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use GuzzleHttp\Psr7\Response as GuzzleResponse;
|
||||
|
||||
/**
|
||||
* Class TopicResponse
|
||||
*
|
||||
* @package LaravelFCM\Response
|
||||
*/
|
||||
class TopicResponse extends BaseResponse {
|
||||
|
||||
const LIMIT_RATE_TOPICS_EXCEEDED = "TopicsMessageRateExceeded";
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var string
|
||||
*/
|
||||
protected $topic;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var string
|
||||
*/
|
||||
protected $messageId;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var string
|
||||
*/
|
||||
protected $error;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @var bool
|
||||
*/
|
||||
protected $needRetry = false;
|
||||
|
||||
/**
|
||||
* TopicResponse constructor.
|
||||
*
|
||||
* @param GuzzleResponse $response
|
||||
* @param Topics $topic
|
||||
*/
|
||||
public function __construct(GuzzleResponse $response, Topics $topic)
|
||||
{
|
||||
$this->topic = $topic;
|
||||
parent::__construct($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* parse the response
|
||||
*
|
||||
* @param $responseInJson
|
||||
*/
|
||||
protected function parseResponse($responseInJson)
|
||||
{
|
||||
if (!$this->parseSuccess($responseInJson)) {
|
||||
$this->parseError($responseInJson);
|
||||
}
|
||||
|
||||
$this->logResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @param $responseInJson
|
||||
*/
|
||||
private function parseSuccess($responseInJson)
|
||||
{
|
||||
if (array_key_exists(self::MESSAGE_ID, $responseInJson)) {
|
||||
$this->messageId = $responseInJson[ self::MESSAGE_ID ];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @param $responseInJson
|
||||
*/
|
||||
private function parseError($responseInJson)
|
||||
{
|
||||
if (array_key_exists(self::ERROR, $responseInJson)) {
|
||||
if (in_array(self::LIMIT_RATE_TOPICS_EXCEEDED, $responseInJson)) {
|
||||
$this->needRetry = true;
|
||||
}
|
||||
|
||||
$this->error = $responseInJson[ self::ERROR ];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Log the response
|
||||
*/
|
||||
protected function logResponse()
|
||||
{
|
||||
$logger = new Logger('Laravel-FCM');
|
||||
$logger->pushHandler(new StreamHandler(storage_path('logs/laravel-fcm.log')));
|
||||
|
||||
$topic = $this->topic->build();
|
||||
$logMessage = "notification send to topic: $topic";
|
||||
if ($this->messageId) {
|
||||
$logMessage .= "with success (message-id : $this->messageId)";
|
||||
}
|
||||
else {
|
||||
$logMessage .= "with error (error : $this->error)";
|
||||
}
|
||||
|
||||
$logger->info($logMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* true if topic sent with success
|
||||
* @return bool
|
||||
*/
|
||||
public function isSuccess()
|
||||
{
|
||||
return (bool) $this->messageId;
|
||||
}
|
||||
|
||||
/**
|
||||
* return error message
|
||||
* you should test if it's necessary to resent it
|
||||
*
|
||||
* @return string error
|
||||
*/
|
||||
public function error()
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
/**
|
||||
* return true if it's necessary resent it using exponential backoff
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function shouldRetry()
|
||||
{
|
||||
return $this->needRetry;
|
||||
}
|
||||
}
|
45
vendor/brozot/laravel-fcm/src/Sender/BaseSender.php
vendored
Normal file
45
vendor/brozot/laravel-fcm/src/Sender/BaseSender.php
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php namespace LaravelFCM\Sender;
|
||||
|
||||
/**
|
||||
* Class BaseSender
|
||||
*
|
||||
* @package LaravelFCM\Sender
|
||||
*/
|
||||
abstract class BaseSender {
|
||||
|
||||
/**
|
||||
* Guzzle Client
|
||||
* @var \Illuminate\Foundation\Application|mixed
|
||||
*/
|
||||
protected $client;
|
||||
|
||||
/**
|
||||
* configuration
|
||||
* @var array
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* url
|
||||
* @var mixed
|
||||
*/
|
||||
protected $url;
|
||||
|
||||
/**
|
||||
* BaseSender constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->client = app('fcm.client');
|
||||
$this->config = app('config')->get('fcm.http', []);
|
||||
|
||||
$this->url = $this->getUrl();
|
||||
}
|
||||
|
||||
/**
|
||||
* get the url
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected abstract function getUrl();
|
||||
}
|
108
vendor/brozot/laravel-fcm/src/Sender/FCMGroup.php
vendored
Normal file
108
vendor/brozot/laravel-fcm/src/Sender/FCMGroup.php
vendored
Normal file
@@ -0,0 +1,108 @@
|
||||
<?php namespace LaravelFCM\Sender;
|
||||
|
||||
use LaravelFCM\Request\GroupRequest;
|
||||
use GuzzleHttp\Psr7\Response as GuzzleResponse;
|
||||
|
||||
/**
|
||||
* Class FCMGroup
|
||||
*
|
||||
* @package LaravelFCM\Sender
|
||||
*/
|
||||
class FCMGroup extends BaseSender {
|
||||
|
||||
const CREATE = "create";
|
||||
const ADD = "add";
|
||||
const REMOVE = "remove";
|
||||
|
||||
/**
|
||||
* Create a group
|
||||
*
|
||||
* @param $notificationKeyName
|
||||
* @param array $registrationIds
|
||||
*
|
||||
* @return null
|
||||
*/
|
||||
public function createGroup($notificationKeyName, array $registrationIds)
|
||||
{
|
||||
$request = new GroupRequest(self::CREATE, $notificationKeyName, null, $registrationIds);
|
||||
|
||||
$response = $this->client->post($this->url, $request->build());
|
||||
|
||||
return $this->getNotificationToken($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* add registrationId to a existing group
|
||||
*
|
||||
* @param $notificationKeyName
|
||||
* @param $notificationKey
|
||||
* @param array $registrationIds registrationIds to add
|
||||
*
|
||||
* @return null
|
||||
*/
|
||||
public function addToGroup($notificationKeyName, $notificationKey, array $registrationIds)
|
||||
{
|
||||
$request = new GroupRequest(self::ADD, $notificationKeyName, $notificationKey, $registrationIds);
|
||||
$response = $this->client->post($this->url, $request->build());
|
||||
|
||||
return $this->getNotificationToken($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* remove registrationId to a existing group
|
||||
*
|
||||
* >Note: if you remove all registrationIds the group is automatically deleted
|
||||
*
|
||||
* @param $notificationKeyName
|
||||
* @param $notificationKey
|
||||
* @param array $registeredIds registrationIds to remove
|
||||
*
|
||||
* @return null
|
||||
*/
|
||||
public function removeFromGroup($notificationKeyName, $notificationKey, array $registeredIds)
|
||||
{
|
||||
$request = new GroupRequest(self::REMOVE, $notificationKeyName, $notificationKey, $registeredIds);
|
||||
$response = $this->client->post($this->url, $request->build());
|
||||
|
||||
return $this->getNotificationToken($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @param GuzzleResponse $response
|
||||
*
|
||||
* @return null
|
||||
*/
|
||||
private function getNotificationToken(GuzzleResponse $response)
|
||||
{
|
||||
if ($this->isValidResponse($response)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$json = json_decode($response->getBody(), true);
|
||||
return $json['notification_key'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @param $response
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isValidResponse(GuzzleResponse $response)
|
||||
{
|
||||
return $response->getReasonPhrase() != 'OK' || $response->getStatusCode() != 200;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the url
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getUrl()
|
||||
{
|
||||
return $this->config['server_group_url'];
|
||||
}
|
||||
}
|
131
vendor/brozot/laravel-fcm/src/Sender/FCMSender.php
vendored
Normal file
131
vendor/brozot/laravel-fcm/src/Sender/FCMSender.php
vendored
Normal file
@@ -0,0 +1,131 @@
|
||||
<?php namespace LaravelFCM\Sender;
|
||||
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use LaravelFCM\FCMRequest;
|
||||
use LaravelFCM\Message\Options;
|
||||
use LaravelFCM\Message\PayloadData;
|
||||
use LaravelFCM\Message\PayloadNotification;
|
||||
use \GuzzleHttp\Psr7\Response as GuzzleResponse;
|
||||
use LaravelFCM\Message\Topics;
|
||||
use LaravelFCM\Request\Request;
|
||||
use LaravelFCM\Response\DownstreamResponse;
|
||||
use LaravelFCM\Response\GroupResponse;
|
||||
use LaravelFCM\Response\TopicResponse;
|
||||
|
||||
/**
|
||||
* Class FCMSender
|
||||
*
|
||||
* @package LaravelFCM\Sender
|
||||
*/
|
||||
class FCMSender extends BaseSender {
|
||||
|
||||
const MAX_TOKEN_PER_REQUEST = 1000;
|
||||
|
||||
/**
|
||||
* send a downstream message to
|
||||
*
|
||||
* - a unique device with is registration Token
|
||||
* - or to multiples devices with an array of registrationIds
|
||||
*
|
||||
* @param String|array $to
|
||||
* @param Options|null $options
|
||||
* @param PayloadNotification|null $notification
|
||||
* @param PayloadData|null $data
|
||||
*
|
||||
* @return DownstreamResponse|null
|
||||
*
|
||||
*/
|
||||
public function sendTo($to, Options $options = null, PayloadNotification $notification = null, PayloadData $data = null)
|
||||
{
|
||||
$response = null;
|
||||
|
||||
if (is_array($to)) {
|
||||
$partialTokens = array_chunk($to, self::MAX_TOKEN_PER_REQUEST, false);
|
||||
foreach ($partialTokens as $tokens) {
|
||||
$request = new Request($tokens, $options, $notification, $data);
|
||||
|
||||
try {
|
||||
$responseGuzzle = $this->client->post($this->url, $request->build());
|
||||
}
|
||||
catch (ClientException $e) {
|
||||
$responseGuzzle = $e->getResponse();
|
||||
}
|
||||
|
||||
$responsePartial = new DownstreamResponse($responseGuzzle, $tokens);
|
||||
if (!$response) {
|
||||
$response = $responsePartial;
|
||||
}
|
||||
else {
|
||||
$response->merge($responsePartial);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
$request = new Request($to, $options, $notification, $data);
|
||||
$response = $this->client->post($this->url, $request->build());
|
||||
$response = new DownstreamResponse($response, $to);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message to a group of devices identified with them notification key
|
||||
*
|
||||
* @param $notificationKey
|
||||
* @param Options|null $options
|
||||
* @param PayloadNotification|null $notification
|
||||
* @param PayloadData|null $data
|
||||
*
|
||||
* @return GroupResponse
|
||||
*/
|
||||
public function sendToGroup($notificationKey, Options $options = null, PayloadNotification $notification = null, PayloadData $data = null)
|
||||
{
|
||||
$request = new Request($notificationKey, $options, $notification, $data);
|
||||
|
||||
try {
|
||||
$response = $this->client->post($this->url, $request->build());
|
||||
}
|
||||
catch (ClientException $e) {
|
||||
$response = $e->getResponse();
|
||||
}
|
||||
|
||||
return new GroupResponse($response, $notificationKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send message devices registered at a or more topics
|
||||
*
|
||||
* @param Topics $topics
|
||||
* @param Options|null $options
|
||||
* @param PayloadNotification|null $notification
|
||||
* @param PayloadData|null $data
|
||||
*
|
||||
* @return TopicResponse
|
||||
*/
|
||||
public function sendToTopic(Topics $topics, Options $options = null, PayloadNotification $notification = null, PayloadData $data = null)
|
||||
{
|
||||
|
||||
$request = new Request(null, $options, $notification, $data, $topics);
|
||||
try {
|
||||
$response = $this->client->post($this->url, $request->build());
|
||||
}
|
||||
catch (ClientException $e) {
|
||||
$response = $e->getResponse();
|
||||
}
|
||||
|
||||
|
||||
return new TopicResponse($response, $topics);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get the url
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getUrl()
|
||||
{
|
||||
return $this->config[ 'server_send_url' ];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user