Invalid topic value provided - While sending FCM to a topic

萝らか妹 提交于 2019-12-25 00:01:45

问题


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

Json Payload

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

Response

{  
   "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"
         }
      ]
   }
}

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.

Any help appreciated. TIA

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-messaging Request

{
  "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"
      }
   }
}

回答1:


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!",
   }
}


来源:https://stackoverflow.com/questions/51149628/invalid-topic-value-provided-while-sending-fcm-to-a-topic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!