You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

93 lines
2.1 KiB

<?php
namespace App\Channels;
use App\Channels\Messages\FcmMessage;
use Illuminate\Notifications\Notification;
use GuzzleHttp\Client as HttpClient;
class FcmChannel
{
/**
* The API URL for FCM.
*
* @var string
*/
const API_URI = 'https://fcm.googleapis.com/fcm/send';
/**
* The HTTP client instance.
*
* @var \GuzzleHttp\Client
*/
protected $http;
/**
* The FCM API key.
*
* @var string
*/
protected $apikey;
/**
* Create a new FCM channel instance.
*
* @param \GuzzleHttp\Client $http
* @param string $apiKey
* @return void
*/
public function __construct(HttpClient $http, string $apiKey)
{
$this->http = $http;
$this->apiKey = $apiKey;
}
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return void
*/
public function send($notifiable, Notification $notification)
{
$message = $notification->toFcm($notifiable);
$message->to($notifiable->routeNotificationFor('fcm', $notification));
if (! $this->apiKey || (! $message->topic && ! $message->to)) {
return;
}
$this->http->post(self::API_URI, [
'headers' => [
'Authorization' => "key={$this->apiKey}",
'Content-Type' => 'application/json',
],
'json' => $this->buildJsonPayload($message),
]);
}
protected function buildJsonPayload(FcmMessage $message)
{
$payload = array_filter([
'priority' => $message->priority,
'data' => $message->data,
'notification' => $message->notification,
'condition' => $message->condition,
]);
if ($message->topic) {
$payload['to'] = "/topics/{$message->topic}";
} else {
if (is_array($message->to)) {
$payload['registration_ids'] = $message->to;
} else {
$payload['to'] = $message->to;
}
}
return $payload;
}
}