Advertisement
Guest User

Apple Push Notification

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