【问题标题】:Apple APNs push notification is not workingApple APNs 推送通知不起作用
【发布时间】:2021-09-17 02:15:37
【问题描述】:

在过去的 7 年里,我的团队一直在使用 Apple 推送通知,这就像一个宝石一样工作。最近生成的开发证书和 App Store 证书中的构建都没有收到通知。

通知是从 PHP 代码发送的。

<?php

// Put your device token here (without spaces):
$deviceToken = '****************';
// Put your private key's passphrase here:
$passphrase = '******';

// Put your alert message here:
$message = 'Hi User!';
//$message =$_REQUEST['message'];
////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'cert.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

/* Open a connection to the APNS server*/
 $fp = stream_socket_client(
 'ssl://gateway.push.apple.com:2195', $err,
 $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
    exit("Failed to connect: $err $errstr" . 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
echo $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);
?>

推送通知结果为成功。但是应用程序没有收到通知。这段代码有新的更新吗?在设备上接收通知是否有可能出错?

【问题讨论】:

    标签: ios push-notification notifications apple-push-notifications apns-php


    【解决方案1】:

    如果您使用 p12 或 pem 文件发送推送,则会停止。

    你必须使用新的方式来使用 P8 文件

    查看以下链接了解更多详情。

    https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns/

    【讨论】:

    • 我们正在使用 pem 文件,但我不知道它已停止。生产中预先存在的 pem 文件正在运行(可能仍然支持旧的 pem 文件)。我很早就看到了链接,其中没有关于 pem、p12 或 p8 文件的信息。当 Apple 停止对 pem 文件的支持时,您能帮我详细说明一下吗
    猜你喜欢
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    相关资源
    最近更新 更多