2013-08-05 141 views
2

我開發推送通知的iPhone應用程序,並且當我在本地主機上運行php文件時遇到了錯誤。我已經正確創建了證書.pem文件。然後當我測試我的本地主機上的應用程序時,會顯示警告。PHP上的推送通知錯誤(iOS上的簡單推送通知)


警告:stream_socket_client()[function.stream插槽客戶端]:無法設置/應用程序的私有密鑰文件'/Applications/XAMPP/xamppfiles/htdocs/PushNotificationProject/ck.pem」/XAMPP/xamppfiles/htdocs中/ PushNotificationProject/simplepush.php第21行

警告:stream_socket_client()[function.stream插槽客戶端]:無法創建/應用程序的SSL手柄/ XAMPP/xamppfiles/htdocs中/ PushNotificationProject /simplepush.php on line 21

Warning:stream_socket_client()[functio n.stream-插座客戶]:無法使加密在/Applications/XAMPP/xamppfiles/htdocs/PushNotificationProject/simplepush.php第21行

警告:stream_socket_client()[function.stream插座-客戶]:無法連接到第21行的/Applications/XAMPP/xamppfiles/htdocs/PushNotificationProject/simplepush.php中的ssl://gateway.sandbox.push.apple.com:2195(未知錯誤) 未能連接:0


我得到這個錯誤。什麼可能是這個錯誤的原因?

這是我的PHP代碼。

<?php 
// Put your device token here (without spaces): 
$deviceToken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; 
// Put your private key's passphrase here: 
$passphrase = 'xxxxxxxxxx'; 

// Put your alert message here: 
$message = 'My first push notification!'; 
//////////////////////////////////////////////////////////////////////////////// 

$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); 
?> 

請幫我解決這個錯誤。

回答

0

我看到你沒有使用任何SSL證書。首先爲您的應用創建SSL證書。

enter image description here

+0

你好@Tapas帕爾。謝謝你提醒我。我沒有使用我的SSL證書,也許我忘了它。我已經下載了名爲:aps_development.cer的文件作爲默認文件名。接下來我應該做什麼? – Jarich

+0

我認爲你已經下載了錯誤的一個SSL證書擴展名是.pem不是.cer –

+0

我可以在哪裏生成SSL證書?我在證書類別下生成它。 – Jarich

0

我有同樣的問題,it've得到解決。

請確保您的$密碼正確。 $ passphrase是.pem文件的密碼

如果可能,應該再次創建.pem。

祝你好運!

0

我有同樣的問題,它已經解決了。 plz在你的php文件中更改如下

$ passphrase ='xxxxxxxxxx';到

$ passphrase = xxxxxxxxxx;

然後嘗試發送推送通知

-1
I'm Ravi Malviya. 

Code snippets. 

//*//Notification Configration.(Can Only Test On Device) 

1.PushChatKey.certSigningRequest //Computer authentication certificate then from keychain export PushChatKey.p12 
2.Explicit App ID //create it With Enable push notifcation. 
3.aps_development.cer //create it under app id and then download it. 
4.Create developer certificate and provisioning profile on This App id 

$ openssl x509 -in aps_development.cer -inform der -out PushChatCert.pem//convert Apple Development iOS Push Services to .pem (public key) 
$ openssl pkcs12 -nocerts -out PushChatKey.pem -in PushChatKey.p12 // convert PushChatKey.p12 to .pem (public key) 

$ cat PushChatCert.pem PushChatKey.pem > ck.pem //concate pub and pri key //RSA 

$ telnet gateway.sandbox.push.apple.com 2195 //check properly APNS working or not 

$ openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushChatCert.pem -key PushChatKey.pem //Enter pass phrase for PushChatKey.pem: 

//Web server side Configration. 
1.sudo su - (create root login from login options->network Account server (click join)->open directory utility ->Edit->Enable root) 
2.Start Apache 
# apachectl start 
3.Enter in browser http://localhost 
4.Enable Php Source can get under this directory 
# cd /etc/apache2/ 
# cp httpd.conf httpd.conf.bak 
5.open httpd.conf in textMate 
vi httpd.conf 
6.remove # and save file from 
LoadModule php5_module libexec/apache2/libphp5.so 
7.Restart your apache 
# apachectl restart 
Varify php enable or not place php file in /Library/WebServer/Documents 
Open url: http://localhost/fileName.php 

//////From Command line 
cd Documents/RAndD/ZObjectiveC/ 
php simplepush.php