Advertisement
thieumao

push noti ios

Oct 27th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.61 KB | None | 0 0
  1. <?php
  2.  
  3. // Put your device token here (without spaces):
  4. //$deviceToken = 'ad648e26c765d78bad945eaf7eed7b192ffe6ed47fbfcfe973a11e7ed96931f2';
  5. $deviceToken = '047f373daca4a2f50694d87eda97eb4113901208414e0c31eb19b78ba1a78892';
  6.  
  7. // Put your private key's passphrase here:
  8. $passphrase = '';
  9.  
  10. $message = 'Breaking News';//$argv[1];
  11. $url = 'https://raywenderlich.com';//$argv[2];
  12.  
  13. if (!$message || !$url)
  14.     exit('Example Usage: $php newspush.php \'Breaking News!\' \'https://raywenderlich.com\'' . "\n");
  15.  
  16. ////////////////////////////////////////////////////////////////////////////////
  17.  
  18. $ctx = stream_context_create();
  19. stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
  20. stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
  21.  
  22. // Open a connection to the APNS server
  23. $fp = stream_socket_client(
  24.   'ssl://gateway.sandbox.push.apple.com:2195', $err,
  25.   $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
  26.  
  27. if (!$fp)
  28.   exit("Failed to connect: $err $errstr" . PHP_EOL);
  29.  
  30. echo 'Connected to APNS' . PHP_EOL;
  31.  
  32. // Create the payload body
  33. $body['aps'] = array(
  34.   'alert' => $message,
  35.   'sound' => 'default',
  36.   'link_url' => $url,
  37.   );
  38.  
  39. // Encode the payload as JSON
  40. $payload = json_encode($body);
  41.  
  42. // Build the binary notification
  43. $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
  44.  
  45. // Send it to the server
  46. $result = fwrite($fp, $msg, strlen($msg));
  47.  
  48. if (!$result)
  49.   echo 'Message not delivered' . PHP_EOL;
  50. else
  51.   echo 'Message successfully delivered' . PHP_EOL;
  52.  
  53. // Close the connection to the server
  54. fclose($fp);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement