2013-08-05 119 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