You are here

function push_notifications_open_apns in Push Notifications 7

Open an APNS connection. Should be closed by calling fclose($connection) after usage.

1 call to push_notifications_open_apns()
push_notifications_apns_send_message in ./push_notifications.module
Send out push notifications through APNS.

File

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

Code

function push_notifications_open_apns() {

  // Determine the absolute path of the certificate.
  // @see http://stackoverflow.com/questions/809682
  $apns_cert = _push_notifications_get_apns_certificate();

  // Create a stream context.
  $stream_context = stream_context_create();

  // Set options on the stream context.
  stream_context_set_option($stream_context, 'ssl', 'local_cert', $apns_cert);

  // If the user has a passphrase stored, we use it.
  $passphrase = variable_get('push_notifications_apns_passphrase', '');
  if (strlen($passphrase)) {
    stream_context_set_option($stream_context, 'ssl', 'passphrase', $passphrase);
  }
  if (variable_get('push_notifications_set_entrust_certificate', FALSE)) {
    stream_context_set_option($stream_context, 'ssl', 'CAfile', drupal_get_path('module', 'push_notifications') . '/certificates/entrust_2048_ca.cer');
  }

  // Open an Internet socket connection.
  $apns = stream_socket_client('ssl://' . PUSH_NOTIFICATIONS_APNS_HOST . ':' . PUSH_NOTIFICATIONS_APNS_PORT, $error, $error_string, 2, STREAM_CLIENT_CONNECT, $stream_context);
  if (!$apns) {
    watchdog('push_notifications', 'Connection to Apple Notification Server failed.', NULL, WATCHDOG_ERROR);
    return FALSE;
  }
  else {
    return $apns;
  }
}