gpt4 book ai didi

iOS推送通知在从服务器运行但在真实设备上不工作时工作

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

我正在使用 php 为 iOS 开发远程推送通知。当我使用命令 php push_notification.php 从服务器运行我的代码时( push_notification.php 是我的 api 的文件名),它正在成功发送通知。但是,当我尝试从 postman 或真实设备运行 api 时,它没有发送任何通知并给我一个错误说 Failed to connect to the APNS server. Error no = 0<br/> .这是我的代码。

<?php

/* sample device token*/
$id = '9A057567E5208D4471294A0FDFFF777EEE92E2205EBA9334C5ADE5A383F7B344';

$apnsServer = 'ssl://gateway.push.apple.com:2195';

/* Make sure this is set to the password that you set for your private key
when you exported it to the .pem file using openssl on your OS X */

$privateKeyPassword = 'password';
$message = 'Welcome to iOS 10 Push Notifications';
/* Pur your device token here */
$deviceToken = $id;
/* Replace this with the name of the file that you have placed by your PHP
script file, containing your private key and certificate that you generated
earlier */
$pushCertAndKeyPemFile = dirname(__FILE__).'/PushCertificateAndKey.cer';
$stream = stream_context_create();
stream_context_set_option($stream,
'ssl',
'passphrase',
$privateKeyPassword);
stream_context_set_option($stream,
'ssl',
'local_cert',
$pushCertAndKeyPemFile);
stream_context_set_option( $stream , 'ssl', 'verify_peer', false);

$connectionTimeout = 60;
$connectionType = STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT;
$connection = stream_socket_client($apnsServer,$errorNumber,$errorString, $connectionTimeout, $connectionType,$stream);
if (!$connection){
echo "Failed to connect to the APNS server. Error no = $errorNumber<br/>";
exit;
} else {
echo "Successfully connected to the APNS. Processing...</br>";
}
$messageBody['aps'] = array('alert' => $message,
'sound' => 'default',
'badge' => 2,
);
$payload = json_encode($messageBody);
$notification = chr(0) .
pack('n', 32) .
pack('H*', $deviceToken) .
pack('n', strlen($payload)) .
$payload;
$wroteSuccessfully = fwrite($connection, $notification, strlen($notification));
if (!$wroteSuccessfully){
echo "Could not send the message<br/>";
}
else {
echo "Successfully sent the message: ".$message."to device token: ".$id.PHP_EOL;
}
fclose($connection);
?>

我知道有很多与推送通知相关的问题。但我认为我的不同之处在于我能够从我的服务器发送通知,但不能从我的设备或 postman 发送通知。这有什么问题,为什么当我从服务器运行它而不是在真实设备上运行时它可以工作?

最佳答案

您需要检查一些事项才能正确测试 Push Notifications流动。

  • 您在使用 DevelopmentProduction服务器上用于发送 Push Notifications 的证书?
  • 您是直接从 Xcode 安装应用程序吗?或使用 Development (Debug)Production (Release) build ?
  • 还要确保您已经安装了安装在 .p12 中的 APNS 证书。在您的开发机器上格式化。

  • 您必须注意,从开发版本安装的应用程序只能接收来自具有开发证书的服务器的通知。
    而生产应用程序只能接收来自具有生产证书的服务器的通知。

    希望这可以帮助

    关于iOS推送通知在从服务器运行但在真实设备上不工作时工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60881547/

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