gpt4 book ai didi

ios - codeigniter - 连接失败 : 0 while sending ios notifications

转载 作者:行者123 更新时间:2023-11-29 05:30:11 25 4
gpt4 key购买 nike

我正在尝试从 PHP Code Igniter 框架向 iOS APNS 发送远程推送通知。

为此,作为一种实践,我编写了核心文件,并在同一帐户上,当我在 Controller 功能中添加相同的代码时,它运行良好,它给了我:

"Failed to connect: 0".

经过大量研究后,我了解到这一行:

$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);

为什么相同的代码可以在核心 PHP 中运行,但不能在 CI 中运行?

我尝试提供 .pem 文件的完整路径,如下所示 Failed to connect: 0 push notification

但运气不佳。

请帮助我摆脱这种沮丧。

public function sendIosPushNotifications()
{
// set time limit to zero in order to avoid timeout
set_time_limit(0);

// charset header for output
header('content-type: text/html; charset: utf-8');

// this is the pass phrase you defined when creating the key
$passphrase = '';

// you can post a variable to this string or edit the message here
if (!isset($_POST['msg'])) {
$_POST['msg'] = "Notification message here from gokul sxsxsxs!";
}

// tr_to_utf function needed to fix the Turkish characters
$message = $this->tr_to_utf($_POST['msg']);

// load your device ids to an array
$deviceIds = array(
'6a774e1779d1c1932c112ff7a45c337db96605942acbdb45d5c386a329bda381'
);

// this is where you can customize your notification
$payload = '{"aps":{"alert":"' . $message . '","sound":"default"}}';

$result = 'Start' . '<br />';


// start to create connection
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'Certificates123.pem');
//stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

echo count($deviceIds) . ' devices will receive notifications.<br />';

foreach ($deviceIds as $item) {
// wait for some time
sleep(1);

// 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" . '<br />');
} else {
echo 'Apple service is online. ' . '<br />';
}

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $item) . pack('n', strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result) {
echo 'Undelivered message count: ' . $item . '<br />';
} else {
echo 'Delivered message count: ' . $item . '<br />';
}

if ($fp) {
fclose($fp);
echo 'The connection has been closed by the client' . '<br />';
}
}

echo count($deviceIds) . ' devices have received notifications.<br />';

// function for fixing Turkish characters


// set time limit back to a normal value
set_time_limit(30);
}

最佳答案

这是路径问题...将您的证书文件放在 CI 根目录中..并给出类似 ./certificate/filename.pem 的路径,并且它有效

关于ios - codeigniter - 连接失败 : 0 while sending ios notifications,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57673567/

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