Advertisement
Guest User

apns

a guest
Feb 1st, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.40 KB | None | 0 0
  1. <?php
  2.    
  3.    
  4.     ini_set('display_errors','On');
  5.     error_reporting(E_ALL);
  6.    
  7.    
  8.     $deviceToken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
  9.     $passphrase = '‚1234‘' ;
  10.     $titel = 'EMERGENCY';
  11.     $message = 'this is a test';
  12.    
  13.     $ctx = stream_context_create();
  14.    
  15.     stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns_cert.pem');
  16.     stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
  17.     // Open a connection to the APNS server
  18.     $fp = stream_socket_client(
  19.                                'ssl://gateway.sandbox.push.apple.com:2195', $err,
  20.                                $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
  21.     if (!$fp)
  22.     exit("Failed to connect: $err $errstr" . PHP_EOL);
  23.     echo 'Connected to APNS' . PHP_EOL;
  24.     // Create the payload body
  25.     $body['aps'] = array(
  26.                          'alert' => $message,
  27.                          'sound' => 'default',
  28.                          'patient' => 'test'
  29.                          );
  30.    
  31.    
  32.     $payload = json_encode($body);
  33.    
  34.     $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
  35.    
  36.     $result = fwrite($fp, $msg, strlen($msg));
  37.     if (!$result)
  38.     echo 'Message not delivered' . PHP_EOL;
  39.     else
  40.     echo 'Message successfully delivered' . PHP_EOL;
  41.    
  42.     fclose($fp);
  43.    
  44.     ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement