You are here

private function PushNotificationsBroadcasterApns::openConnection in Push Notifications 8

Open a stream connection to APNS. Should be closed by calling fclose($connection) after usage.

1 call to PushNotificationsBroadcasterApns::openConnection()
PushNotificationsBroadcasterApns::sendBroadcast in src/PushNotificationsBroadcasterApns.php
Send the broadcast message.

File

src/PushNotificationsBroadcasterApns.php, line 137
Contains \Drupal\push_notifications\PushNotificationsBroadcasterApns.

Class

PushNotificationsBroadcasterApns
Broadcasts Android messages.

Namespace

Drupal\push_notifications

Code

private function openConnection() {

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

  // Set options on the stream context.
  stream_context_set_option($stream_context, 'ssl', 'local_cert', $this->certificate_path);

  // If the user has a passphrase stored, we use it.
  if (strlen($this->config
    ->get('passphrase'))) {
    stream_context_set_option($stream_context, 'ssl', 'passphrase', $this->config
      ->get('passphrase'));
  }
  if ($this->config
    ->get('set_entrust_certificate')) {
    stream_context_set_option($stream_context, 'ssl', 'CAfile', drupal_get_path('module', 'push_notifications') . '/certificates/entrust_2048_ca.cer');
  }

  // Open an Internet socket connection.
  $this->apns = stream_socket_client('ssl://' . $this->gateway . ':' . self::PUSH_NOTIFICATIONS_APNS_PORT, $error, $error_string, 2, STREAM_CLIENT_CONNECT, $stream_context);
  if (!$this->apns) {
    throw new \Exception('APNS connection could not be established. Check to make sure you are using a valid certificate file.');
  }
}