gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:58:11 25 4
gpt4 key购买 nike

我正在为苹果设备实现推送通知服务。使用 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 作业运行此脚本时,它不会向设备发送任何通知。它导致......

Warning: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages: error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure in sample.php

Warning: stream_socket_client(): Failed to enable crypto in sample.php on line

Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push .apple.com:2195 (Unknown error) in sample.php on line

Warning: fwrite() expects parameter 1 to be resource, boolean given in sample.php on line

Warning: fclose() expects parameter 1 to be resource, boolean given in sample.php on line



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

提前致谢。

最佳答案

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

您只需更改一行即可将此方法与 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');

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

关于使用计划任务的 PHP 服务器脚本的 Ios 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13890334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com