You are here

public function PushNotificationsBroadcasterGcm::sendBroadcast in Push Notifications 8

Send the broadcast message.

Throws

\Exception Array of tokens and payload necessary to send out a broadcast.

Overrides PushNotificationsBroadcasterInterface::sendBroadcast

File

src/PushNotificationsBroadcasterGcm.php, line 97
Contains \Drupal\push_notifications\PushNotificationsBroadcasterGcm.

Class

PushNotificationsBroadcasterGcm
Broadcasts Android messages.

Namespace

Drupal\push_notifications

Code

public function sendBroadcast() {
  if (empty($this->tokens) || empty($this->payload)) {
    throw new \Exception('No tokens or payload set.');
  }

  // Set token bundles.
  $this->tokenBundles = ceil(count($this->tokens) / 1000);

  // Set number of tokens to attempt.
  $this->countAttempted = count($this->tokens);

  // Send notifications in slices of 1000
  // and process the results.
  for ($i = 0; $i < $this->tokenBundles; $i++) {
    try {
      $bundledTokens = array_slice($this->tokens, $i * 1000, 1000, FALSE);
      $result = $this
        ->sendTokenBundle($bundledTokens);
      $this
        ->processResult($result, $bundledTokens);
    } catch (\Exception $e) {
      \Drupal::logger('push_notifications')
        ->error($e
        ->getMessage());
    }
  }

  // Mark success as true.
  $this->success = TRUE;
}