You are here

function push_notifications_send_alert in Push Notifications 7

Handle delivery of simple alert message.

Parameters

array $tokens Array of token record objects.:

array $payload Payload.:

2 calls to push_notifications_send_alert()
push_notifications_send_message in ./push_notifications.module
Send a simple message alert to an array of recipients.
push_notifications_send_message_bulk in ./push_notifications.module
Send a simple message alert to all tokens in the database.

File

./push_notifications.module, line 579
Push Notifications functionality.

Code

function push_notifications_send_alert($tokens = array(), $payload = array()) {

  // Group tokens into types.
  $tokens_ios = array();
  $tokens_android = array();
  foreach ($tokens as $token) {
    switch ($token->type) {
      case PUSH_NOTIFICATIONS_TYPE_ID_IOS:
        $tokens_ios[] = $token->token;
        break;
      case PUSH_NOTIFICATIONS_TYPE_ID_ANDROID:
        $tokens_android[] = $token->token;
        break;
    }
  }

  // Send payload to iOS recipients.
  if (!empty($tokens_ios)) {

    // Convert the payload into the correct format for APNS.
    $payload_apns = array(
      'aps' => $payload,
    );
    push_notifications_apns_send_message($tokens_ios, $payload_apns);
  }

  // Send payload to Android recipients if configured correctly.
  if (!empty($tokens_android) && (PUSH_NOTIFICATIONS_C2DM_USERNAME && PUSH_NOTIFICATIONS_C2DM_PASSWORD || PUSH_NOTIFICATIONS_GCM_API_KEY || PUSH_NOTIFICATIONS_FCM_SERVER_KEY)) {

    // Determine which method to use for Google push notifications.
    switch (PUSH_NOTIFICATIONS_GOOGLE_TYPE) {
      case PUSH_NOTIFICATIONS_GOOGLE_TYPE_C2DM:
        push_notifications_c2dm_send_message($tokens_android, $payload);
        break;
      case PUSH_NOTIFICATIONS_GOOGLE_TYPE_GCM:
        push_notifications_gcm_send_message($tokens_android, $payload);
        break;
      case PUSH_NOTIFICATIONS_GOOGLE_TYPE_FCM:
        push_notifications_fcm_send_message($tokens_android, $payload);
        break;
    }
  }
}