gpt4 book ai didi

php - 针对多个 registration_ids 时哪个 fcm 注册 id 失败

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:26:50 27 4
gpt4 key购买 nike

我在 php 中从服务器端向几个注册 ID 发送通知。这是请求:

public function androidPushNotification($registration_ids, $title, $message) {
$msg = array (
'message' => $message,
'title' => $title
);

$fields = array (
'registration_ids' => $registration_ids,
'data' => $msg
);

$headers = array (
'Authorization: key=' . $this->API_ACCESS_KEY,
'Content-Type: application/json'
);

$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $this->GCM_URL );
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, json_encode ( $fields ) );
$result = curl_exec ( $ch );
curl_close ( $ch );

return $result;
}

registration_ids 变量在一个数组中有两个注册 ID,其中一个来自客户端应用程序的旧安装,另一个是当前的。

我收到了来自 fcm 的回复:

{
"multicast_id": 7860323906688398625,
"success": 1,
"failure": 1,
"canonical_ids": 0,
"results": [
{
"error": "NotRegistered"
},
{
"message_id": "0:1478735313889582%1b153de0f9fd7ecd"
}
]
}

我怎么知道哪个注册id失败了?

是否有其他方法可以获取此信息?

最佳答案

我找到了答案:

结果数组与注册 ID 的顺序相同。例如,如果请求是:

$fields = array (
'registration_ids' => array('123456','987654'),
'data' => array ('message' => 'This is the message','title' => 'Hi there!')
);

示例响应:

{
"multicast_id": 7860323906688398625,
"success": 1,
"failure": 1,
"canonical_ids": 0,
"results": [
{
"error": "NotRegistered"
},
{
"message_id": "0:1478735313889582%1b153de0f9fd7ecd"
}
]
}

注册失败的id是123456。

来自 gcm 文档:

Here are JSON results for 6 recipients (IDs 4, 8, 15, 16, 23, and 42 respectively) with 3 messages successfully processed, 1 canonical registration token returned, and 3 errors:

{ 
"multicast_id": 216,
"success": 3,
"failure": 3,
"canonical_ids": 1,
"results": [
{ "message_id": "1:0408" },
{ "error": "Unavailable" },
{ "error": "InvalidRegistration" },
{ "message_id": "1:1516" },
{ "message_id": "1:2342", "registration_id": "32" },
{ "error": "NotRegistered"}
]
}

In this example:

  • First message: success, not required.
  • Second message: should be resent (to registration token 8).
  • Third message: had an unrecoverable error (maybe the value got corrupted in the database).
  • Fourth message: success, nothing required.
  • Fifth message: success, but the registration token should be updated in the server database (from 23 to 32).
  • Sixth message: registration token (42) should be removed from the server database because the application was uninstalled from the
    device.

希望对你有帮助,谢谢。

关于php - 针对多个 registration_ids 时哪个 fcm 注册 id 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40518125/

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