You are here

function firebase_send in Firebase Push Notification (FCM) 7

Sends the push notification.

Parameters

string $token: Firebase token that identify each device.

array $param: Parameters for payload. Expected values are:

  • $param['title'] Title of push message
  • $param['body'] Body of push message

Optional values are:

  • $param['icon'] Icon to be displayed. If none is given, the App's icon will be used.
  • $param['sound'] Sound to play. If none is given, the App's default will be used.
  • $param['priority'] Set message priority.
  • $param['click_action'] The action associated with a user click on the notification.
  • $param['content_available'] If sending silent pushes for iOS, this must be equal to TRUE.
  • $param['data'] Send extra information to device. Not displayed to users.
  • $param['badge'] Badge number on App icon.

Return value

bool TRUE if the push was sent successfully, and FALSE if not.

File

./firebase.module, line 86

Code

function firebase_send($token, $param) {

  // We absolutely need the token. If it was not provided, return early.
  if (empty($token)) {
    return FALSE;
  }
  if (!($response = _firebase_sendPushNotification($token, $param))) {

    // Error connecting to Firebase API. For instance, timeout.
    return FALSE;
  }
  if ($response['body']->success === 1 && $response['body']->failure === 0) {
    return TRUE;
  }

  // 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
  $error_message = reset($response['body']->results)->error;
  watchdog('firebase', 'Message failure: !error', array(
    '!error' => $error_message,
  ));
  return FALSE;
}