php - 从 PHP 发送时未收到 APNS 推送通知。我该如何调试这个问题?
问题描述
您好我正在使用以下代码使用 PHP 发送 APNS 推送通知,但设备上未收到通知。
我使用这个工具来验证我是否有错误的令牌或者我的 .pem 文件是否有问题 - 但如果我在这里测试它:https ://www.pushtry.com/一切正常。
function sendAPNS($message, $id)
{
/* We are using the sandbox version of the APNS for development. For production
environments, change this to ssl://gateway.push.apple.com:2195 */
$apnsServer = 'ssl://gateway.sandbox.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 = '';
$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 = 'push.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/>";
}
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));
var_dump($wroteSuccessfully);
if (!$wroteSuccessfully){
echo "Could not send the message<br/>";
}
else {
echo "Successfully sent the message<br/>";
}
fclose($connection);
}
PHP的输出是:
成功连接到 APNS。处理中... int(140) 成功发送消息
我该如何调试这个问题?
解决方案
推荐阅读
- android - Checkbox 自动显示为在 ExpandableListView 中选中
- c# - 使用 Web 服务在 xml 节点中搜索
- python - 如何在视图中使用游标显示查询集结果
- ansible - 如何访问动作插件中的 ansible 变量(不提供它们作为参数)
- python - pd.to_csv 保存,但显然是错误的数据(根据打印功能)
- c# - 忽略尚未通过 EntityTypeConfiguration<> 配置的导航属性
- css - 如何在 mvc 下拉列表中设置特定项目的样式
- c# - 将 DbContext 与数据库中的更改同步
- android - 实现视频过滤并保存过滤后的视频
- django - ModuleNotFoundError: No module named ,在 Django 应用程序中找不到导致错误的原因