2010-10-08 127 views
5

我試圖發送推送通知噸我的iPhone(APNS)。我讀了這個post並試圖實現它。所以我所有的證書都很好(非常規)。使用PHP腳本的Apple推送通知服務

現在我有這個PHP腳本:

$device = '4f30e047 c8c05db9 3fa87e7d ca5325f7 738cb2c0 0b4a02d4 d4329a42 a7128173'; // My iphone deviceToken 
$payload['aps'] = array('alert' => 'This is the alert text', 'badge' => 1, 'sound' => 'default'); 
$payload['server'] = array('serverId' => $serverId, 'name' => $name); 
$output = json_encode($payload); 

$apnsCert = 'apple_push_notification_production.pem'; 

$streamContext = stream_context_create(); 
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert); 

$apns = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $error, $errorString, 2, 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); 

//socket_close($apns); seems to be wrong here ... 
fclose($apns);

當我運行此腳本沒有發生。沒有錯誤只是一個空的頁面,但我的iPhone不會收到推送通知...

你有想法嗎?

非常感謝!

回答

6

在設備令牌沒有空間,帶他們

3

剝去令牌的空間,甚至那麼它不會從該網址,然後工作

$apnsHost = 'gateway.sandbox.push.apple.com'; 

刪除「沙箱」,它會運行。

2

如果你有多個設備,並且循環遍歷它們,我發現你必須爲每個fwrite創建一個新的stream_context_create,以防止蘋果關閉壞標記的連接。

FYI

4

對於開發證書,你需要的網址

'ssl://gateway.sandbox.push.apple.com:2195'
但對於生產
'ssl://gateway.push.apple.com:2195'

1

要發送通知的成功完全你必須做1兩件事。

替換$ output = json_encode($ payload);

$ payload = json_encode($ payload);

-1

您缺少可提供的證書私鑰。添加此行: stream_context_set_option($ streamContext,'ssl','passphrase',$ certificatePrivateKey);