You are here

private function PushNotificationsBroadcasterGcm::processResult in Push Notifications 8

Process the a batch result.

Parameters

array $result: Result of a bundle process, containing the curl info, reponse, and raw response.

array $tokens: Tokens bundle that was processed.

Throws

\Exception Throw Exception when connection with Google play cannot be authenticated.

1 call to PushNotificationsBroadcasterGcm::processResult()
PushNotificationsBroadcasterGcm::sendBroadcast in src/PushNotificationsBroadcasterGcm.php
Send the broadcast message.

File

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

Class

PushNotificationsBroadcasterGcm
Broadcasts Android messages.

Namespace

Drupal\push_notifications

Code

private function processResult($result, $tokens) {

  // If connection is unauthorized, throw Exception.
  if ($result['info']['http_code'] != 200) {
    throw new \Exception('Connection could not be authorized with Google Play. Check your API key.');
  }

  // If Google returns a 200 reply, but that reply includes an error,
  // log the error message.
  if ($result['info']['http_code'] == 200 && !empty($result['response']->failure)) {
    \Drupal::logger('push_notifications')
      ->notice("Google's Server returned an error: @response_raw", array(
      '@response_raw' => $result['response_raw'],
    ));

    // Analyze the failure.
    foreach ($result['response']->results as $token_index => $message_result) {
      if (!empty($message_result->error)) {

        // If the device token is invalid or not registered (anymore because the user
        // has uninstalled the application), remove this device token.
        if ($message_result->error == 'NotRegistered' || $message_result->error == 'InvalidRegistration') {
          $entity_type = 'push_notifications_token';
          $query = \Drupal::entityQuery($entity_type)
            ->condition('token', $tokens[$token_index]);
          $entity_ids = $query
            ->execute();
          $entityTypeManager = \Drupal::entityTypeManager()
            ->getStorage($entity_type);
          $entity = $entityTypeManager
            ->load(array_shift($entity_ids));
          $entity
            ->delete();
          \Drupal::logger('push_notifications')
            ->notice("GCM token not valid anymore. Removing token @token", array(
            '@$token' => $tokens[$token_index],
          ));
        }
      }
    }
  }

  // Count the successful sent push notifications if there are any.
  if ($result['info']['http_code'] == 200 && !empty($result['response']->success)) {
    $this->countSuccess += $result['response']->success;
  }
}