You are here

public function PushNotificationsDispatcher::dispatch in Push Notifications 8

Dispatch message.

File

src/PushNotificationsDispatcher.php, line 52
Contains \Drupal\push_notifications\PushNotificationsDispatcher.

Class

PushNotificationsDispatcher
Handles dispatching of messages. This class will send out the message to all networks in the list of tokens.

Namespace

Drupal\push_notifications

Code

public function dispatch() {
  foreach ($this->networks as $network) {

    // Only try this network if any tokens are available.
    if (empty($this->tokens[$network])) {
      $this->results[$network] = array(
        'network' => $network,
        'count_attempted' => 0,
        'count_success' => 0,
        'success' => NULL,
      );
      continue;
    }

    // Broadcast message.
    try {
      $service_name = 'push_notifications.broadcaster_' . $network;
      $broadcaster = \Drupal::service($service_name);
      $broadcaster
        ->setTokens($this->tokens[$network]);
      $broadcaster
        ->setMessage($this->message);
      $broadcaster
        ->sendBroadcast();
      $this->results[$network] = $broadcaster
        ->getResults();
    } catch (\Exception $e) {

      //drupal_set_message(t('Your message could not be sent. Please check the log for details.'), 'error');
      \Drupal::logger('push_notifications')
        ->error($e
        ->getMessage());
    }
  }
}