You are here

class FirebaseGroupManagerService in Firebase Push Notification (FCM) 8

Same name and namespace in other branches
  1. 3.0.x src/Service/FirebaseGroupManagerService.php \Drupal\firebase\Service\FirebaseGroupManagerService

Service for managing device groups.

Hierarchy

Expanded class hierarchy of FirebaseGroupManagerService

1 string reference to 'FirebaseGroupManagerService'
firebase.services.yml in ./firebase.services.yml
firebase.services.yml
1 service uses FirebaseGroupManagerService
firebase.group_manager in ./firebase.services.yml
Drupal\firebase\Service\FirebaseGroupManagerService

File

src/Service/FirebaseGroupManagerService.php, line 12

Namespace

Drupal\firebase\Service
View source
class FirebaseGroupManagerService extends FirebaseServiceBase {

  /**
   * Maximum devices in group.
   */
  const MAX_DEVICES = 20;

  /**
   * Endpoint for send message.
   */
  const ENDPOINT = 'https://fcm.googleapis.com/fcm/notification';

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    return parent::buildHeader() + [
      'project_id' => $this->configFactory
        ->get('firebase.settings')
        ->get('sender_id'),
    ];
  }

  /**
   * Method for creation new device group.
   *
   * @param string $groupName
   *   Unique name for the group of devices.
   * @param array $deviceTokens
   *   Device or devices tokens, which should be combined into one group.
   *
   * @return bool|array
   *   Result from FCM.
   */
  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();
  }

  /**
   * Method for adding new devices to an existing group.
   *
   * @param string $groupName
   *   Unique name for the group of devices.
   * @param string $groupToken
   *   The token for identify group in FCM.
   * @param array $deviceTokens
   *   Device or devices tokens, which should be combined into one group.
   *
   * @return bool|array
   *   Result from FCM.
   */
  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();
  }

  /**
   * Method for removing devices from existing group.
   *
   * @param string $groupName
   *   Unique name for the group of devices.
   * @param string $groupToken
   *   The token for identify group in FCM.
   * @param array $deviceTokens
   *   Device or devices tokens, which should be combined into one group.
   *
   * @return bool|array
   *   Result from FCM.
   */
  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();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function Aliased as: sleep 1
DependencySerializationTrait::__wakeup public function Aliased as: wakeup 2
FirebaseGroupManagerService::addToGroup public function Method for adding new devices to an existing group.
FirebaseGroupManagerService::buildHeader public function Build the header. Overrides FirebaseServiceBase::buildHeader
FirebaseGroupManagerService::createGroup public function Method for creation new device group.
FirebaseGroupManagerService::ENDPOINT constant Endpoint for send message.
FirebaseGroupManagerService::MAX_DEVICES constant Maximum devices in group.
FirebaseGroupManagerService::removeFromGroup public function Method for removing devices from existing group.
FirebaseGroupManagerService::__construct public function Constructs a FirebaseServiceBase object. Overrides FirebaseServiceBase::__construct
FirebaseServiceBase::$body protected property Request body.
FirebaseServiceBase::$client protected property HTTP client.
FirebaseServiceBase::$configFactory protected property The config factory.
FirebaseServiceBase::$endpoint protected property Firebase service endpoint.
FirebaseServiceBase::$key protected property Firebase API Key.
FirebaseServiceBase::$logger protected property The logger channel.
FirebaseServiceBase::resetService public function Reset body of service.
FirebaseServiceBase::send public function Overrides FirebaseServiceInterface::send 1
FirebaseServiceBase::__sleep public function
FirebaseServiceBase::__wakeup public function