You are here

function push_notifications_apns_send_message in Push Notifications 7

Send out push notifications through APNS.

Parameters

$tokens: Array of iOS tokens

$payload: Payload to send. Minimum requirement is a nested array in this format: $payload = array( 'aps' => array( 'alert' => 'Push Notification Test', ); );

Return value

Array with the following keys:

  • count_attempted (# of attempted messages sent)
  • count_success (# of successful sends)
  • success (# boolean)
  • message (Prepared result message)
2 calls to push_notifications_apns_send_message()
push_notifications_mass_push_form_submit in includes/push_notifications.admin.inc
Submit handler for sending out a mass-push notification.
push_notifications_send_alert in ./push_notifications.module
Handle delivery of simple alert message.

File

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

Code

function push_notifications_apns_send_message($tokens, $payload) {
  if (!is_array($tokens) || empty($payload) || is_array($tokens) && empty($tokens)) {
    return FALSE;
  }
  $payload_apns = array();

  // Allow for inclusion of custom payloads.
  foreach ($payload as $key => $value) {
    if ($key != 'aps') {
      $payload_apns[$key] = $value;
    }
  }

  // Add the default 'aps' key for the payload.
  $payload_apns['aps'] = $payload['aps'];

  // Set the default sound if no sound was set.
  if (!isset($payload_apns['aps']['sound'])) {
    $payload_apns['aps']['sound'] = PUSH_NOTIFICATIONS_APNS_NOTIFICATION_SOUND;
  }

  // JSON-encode the payload.
  $payload_apns = json_encode($payload_apns);
  $result = array(
    'count_attempted' => 0,
    'count_success' => 0,
    'success' => 0,
    'message' => '',
  );

  // Send a push notification to every recipient.
  $stream_counter = 0;
  foreach ($tokens as $token) {

    // Open an apns connection, if necessary.
    if ($stream_counter == 0) {
      $apns = push_notifications_open_apns();
      if (!$apns) {
        $result['message'] = t('APNS connection could not be established. Check to make sure you are using a valid certificate file.');
        return $result;
      }
    }
    $stream_counter++;
    $result['count_attempted']++;
    $apns_message = chr(0) . chr(0) . chr(32) . pack('H*', $token) . pack('n', strlen($payload_apns)) . $payload_apns;

    // Write the payload to the currently active streaming connection.
    $success = fwrite($apns, $apns_message);
    if ($success) {
      $result['count_success']++;
    }
    elseif ($success == 0 || $success == FALSE || $success < strlen($apns_message)) {
      $error_message = array(
        'message' => t('APNS message could not be sent.'),
        'token' => 'Token: ' . $token,
        'data' => 'fwrite returned: ' . $success,
      );
      watchdog('push_notifications', implode($error_message, '<br />'));
    }

    // Reset the stream counter if no more messages should
    // be sent with the current stream context.
    // This results in the generation of a new stream context
    // at the beginning of this loop.
    if ($stream_counter >= PUSH_NOTIFICATIONS_APNS_STREAM_CONTEXT_LIMIT) {
      $stream_counter = 0;
      if (is_resource($apns)) {
        fclose($apns);
      }
    }
  }

  // Close the apns connection if it hasn't already been closed.
  // Need to check if $apns is a resource, as pointer will not
  // be closed by fclose.
  if (is_resource($apns)) {
    fclose($apns);
  }
  $result['message'] = t('Successfully sent !count_success iOS push messages (attempted to send !count messages).', array(
    '!count_success' => $result['count_success'],
    '!count' => $result['count_attempted'],
  ));
  $result['success'] = TRUE;

  // Invoke rules.
  if (module_exists('rules')) {
    rules_invoke_event_by_args('push_notifications_after_apns_send', array(
      'type_id' => PUSH_NOTIFICATIONS_TYPE_ID_IOS,
      'payload' => $payload,
      'count_attempted' => $result['count_attempted'],
      'count_success' => $result['count_success'],
      'success' => $result['success'],
      'result_message' => $result['message'],
    ));
  }
  return $result;
}