PHP上的推送通知错误(iOS上的简单推送通知)

时间:2013-08-05 17:14:11

标签: php ios iphone ipad push-notification

开发推送通知的iPhone应用程序,我在本地主机上运行php文件时遇到错误。我已正确创建了证书,.pem文件。然后当我在我的localhost主机上测试应用程序时,会显示警告。


  

警告:stream_socket_client()[function.stream-socket-client]:无法在/ Applications / XAMPP / xamppfiles / htdocs /中设置私钥文件`/Applications/XAMPP/xamppfiles/htdocs/PushNotificationProject/ck.pem'第21行的PushNotificationProject / simplepush.php

     

警告:stream_socket_client()[function.stream-socket-client]:无法在第21行的/Applications/XAMPP/xamppfiles/htdocs/PushNotificationProject/simplepush.php中创建SSL句柄

     

警告:stream_socket_client()[function.stream-socket-client]:无法在第21行的/Applications/XAMPP/xamppfiles/htdocs/PushNotificationProject/simplepush.php中启用加密

     

警告:stream_socket_client()[function.stream-socket-client]:无法连接到/ Applications / XAMPP / xamppfiles / htdocs中的ssl://gateway.sandbox.push.apple.com:2195(未知错误)第21行的/PushNotificationProject/simplepush.php   连接失败: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);
?>

请帮我解决这个错误。

3 个答案:

答案 0 :(得分:0)

我看到您没有使用任何SSL证书。首先为您的应用创建SSL证书。

enter image description here

答案 1 :(得分:0)

我有同样的问题,已经解决了。

确保您的$密码是正确的。 $ passphrase是.pem文件的密码

如果可能,您应该再次创建.pem。

祝你好运!

答案 2 :(得分:-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