【发布时间】:2021-12-24 10:33:42
【问题描述】:
我向我的应用程序发送推送通知的能力运行良好;有一天,这些设备刚刚停止接收它们。我已经测试了我的开发和生产证书,并且都完美地连接到了 APNS,并告诉我我的通知已发送。也就是说,没有设备(即使是通过设备令牌指定的设备)收到通知?
这是我用来连接 APNS 并发送消息的 PHP:
PushNotify.php
<?php
$apnsServer = 'api.sandbox.push.apple.com:443';
/* 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 = '0000001';
/* Put your own message here if you want to */
$message = 'Hey there it's today';
/* Pur your device token here */
$deviceToken =
'2374387438mytoken';
/* 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 = 'apns-development.pem';
$stream = stream_context_create();
stream_context_set_option($stream,
'ssl',
'passphrase',
$privateKeyPassword);
stream_context_set_option($stream,
'ssl',
'local_cert',
$pushCertAndKeyPemFile);
$connectionTimeout = 20;
$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<br/>";
}
fclose($connection);
?>
再次返回成功消息,但未收到任何通知:/ 任何帮助表示感谢,这让我大吃一惊!我至少重新创建了 5 次证书(即使我已经确认它们正在连接到 APNS)。
【问题讨论】:
标签: ios apple-push-notifications apns-php