1

我正在为苹果设备实现推送通知服务。使用 php 脚本,只有当 php 脚本从浏览器命中时,它才会推送通知。当我通过浏览器点击服务器 php 脚本时,它会将通知推送到 Apple 设备。我的 php 脚本是...

// 在苹果设备中发送通知的方法结束

function sendNotificationToAppleDevice($token,$message)
    {
    /////////////////////////////////////////////For apple divice////////////////////////////////

$passphrase='1234';
// Create a stream to the server
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', 'sample.pem');
stream_context_set_option($streamContext, 'ssl', 'passphrase', $passphrase);

$apns = stream_socket_client( 'ssl://gateway.sandbox.push.apple.com:2195', $error,$errorString,60,STREAM_CLIENT_CONNECT, $streamContext);

// You can access the errors using the variables $error and $errorString


// Now we need to create JSON which can be sent to APNS
        $inc=0;                  
$load = array(
'aps' => array(
'alert' => $message,
'badge' =>$inc,
)
);
$inc++;
$payload = json_encode($load);
// The payload needs to be packed before it can be sent
$apnsMessage = chr(0) . chr(0) . chr(32);
$apnsMessage .= pack('H*', str_replace(' ', '', $token));
$apnsMessage .= chr(0) . chr(strlen($payload)) . $payload;

// Write the payload to the APNS

fwrite($apns, $apnsMessage);

echo "just wrote " . $payload;

// Close the connection

fclose($apns);
    }

当此脚本被浏览器命中时,此脚本运行良好并成功发送通知。

现在,当我从计划任务中的 cron 作业运行此脚本时,它不会向设备发送任何通知。它导致......

警告:stream_socket_client():SSL 操作失败,代码为 1。OpenSSL 错误消息:错误:14094410:SSL 例程:SSL3_READ_BYTES:sslv3 警报握手失败在 sample.php

警告:stream_socket_client(): 无法在线在 sample.php 中启用加密

警告:stream_socket_client(): 无法连接到 ssl://gateway.sandbox.push .apple.com:2195 (Unknown error) in sample.php 在线

警告:fwrite() 期望参数 1 是资源,在线 sample.php 中给出的布尔值

警告:fclose() 期望参数 1 是资源,在线 sample.php 中给出的布尔值

此功能和错误有什么问题..请任何人帮助我...

提前致谢。

4

1 回答 1

5

经过大量研究后,我终于找到了漏洞在哪里..

您只需更改一行即可将此方法与 cron 作业一起使用..:-)

更改此行

stream_context_set_option($streamContext, 'ssl', 'local_cert', 'sample.pem');

stream_context_set_option($streamContext, 'ssl', 'local_cert', 'your FULL path of pem file');

对于我的情况,我使用

stream_context_set_option($streamContext, 'ssl', 'local_cert', 'C:\Websites\games\project_name\sample.pem');

仅此而已...希望您的问题现在可以解决..

于 2013-04-01T10:49:16.447 回答