FirebaseNotificationService.php in Firebase Push Notification (FCM) 8
File
src/Service/FirebaseNotificationService.php
View source
<?php
namespace Drupal\firebase\Service;
class FirebaseNotificationService {
public $messageService;
public function __construct(FirebaseMessageService $messageService) {
$this->messageService = $messageService;
}
public function send($token, array $param) {
$options = [];
$notification = [];
if (!isset($param['priority'])) {
$options['priority'] = 'high';
}
if (isset($param['icon'])) {
$notification['icon'] = $param['icon'];
}
if (isset($param['sound'])) {
$notification['sound'] = $param['sound'];
}
if (isset($param['click_action'])) {
$notification['click_action'] = $param['click_action'];
}
if (isset($param['badge'])) {
$notification['badge'] = $param['badge'];
}
if (isset($param['content_available'])) {
$options['content_available'] = $param['content_available'];
}
if (!empty($param['title']) && !empty($param['body'])) {
$notification += [
'title' => $param['title'],
'body' => $param['body'],
];
}
$this->messageService
->setRecipients($token);
$this->messageService
->setOptions($options);
$this->messageService
->setNotification($notification);
if (isset($param['data'])) {
$this->messageService
->setData($param['data']);
}
$this->messageService
->send();
}
}