从 gcm 迁移到 fcm 后推送通知不起作用
push notifications are not working after migrating to fcm from gcm
我已根据这些指南https://developers.google.com/cloud-messaging/ios/ios-migrate-fcm 将我的代码从 apns 迁移到 fcm。更新代码后,推送通知不起作用。
我已经创建了证书 .p12 和 .pem 文件,并使用我以前的 apns 相关代码测试了这两个文件。使用旧代码时,证书就可以了,推送通知会发送到客户端设备。但是当我用新的 fcm 相关代码测试这些证书时,没有出现推送通知。我还更改了我的服务器端点。我尝试从 firebase 控制台发送通知,它也工作正常。
我需要更改我的服务器上的某些内容吗?
问题终于解决了。我需要更改我的服务器端代码。使用 FCM,服务器端不再需要 .pem 证书(php 脚本)。我所做的是,
$url = "https://fcm.googleapis.com/fcm/send";
$token = "";
$serverKey = '';
$title = "Title";
$body = "Body of the message";
$notification = array('title' =>$title , 'text' => $body, 'sound' => 'default', 'badge' => '1');
$arrayToSend = array('to' => $token, 'notification' => $notification,'priority'=>'high');
$json = json_encode($arrayToSend);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key='. $serverKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
"POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
//Send the request
$response = curl_exec($ch);
//Close request
if ($response === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
来源:https://www.cumulations.com/blogs/87/how-to-send-push-notifications-in-php-to-ios-devices-using-fcm
我已根据这些指南https://developers.google.com/cloud-messaging/ios/ios-migrate-fcm 将我的代码从 apns 迁移到 fcm。更新代码后,推送通知不起作用。
我已经创建了证书 .p12 和 .pem 文件,并使用我以前的 apns 相关代码测试了这两个文件。使用旧代码时,证书就可以了,推送通知会发送到客户端设备。但是当我用新的 fcm 相关代码测试这些证书时,没有出现推送通知。我还更改了我的服务器端点。我尝试从 firebase 控制台发送通知,它也工作正常。
我需要更改我的服务器上的某些内容吗?
问题终于解决了。我需要更改我的服务器端代码。使用 FCM,服务器端不再需要 .pem 证书(php 脚本)。我所做的是,
$url = "https://fcm.googleapis.com/fcm/send";
$token = "";
$serverKey = '';
$title = "Title";
$body = "Body of the message";
$notification = array('title' =>$title , 'text' => $body, 'sound' => 'default', 'badge' => '1');
$arrayToSend = array('to' => $token, 'notification' => $notification,'priority'=>'high');
$json = json_encode($arrayToSend);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key='. $serverKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
"POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
//Send the request
$response = curl_exec($ch);
//Close request
if ($response === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
来源:https://www.cumulations.com/blogs/87/how-to-send-push-notifications-in-php-to-ios-devices-using-fcm