Firebase云消息传递:使用curl命令将设备添加到主题

时间:2016-12-13 20:51:06

标签: php curl firebase firebase-cloud-messaging

现在我可以使用curl命令向单个设备发送通知:

curl --header "Authorization: key=AAAxxxxxE4:xxxxxxxuXog" --header Content-Type:"application/json; application/x-www-form-urlencoded;charset=UTF-8" -d '{"to":"DeviceToken","notification":{"title":"Test","body":"Test Message","icon":"icon-192x192.png"}}' https://fcm.googleapis.com/fcm/send 

这很有效。现在,我尝试使用主题功能来扩展收件人数量。

我尝试使用curl命令将设备添加到主题“svp”:

curl --header "Authorization: key=AAAxxxxxE4:xxxxxxxuXog" --header Content-Type:"application/json" https://iid.googleapis.com/iid/v1/DeviceToken/topics/svp

这里我收到错误消息:

{"error":"InvalidToken"}

但我确定,授权密钥和令牌都是正确的。 (我仍然可以发送通知)。

有人可以帮我找到解决问题的方法吗?

2 个答案:

答案 0 :(得分:1)

我认为您错过了cURL请求中的rel,在令牌和主题名称之间。如Instance ID docs所示,格式应为:

https://iid.googleapis.com/iid/v1/IID_TOKEN/rel/topics/TOPIC_NAME

答案 1 :(得分:0)

由于某些原因,https://iid.googleapis.com/iid/v1/IID_TOKEN/rel/topics/TOPIC_NAME对我不起作用。在我看来,我所做的一切都正确,但是我决定做的就是尝试使用https://iid.googleapis.com/iid/v1:batchAdd并仅发送一个令牌来进行等效操作:

$curlUrl = "https://iid.googleapis.com/iid/v1:batchAdd";
$mypush = array("to"=>"/topics/toronto", "registration_tokens"=>array("acwAw4F8b0W:AMA91bEAKsN1aGjMm5xsobxBHxbYZLfioTPMEIN90njdiK5C2MnOYF4NcOy6ot6BFanMTBIoKRGcyev2RJuydGWt1XHwsniNZ6h8Pjvn9Fqth-Mqgj_5-YN9pt_nMAtgG8blc5bodWyA"));
$myjson = json_encode($mypush);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $curlUrl);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_POST, True);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myjson);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Authorization:key=[My authorization key]'));
//getting response from server
$response = curl_exec($ch);

我可以将令牌成功添加到主题。返回的内容是:

{"results":[{}]}

但是后来我通过使用https://iid.googleapis.com/iid/info/IID_TOKEN来确认该主题,该令牌已按照我想要的方式成功添加到了该主题中,是的,使用https://iid.googleapis.com/iid/info/IID_TOKEN一切对我来说都是正确的。因此,如果您在https://iid.googleapis.com/iid/v1/IID_TOKEN/rel/topics/TOPIC_NAME方面遇到问题,可以按照我的方式简单地使用https://iid.googleapis.com/iid/v1:batchAdd

相关问题