PushNotifications.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. // Server file
  3. class PushNotifications {
  4. // (Android)API access key from Google API's Console.
  5. private static $API_ACCESS_KEY = 'AIzaSyAXstfwpQbLarzZPRO8MXwHx31TSly5QQ4';
  6. // (iOS) Private key's passphrase.
  7. private static $passphrase = 'nottset';
  8. // (Windows Phone 8) The name of our push channel.
  9. private static $channelName = "notset";
  10. // Change the above three vriables as per your app.
  11. public function __construct() {
  12. exit('Init function is not allowed');
  13. }
  14. // Sends Push notification for Android users
  15. public function android($data, $reg_id) {
  16. $url = 'https://android.googleapis.com/gcm/send';
  17. $message = array(
  18. 'title' => $data['mtitle'],
  19. 'message' => $data['mdesc'],
  20. 'subtitle' => '',
  21. 'tickerText' => '',
  22. 'msgcnt' => 1,
  23. 'vibrate' => 1
  24. );
  25. $headers = array(
  26. 'Authorization: key=' .self::$API_ACCESS_KEY,
  27. 'Content-Type: application/json'
  28. );
  29. $fields = array(
  30. 'registration_ids' => array($reg_id),
  31. 'data' => $message,
  32. );
  33. return $this->useCurl($url, $headers, json_encode($fields));
  34. }
  35. // Sends Push's toast notification for Windows Phone 8 users
  36. public function WP($data, $uri) {
  37. $delay = 2;
  38. $msg = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" .
  39. "<wp:Notification xmlns:wp=\"WPNotification\">" .
  40. "<wp:Toast>" .
  41. "<wp:Text1>".htmlspecialchars($data['mtitle'])."</wp:Text1>" .
  42. "<wp:Text2>".htmlspecialchars($data['mdesc'])."</wp:Text2>" .
  43. "</wp:Toast>" .
  44. "</wp:Notification>";
  45. $sendedheaders = array(
  46. 'Content-Type: text/xml',
  47. 'Accept: application/*',
  48. 'X-WindowsPhone-Target: toast',
  49. "X-NotificationClass: $delay"
  50. );
  51. $response = $this->useCurl($uri, $sendedheaders, $msg);
  52. $result = array();
  53. foreach(explode("\n", $response) as $line) {
  54. $tab = explode(":", $line, 2);
  55. if (count($tab) == 2)
  56. $result[$tab[0]] = trim($tab[1]);
  57. }
  58. return $result;
  59. }
  60. // Sends Push notification for iOS users
  61. public function iOS($data, $devicetoken) {
  62. $deviceToken = $devicetoken;
  63. $ctx = stream_context_create();
  64. // ck.pem is your certificate file
  65. stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
  66. stream_context_set_option($ctx, 'ssl', 'passphrase', self::$passphrase);
  67. // Open a connection to the APNS server
  68. $fp = stream_socket_client(
  69. 'ssl://gateway.sandbox.push.apple.com:2195', $err,
  70. $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
  71. if (!$fp)
  72. exit("Failed to connect: $err $errstr" . PHP_EOL);
  73. // Create the payload body
  74. $body['aps'] = array(
  75. 'alert' => array(
  76. 'title' => $data['mtitle'],
  77. 'body' => $data['mdesc'],
  78. ),
  79. 'sound' => 'default'
  80. );
  81. // Encode the payload as JSON
  82. $payload = json_encode($body);
  83. // Build the binary notification
  84. $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
  85. // Send it to the server
  86. $result = fwrite($fp, $msg, strlen($msg));
  87. // Close the connection to the server
  88. fclose($fp);
  89. if (!$result)
  90. return 'Message not delivered' . PHP_EOL;
  91. else
  92. return 'Message successfully delivered' . PHP_EOL;
  93. }
  94. // Curl
  95. private function useCurl(&$model, $url, $headers, $fields = null) {
  96. // Open connection
  97. $ch = curl_init();
  98. if ($url) {
  99. // Set the url, number of POST vars, POST data
  100. curl_setopt($ch, CURLOPT_URL, $url);
  101. curl_setopt($ch, CURLOPT_POST, true);
  102. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  103. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  104. // Disabling SSL Certificate support temporarly
  105. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  106. if ($fields) {
  107. curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
  108. }
  109. // Execute post
  110. $result = curl_exec($ch);
  111. if ($result === FALSE) {
  112. die('Curl failed: ' . curl_error($ch));
  113. }
  114. // Close connection
  115. curl_close($ch);
  116. return $result;
  117. }
  118. }
  119. }
  120. ?>