FirebaseGroupManagerService.php in Firebase Push Notification (FCM) 8
File
src/Service/FirebaseGroupManagerService.php
View source
<?php
namespace Drupal\firebase\Service;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Logger\LoggerChannelInterface;
use GuzzleHttp\Client;
class FirebaseGroupManagerService extends FirebaseServiceBase {
const MAX_DEVICES = 20;
const ENDPOINT = 'https://fcm.googleapis.com/fcm/notification';
public function __construct(ConfigFactory $configFactory, Client $client, LoggerChannelInterface $loggerChannel) {
parent::__construct($configFactory, $client, $loggerChannel);
$config = $this->configFactory
->get('firebase.settings');
$this->key = $config
->get('server_key');
$this->endpoint = self::ENDPOINT;
}
public function buildHeader() {
return parent::buildHeader() + [
'project_id' => $this->configFactory
->get('firebase.settings')
->get('sender_id'),
];
}
public function createGroup($groupName, array $deviceTokens = []) {
if (!$groupName || empty($deviceTokens)) {
return FALSE;
}
if (count($deviceTokens) > self::MAX_DEVICES) {
throw new \OutOfRangeException('Device in group limit exceeded. Firebase supports a maximum of %u devices in one group.', self::MAX_DEVICES);
}
$this->body = [
'operation' => 'create',
'notification_key_name' => $groupName,
'registration_ids' => $deviceTokens,
];
return $this
->send();
}
public function addToGroup($groupName, $groupToken, array $deviceTokens) {
if (!$groupName || empty($deviceTokens) || !$groupToken) {
return FALSE;
}
if (count($deviceTokens) > self::MAX_DEVICES) {
throw new \OutOfRangeException('Device in group limit exceeded. Firebase supports a maximum of %u devices in one group.', self::MAX_DEVICES);
}
$this->body = [
'operation' => 'add',
'notification_key_name' => $groupName,
'notification_key' => $groupToken,
'registration_ids' => $deviceTokens,
];
return $this
->send();
}
public function removeFromGroup($groupName, $groupToken, array $deviceTokens) {
if (!$groupName || empty($deviceTokens) || !$groupToken) {
return FALSE;
}
$this->body = [
'operation' => 'remove',
'notification_key_name' => $groupName,
'notification_key' => $groupToken,
'registration_ids' => $deviceTokens,
];
return $this
->send();
}
}