提供的主题值无效

提供的主题值无效

本文介绍了提供的主题值无效-将FCM发送到主题时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向主题发送FCM消息.但是从服务器收到提供了无效的主题值"错误.

I am trying to send an FCM message to a topic. But getting "Invalid topic value provided" error from the server.

Json有效载荷

{
   "message":{
      "topic":"/topics/news",
      "data":{
         "title":"Hellow World",
         "message":"This is the Topic Message",
         "type1":"100",
         "type2":"abc"
      }
   }
}

响应

{
   "error":{
      "code":400,
      "message":"Request contains an invalid argument.",
      "status":"INVALID_ARGUMENT",
      "details":[
         {
            "@type":"type.googleapis.com/google.rpc.BadRequest",
            "fieldViolations":[
               {
                  "field":"message.topic",
                  "description":"Invalid topic value provided."
               }
            ]
         },
         {
            "@type":"type.googleapis.com/google.firebase.fcm.v1.FcmError",
            "errorCode":"INVALID_ARGUMENT"
         }
      ]
   }
}

我尝试将主题值设置为新闻"(不带'/topics/'),但会引发相同的错误.我可以从Firebase控制台将消息发送到该主题,而不会出现任何问题.

I have tried topic value as "news" (without '/topics/') but it throws same error. I can send message to the topic from firebase console without any problem.

任何帮助表示赞赏. TIA

Any help appreciated. TIA

Edit1-下面的通知有效负载工作正常,但数据有效负载不起作用.根据文档,也允许数据有效载荷 https://firebase.google .com/docs/cloud-messaging/android/topic-messaging 请求

Edit1 - Notification payload below works fine but data payload doesn't work. As per documentation, data payloads too are allowed https://firebase.google.com/docs/cloud-messaging/android/topic-messagingRequest

{
  "message":{
    "topic" : "foo-bar",
    "notification" : {
      "body" : "This is a Firebase Cloud Messaging Topic Message!",
      "title" : "FCM Message",
      }
   }
}

这行得通.我的代码中有一个小错误,该错误为该主题添加了其他引号.下面的请求就像一个魅力

EDIT2 :This works. I had a small bug in my code which was adding additional quotes to the topic. Below request works like a charm

{
   "message":{
      "topic":"news",
      "data":{
         "title":"Hellow World",
         "message":"This is the Topic Message",
         "type1":"100",
         "type2":"abc"
      }
   }
}

推荐答案

根据 doc 您的请求应如下所示:

according to doc yor request should be like this:

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{
  "to": "/topics/foo-bar",
  "data": {
    "message": "This is a Firebase Cloud Messaging Topic Message!",
   }
}

这篇关于提供的主题值无效-将FCM发送到主题时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 14:31