Ios使用计划任务使用PHP服务器脚本推送通知

时间:2012-12-15 07:32:29

标签: ios cron push-notification

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

//在APPLE设备中发送消息的方法结束

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:sample.php中的sslv3警报握手失败

     

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

     

警告:stream_socket_client():无法连接   ssl://gateway.sandbox.push .apple.com:2195(未知错误)in   sample.php在线

     

警告:fwrite()要求参数1为资源,布尔值为   sample.php在线

     

警告:fclose()要求参数1为资源,布尔值为   sample.php在线

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

提前致谢。

1 个答案:

答案 0 :(得分: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');

这就是......希望你的问题现在能够解决..