Advertisement
Guest User

APNS.php

a guest
Jul 6th, 2014
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. !/usr/bin/env php
  2. <?php
  3. $deviceToken = '02da851dXXXXXXXXb4f2b5bfXXXXXXXXce198270XXXXXXXX0d3dac72bc87cd60'; // masked for security reason
  4. // Passphrase for the private key (ck.pem file)
  5. // $pass = '';
  6.  
  7. // Get the parameters from http get or from command line
  8. $message = $_GET['message'] or $message = $argv[1] or $message = 'Message received from javacom';
  9. $badge = (int)$_GET['badge'] or $badge = (int)$argv[2];
  10. $sound = $_GET['sound'] or $sound = $argv[3];
  11.  
  12. // Construct the notification payload
  13. $body = array();
  14. $body['aps'] = array('alert' => $message);
  15. if ($badge)
  16. $body['aps']['badge'] = $badge;
  17. if ($sound)
  18. $body['aps']['sound'] = $sound;
  19.  
  20.  
  21. /* End of Configurable Items */
  22.  
  23. $ctx = stream_context_create();
  24. stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
  25. // assume the private key passphase was removed.
  26. // stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
  27.  
  28. $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
  29. // for production change the server to ssl://gateway.push.apple.com:2195
  30. if (!$fp) {
  31. print "Failed to connect $err $errstr\n";
  32. return;
  33. }
  34. else {
  35. print "Connection OK\n";
  36. }
  37.  
  38. $payload = json_encode($body);
  39. $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
  40. print "sending message :" . $payload . "\n";
  41. fwrite($fp, $msg);
  42. fclose($fp);
  43. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement