FirebaseMessageService.php in Firebase Push Notification (FCM) 8
File
src/Service/FirebaseMessageService.php
View source
<?php
namespace Drupal\firebase\Service;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Logger\LoggerChannelInterface;
use GuzzleHttp\ClientInterface;
class FirebaseMessageService extends FirebaseServiceBase {
const MAX_DEVICES = 1000;
const MAX_TOPICS = 3;
const ENDPOINT = 'https://fcm.googleapis.com/fcm/send';
protected $condition;
public function __construct(ConfigFactory $configFactory, ClientInterface $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 createCondition($condition) {
$this->condition = $condition;
}
private function checkReservedKeywords(array $data) {
foreach ($data as $key => $value) {
if (preg_match('/(^from$)|(^gcm)|(^google)/', $key)) {
return FALSE;
}
}
return TRUE;
}
public function setData(array $data) {
if ($this
->checkReservedKeywords($data)) {
$this->body['data'] = $data;
}
else {
throw new \InvalidArgumentException('Keys in data shouldn\'t contain "form" and any keys starting with "google" and "gcm".');
}
}
public function setOptions(array $options) {
if (isset($options['priority']) && in_array(strtolower($options['priority']), [
'high',
'normal',
])) {
$this->body['priority'] = $options['priority'];
}
if (isset($options['content_available'])) {
$this->body['content_available'] = (bool) $options['content_available'];
}
if (isset($options['mutable_content'])) {
$this->body['mutable_content'] = (bool) $options['mutable_content'];
}
if (isset($options['time_to_live']) && ((int) $options['time_to_live'] >= 0 && (int) $options['time_to_live'] <= 2419200)) {
$this->body['time_to_live'] = $options['time_to_live'];
}
if (isset($options['dry_run'])) {
$this->body['dry_run'] = (bool) $options['dry_run'];
}
}
public function setRecipients($recipients) {
if (is_array($recipients)) {
if (count($recipients) < self::MAX_DEVICES) {
$this->body['registration_ids'] = $recipients;
}
else {
throw new \OutOfRangeException(sprintf('Message device limit exceeded. Firebase supports a maximum of %u devices.', self::MAX_DEVICES));
}
}
else {
$this->body['to'] = $recipients;
}
}
public function setTopics($topics) {
if (is_array($topics) and count($topics) > 1) {
if (count($topics) > self::MAX_TOPICS) {
throw new \OutOfRangeException(sprintf('Topics limit exceeded. Firebase supports a maximum of %u topics.', self::MAX_TOPICS));
}
elseif (!$this->condition) {
throw new \InvalidArgumentException('Missing message condition. You must specify a condition pattern when sending to combinations of topics.');
}
elseif (count($topics) != substr_count($this->condition, '%s')) {
throw new \UnexpectedValueException('The number of message topics must match the number of occurrences of "%s" in the condition pattern.');
}
else {
foreach ($topics as &$topic) {
$topic = vsprintf("'%s' in topics", $topic);
}
$this->body['condition'] = vsprintf($this->condition, $topics);
}
}
else {
if (is_array($topics)) {
$topics = reset($topics);
}
$this->body['to'] = sprintf('/topics/%s', $topics);
}
}
public function setNotification(array $notification) {
if (!empty($notification['title'])) {
$this->body['notification'] = $notification;
}
}
public function send() {
if (!isset($this->body['notification']) && !isset($this->body['data'])) {
throw new \InvalidArgumentException('The message must belong to one of the message types (notification or data message).');
}
if (!isset($this->body['to']) && !isset($this->body['registration_ids']) && !isset($this->body['condition'])) {
throw new \InvalidArgumentException('The message should contain target.');
}
return parent::send();
}
}