[英]ios Push Notification not working on the server
我在本地機器上測試了推送通知,它運行正常。 然后我使用相同的證書(“。pem”文件)將我的文件上傳到真實服務器,並確保端口2195和2196是打開的。
我測試過:
telnet gateway.sandbox.push.apple.com 2195
這是工作...
root@server ~ # telnet gateway.sandbox.push.apple.com 2195
Trying 17.149.34.54...
Connected to gateway.sandbox.push.apple.com.
Escape character is '^]'.
但是,當我從我的PHP腳本測試時,它返回:
警告:stream_socket_client()[function.stream-socket-client]:無法連接到ssl://gateway.sandbox.push.apple.com:2195(連接超時)
警告:stream_socket_client()[function.stream-socket-client]:無法連接到ssl://feedback.sandbox.push.apple.com:2196
有什么建議?
這是php代碼:
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert',
"path/to/certificate");
$fp = stream_socket_client("ssl://gateway.push.apple.com:2195",
$error, $errorString, 100,
(STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT), $ctx);
這段代碼可以正常工作:
$device = 'sdfsdfsfsdffsd';
$payload['aps'] = array('alert' => 'Hello I am testing the server code ....', 'badge' => 1, 'sound' => 'default');
$payload = json_encode($payload);
$options = array('ssl' => array(
'local_cert' =>'ck.pem',
'passphrase' => 'abc123'
));
$streamContext = stream_context_create();
stream_context_set_option($streamContext, $options);
$apns = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $device)) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);
fclose($apns);
僅檢查pem文件路徑,並且端口已打開。
我遇到了'無法連接到...(連接超時)'的相同問題。 我可以從我的家用機器連接,但我不能從托管服務器。
就我而言,端口2195和2196不是簡單地從托管服務器打開。 我不得不聯系技術支持來打開這些端口。 Ping可能有效,因為它使用不同的端口號。 因此,請與您的托管服務公司聯系,以確保這些端口是開放的。
祝好運,
卡茲
在stream_socket_client
函數中,您不應傳遞證書路徑,而應傳遞APNS網址。
此方法打開一個連接並返回它:
public function connect(){
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', '/your/cert/path');
$apns = stream_socket_client('use.apns.url.here', $error, $errorString, 9, STREAM_CLIENT_CONNECT, $streamContext);
if (!$apns){
$this->logger->error("Failed to connect to APNS: {$error} {$errorString}.");
}
return $apns;
}
構建apns消息后,可以使用以下命令進行推送
fwrite($apns, $apnsMessage);
如果您的目標是實現自己的推送通知API,則可以按照本教程http://goo.gl/9Q0u進行操作。
還有現有的php庫:
還有一個非常好的Java庫(我目前用它來進行大量推送):
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.