gpt4 book ai didi

ios - 在IOS中从MDM服务器发送推送通知时出现推送通知错误

转载 作者:行者123 更新时间:2023-12-01 16:52:55 28 4
gpt4 key购买 nike

我正在使用移动设备管理系统,但推送通知不起作用。我的代码是

//$apnsHost = 'gateway.push.apple.com';
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'PushCert.pem';

// Put your device token here (without spaces):
$deviceToken = 'my-device-token'; //I have replaced my actual token here

// Put your alert message here:
$message = 'My first push notification!';

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $apnsCert);
//stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $ctx);
if (!$fp)
exit("Failed to connect: $error $errorString" . PHP_EOL);

echo 'Connected to APNS' . PHP_EOL;

// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);

// Encode the payload as JSON
$payload = json_encode($body);

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

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

if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp);

我收到类似 Failed to connect: 0的错误,如果我执行 var_dump($fp),则会得到 bool(false)。该问题有什么解决方案?

最佳答案

<?php

// Provide the Host Information.
$tHost = 'gateway.sandbox.push.apple.com';
$tPort = 2195;

// Provide the Certificate and Key Data.
$tCert = 'ck.pem';

// Provide the Private Key Passphrase (alternatively you can keep this secrete
// and enter the key manually on the terminal -> remove relevant line from code).
// Replace XXXXX with your Passphrase
$tPassphrase = '1234';

// Provide the Device Identifier (Ensure that the Identifier does not have spaces in it).
// Replace this token with the token of the iOS device that is to receive the notification.
$tToken = 'YOUR DEVICE TOKEN';

// The message that is to appear on the dialog.
$tAlert = 'You have a LiveCode APNS Message';

// The Badge Number for the Application Icon (integer >=0).
$tBadge = 8;

// Audible Notification Option.
$tSound = 'default';

// The content that is returned by the LiveCode "pushNotificationReceived" message.
$tPayload = 'APNS Message Handled by LiveCode';

// Create the message content that is to be sent to the device.
$tBody['aps'] = array (
'alert' => $tAlert,
'badge' => $tBadge,
'sound' => $tSound,
);
$tBody ['payload'] = $tPayload;

// Encode the body to JSON.
$tBody = json_encode ($tBody);

// Create the Socket Stream.
$tContext = stream_context_create ();
stream_context_set_option ($tContext, 'ssl', 'local_cert', $tCert);

// Remove this line if you would like to enter the Private Key Passphrase manually.
stream_context_set_option ($tContext, 'ssl', 'passphrase', $tPassphrase);

// Open the Connection to the APNS Server.
$tSocket = stream_socket_client ('ssl://'.$tHost.':'.$tPort, $error, $errstr, 30, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $tContext);

// Check if we were able to open a socket.
if (!$tSocket)
exit ("APNS Connection Failed: $error $errstr" . PHP_EOL);

// Build the Binary Notification.
$tMsg = chr (0) . chr (0) . chr (32) . pack ('H*', $tToken) . pack ('n', strlen ($tBody)) . $tBody;

// Send the Notification to the Server.
$tResult = fwrite ($tSocket, $tMsg, strlen ($tMsg));

if ($tResult)
echo 'Delivered Message to APNS' . PHP_EOL;
else
echo 'Could not Deliver Message to APNS' . PHP_EOL;

// Close the Connection to the Server.
fclose ($tSocket);
?>

试试这个代码...

如果您为PushCert.pem创建密码,则取消注释该行
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

并将密码放入$ passphase

关于ios - 在IOS中从MDM服务器发送推送通知时出现推送通知错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14231193/

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