[轉貼]PHP 推撥 IOS PUSH

列印

文章轉貼自:http://blog.csdn.net/newjueqi/article/details/8315093

是按照教程http://blog.csdn.net/newjueqi/article/details/7898591 來做的。

注意事項:

1. 測試階段使用的推送地址:

ssl://gateway.sandbox.push.apple.com:2195

 

正式上線使用的推送地址:

ssl://gateway.push.apple.com:2195

 

 

2.那裡給的php推送代碼有問題,下面我貼出修改後推送成功的代碼:

 

  1. <?php
  2. // 这里是我们上面得到的deviceToken,直接复制过来(记得去掉空格)
  3. $deviceToken=$_POST['deviceToken'];
  4. // Put your private key's passphrase here:
  5. $passphrase = 'password';
  6. // Put your alert message here:
  7. $message = $_POST['message'];
  8. $ctx = stream_context_create();
  9. stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
  10. stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
  11. // Open a connection to the APNS server
  12. //这个为正是的发布地址
  13. //$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
  14. //这个是沙盒测试地址,发布到appstore后记得修改哦
  15. $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
  16. if (!$fp){
  17. exit("Failed to connect: $err $errstr" . PHP_EOL);
  18. }
  19. //echo 'Connected to APNS' . PHP_EOL;
  20. // Create the payload body
  21. $body['aps'] = array(
  22. 'alert' => $message,
  23. 'forum_id' => 88,
  24. 'topic_id' => 999,
  25. );
  26. // Encode the payload as JSON
  27. $payload = json_encode($body);
  28. // Build the binary notification
  29. //$msg = chr(0).pack('n', 32).pack('H', $deviceToken). pack('n', strlen($payload)).$payload;
  30. $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
  31. // Send it to the server
  32. $result = fwrite($fp, $msg, strlen($msg));
  33. if (!$result){
  34. echo 'Message not delivered' . PHP_EOL;
  35. }else{
  36. echo 'Message successfully delivered' . PHP_EOL;
  37. }
  38. // Close the connection to the server
  39. fclose($fp);
  40. ?>

 

最近更新 ( 週四, 25 四月 2013 16:26 )