【问题标题】:Firebase Cloud Messaging: Add a device to a topic using curl commandFirebase 云消息传递:使用 curl 命令将设备添加到主题
【发布时间】:2016-12-13 20:51:06
【问题描述】:

现在我可以使用 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"}

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

谁能帮我找到解决问题的方法?

【问题讨论】:

    标签: php curl firebase firebase-cloud-messaging


    【解决方案1】:

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

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

    【讨论】:

    • 不,这不会改变任何事情...关于 FCM 的文档,我完全不确定 IID-TOKEN 是否与从 messaging.getToken() 返回的令牌相同...
    • @PM84 加了rel还是不行?唔。好吧,它应该与消息传递令牌相同。他们确实在文档中提到了 registration token。和 AFAIK,没有其他类型的令牌。您是否尝试过对令牌进行编码?如果它仍然不起作用,那么它已经超出了预期的行为。我建议联系 Firebase 支持以获取更多帮助。干杯!
    【解决方案2】:

    由于某种原因,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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-22
      • 1970-01-01
      • 2016-10-30
      • 2021-02-28
      相关资源
      最近更新 更多