2013-10-16 48 views
0

我有一個簡單的phonegap應用程序來測試推送通知。我使用phonegap構建來構建應用程序,然後使用iTunes在我的iphone上安裝應用程序。推送通知的註冊在生產推送證書pem中成功。但在執行下面的php腳本後,我無法收到任何推送通知。腳本本身運行良好,沒有任何錯誤。任何想法可能會出錯?phonegap構建:不接收ios推送通知

// This this a fake device id: 
$deviceToken = '9870h8v088bj29u080af894jj67klfgcv9mmm79k8e4l23456h908743n093e359'; 

// fake password: 
$passphrase = '123456'; 

// Put your alert message here: 
$message = 'New Message'; 
    //////////////////////////////////////////////////////////////////////////////// 

$ctx = stream_context_create(); 
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); 
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); 

// 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); 

echo 'Connected to APNS' . PHP_EOL; 

// Create the payload body 
$body['aps'] = array(
        'alert' => $message, 
        'sound' => 'default', 
        'badge' => '1' 
        ); 

// 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 
echo 'Message successfully delivered' . PHP_EOL; 

// Close the connection to the server 
fclose($fp); 

?> 
+1

你說'推送通知的註冊成功與生產推送證書',但你連接到沙箱服務器 - 'ssl://gateway.sandbox.push.apple.com:2195'。你在沙箱或生產中測試這個嗎? – Eran

+0

謝謝,我也注意到了。它在我將其更改爲生產服務器後生效。 – Vaeianor

+0

@Vaeianor:我有同樣的問題,你的解決方案是什麼?請在這裏發帖。 – Ved

回答

1

我也經歷了這個問題,但我已經得到了解決方案。要將通知發送到設備您必須在防火牆中啓用2195端口。只需讓您的服務器公司啓用此端口即可。我從hostgator有服務器,我打電話給他們,他們啓用了端口,現在工作正常。