You are here

function push_notifications_send_message in Push Notifications 7

Send a simple message alert to an array of recipients.

Parameters

array $recipients Array of user ids.:

string $message Message to be included in payload.:

Return value

mixed Flag to indicate if delivery was successful.

2 calls to push_notifications_send_message()
push_notifications_privatemsg_message_insert in ./push_notifications.module
Implements hook_privatemsg_message_insert.
push_notifications_send_message_account in ./push_notifications.module
Send a simple message alert to a single account.

File

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

Code

function push_notifications_send_message($recipients, $message) {
  if (!is_array($recipients) || !is_string($message)) {
    return FALSE;
  }

  // Let modules modify the message before it is sent.
  foreach (module_implements('push_notifications_send_message') as $module) {
    $function = $module . '_push_notifications_send_message';
    $function($message, $type = 'simple');
  }

  // Shorten the message characters / 8 bit.
  $message = truncate_utf8($message, PUSH_NOTIFICATIONS_APNS_PAYLOAD_SIZE_LIMIT, TRUE, TRUE);

  // Convert the payload into the correct format for delivery.
  $payload = array(
    'alert' => $message,
  );

  // Determine if any of the recipients have one or multiple tokens stored.
  $tokens = array();
  foreach ($recipients as $uid) {
    $user_tokens = push_notification_get_user_tokens($uid);
    if (!empty($user_tokens)) {
      $tokens = array_merge($tokens, $user_tokens);
    }
  }

  // Stop right here if none of these users have any tokens.
  if (empty($tokens)) {
    return FALSE;
  }

  // Send a simple alert message.
  push_notifications_send_alert($tokens, $payload);
}