gpt4 book ai didi

php - "error": "InvalidRegistration" while sending notification from PHP

转载 作者:行者123 更新时间:2023-12-05 03:43:32 31 4
gpt4 key购买 nike

使用 registeration_ids 和参数尝试提到的代码,但两者均无效。帮我解决同样的问题。每次它给出多播 ID 和 InvalidRegistration 的错误。收到此错误:{"multicast_id":8367359766XXXXXXXXX,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

function send_notification () {
$fcmUrl = 'https://fcm.googleapis.com/fcm/send';
$args = func_get_args();
$response = ["status" => 0, "message" => "Notification couldn't be sent"];

$title = "Hey test notification";
$body = "";
$apiKey = "QWERTYHHVCFVBVBN";//Server key under the Project settings
$tokenArr = ["XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"];
$refId = 123;
$msgNotification = [
"title" => $title,
"body" => $body
];
$extraNotificationData = [
"refId" => $refId,
"title" => $title
];

$fcmNotification = [
"registration_ids" => $tokenArr,
"notification" => $msgNotification,
"data" => $extraNotificationData
];
$headers = [
"Authorization: key=" . $apiKey,
"Content-Type: application/json"
];

$encodedData = json_encode($fcmNotification);
// echo "<pre>";print_r($fcmNotification);exit;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$fcmUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encodedData);
$result = curl_exec($ch);
if ($result === FALSE) {
die("Curl failed: " . curl_error($ch));
}
print_r($result);exit;
curl_close($ch);
$response = ["status" => 1, "message" => "Notification sent to users", "payload" => $result];

return $response;
}

最佳答案

无效注册通常有以下原因之一:

  • 注册 token /设备 token 无效
  • 客户端应用已主动注销 FCM
  • 客户端应用已自动注销 FCM,因为该应用已被卸载。
  • 设备 token 已过期 - 这可能在 Google 刷新设备 token 时发生,或者对于 iOS 设备,当 APNs token 已过期时
  • API key 与您向其发送消息的设备属于不同的 Firebase 项目。出现这种情况的一个常见原因是,当您使用多个环境时,例如, token 已在生产环境中注册,但您正尝试从临时环境发送消息。

在所有情况下,除了最后一种情况,您都可以从服务器中删除注册 token 并停止向其发送通知。

$fcmUrl = 'https://fcm.googleapis.com/fcm/send'; 表明您正在使用旧版 FCM API 发送消息。请注意,除非您依赖设备组通知,否则 Google 建议 migrating to the newer HTTP v1 API .

关于php - "error": "InvalidRegistration" while sending notification from PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66734924/

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