我正在为苹果设备实现推送通知服务。使用 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 中给出的布尔值
此功能和错误有什么问题..请任何人帮助我...
提前致谢。