You are here

public function FirebaseMessageService::setTopics in Firebase Push Notification (FCM) 8

Same name and namespace in other branches
  1. 3.0.x src/Service/FirebaseMessageService.php \Drupal\firebase\Service\FirebaseMessageService::setTopics()

Add topic or list of topics message target.

Parameters

string|array $topics: Topics without "/topics/".

See also

https://firebase.google.com/docs/cloud-messaging/send-message#send_messa...

File

src/Service/FirebaseMessageService.php, line 164

Class

FirebaseMessageService
Service for pushing message to mobile devices using Firebase.

Namespace

Drupal\firebase\Service

Code

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);
  }
}