You are here

public function FirebaseServiceBase::send in Firebase Push Notification (FCM) 8

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

Throws

\Exception

Overrides FirebaseServiceInterface::send

5 calls to FirebaseServiceBase::send()
FirebaseGroupManagerService::addToGroup in src/Service/FirebaseGroupManagerService.php
Method for adding new devices to an existing group.
FirebaseGroupManagerService::createGroup in src/Service/FirebaseGroupManagerService.php
Method for creation new device group.
FirebaseGroupManagerService::removeFromGroup in src/Service/FirebaseGroupManagerService.php
Method for removing devices from existing group.
FirebaseMessageService::send in src/Service/FirebaseMessageService.php
Send request to FCM.
FirebaseTopicManagerService::processTopicSubscription in src/Service/FirebaseTopicManagerService.php
Process topic un/subscription.
1 method overrides FirebaseServiceBase::send()
FirebaseMessageService::send in src/Service/FirebaseMessageService.php
Send request to FCM.

File

src/Service/FirebaseServiceBase.php, line 94

Class

FirebaseServiceBase
Provides a base class for service, working with FCM.

Namespace

Drupal\firebase\Service

Code

public function send() {

  // Build the header of our request to Firebase.
  // The header is composed by Content-Type and Authorization.
  // The Authorization is our server key, which needs to be provided
  // by the admin interface.
  // @see \Drupal\firebase\Form\ConfigurationForm.
  $headers = $this
    ->buildHeader();

  // The body is composed by an array of data.
  if (!$this->body) {
    throw new \InvalidArgumentException("The body of request shouldn't be blank.");
  }
  try {
    $response = $this->client
      ->post($this->endpoint, [
      'headers' => $headers,
      'body' => Json::encode($this->body),
    ]);
  } catch (\Exception $e) {

    // Error connecting to Firebase API.
    $this->logger
      ->error($e
      ->getMessage());
    throw $e;
  }
  if (isset($response)) {
    $responseBody = Json::decode($response
      ->getBody());
    if ($response
      ->getStatusCode() === 200) {
      if (isset($responseBody['failure']) && $responseBody['failure'] != 0) {

        // Something went wrong. We didn't sent the push notification.
        // Common errors:
        // - Authentication Error
        //   The Server Key is invalid.
        // - Invalid Registration Token
        //   The token (generated by app) is not recognized by Firebase.
        // @see https://firebase.google.com/docs/cloud-messaging/http-server-ref#error-codes
        if (count($responseBody['results']) == 1) {
          $errorMessage = reset($responseBody['results'])['error'];
          $this->logger
            ->error('Failure message: @error', [
            '@error' => $errorMessage,
          ]);
        }
        else {
          $this->logger
            ->error('Some of the recipients will not be able to receive the notification, please check response.');
        }
      }
      return $responseBody;
    }
  }
  return FALSE;
}