[英]PHP Sending Push Notifications Failed to connect: 111 Connection refused
我正在嘗試設置推送通知,我從蘋果開發者那里獲得了我的證書,無法在我的ios應用程序上推送通知,並且我有一個PHP腳本,假設發送推送通知。 我的托管服務提供商將我的帳戶的端口2195出站列入白名單。 但推送通知仍然無效,我收到此錯誤
連接失敗:111連接被拒絕
我正在使用開發證書,它們在我的Apple開發者帳戶中啟用。
<?php
class PushNotifications {
private static $passphrase = 'PASSPHRASE';
public function iOS($data, $devicetoken) {
$deviceToken = $devicetoken;
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'Certificates.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', self::$passphrase);
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
{
exit("Failed to connect: $err $errstr" . PHP_EOL);
}
$body['aps'] = array('alert' => array('title' => $data['mtitle'], 'body' => $data['mdesc'],),'sound' => 'default');
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
fclose($fp);
if (!$result)
return 'Message not delivered' . PHP_EOL;
else
return 'Message successfully delivered' . PHP_EOL;
}
private function useCurl(&$model, $url, $headers, $fields = null) {
$ch = curl_init();
if ($url) {
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if ($fields) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
}
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
}
}
?>
有人可以幫忙嗎?
確保你還沒有被阻止(從你的php主機運行):
telnet gateway.sandbox.push.apple.com 2195
如果拒絕連接,則問題仍然是您的托管服務提供商或您與APNS之間的某個人。
或者,如果您沒有shell訪問權限:
fsockopen('gateway.sandbox.push-apple.com.akadns.net', 2195);
我找到了問題並修復了它。
問題出在.pem證書上。 不知何故,在一個文件中有兩個證書用於生產和開發.pem文件。 具有兩個證書的相同.pem文件在回購中很長一段時間,但APN僅在幾個月前停止工作。 也許蘋果方面有些東西升級/改變了。
但是,解決方案是從.pem文件中刪除第二個證書。 在APN開始工作之后,他們現在正在工作。
希望這是有用的,它適合你。
消息數據
$pushnoti = array();
$pushnoti['type'] = 'message_notification';
$pushnoti['body'] = array( 'message' => "Your Order id Completed");
send_notification_iphone ($deviceToken, $pushnoti);
通知功能
function send_notification_iphone( $deviceToken, $data) {
// Put your private key's passphrase here:
$passphrase = self::$passphrase;
$pem = 'ck.pem';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $pem);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
$message['body'] = $data['body'];
$body['aps'] = array( 'alert' => $data['body']['message'], 'body' => $data['body'], 'type'=> $data['type'] );
$body['badge'] =3;
$body['sound'] ='cat.caf';
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
// Close the connection to the server
fclose($fp);
}
我也遇到了這個錯誤。 以下是你必須記住的事情:
如果使用,請檢查您的php版本支持的ssl
SSL://gateway.sandbox.push.apple.com
並且如果使用的話
TLS://gateway.sandbox.push.apple.com
您在推送或令牌等中發送的數據應等於或小於apple doc中提到的指定字節。 鏈接如下所述。
你也可以使用api get reference來發送push,開發服務器:
api.development.push.apple.com:443
生產服務器:
api.push.apple.com:443
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.