提供的主题值无效-向主题发送FCM时

时间:2018-07-03 08:11:14

标签: android firebase push-notification firebase-cloud-messaging

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

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控制台将消息发送到主题,而不会出现任何问题。

任何帮助表示赞赏。 TIA

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

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

EDIT2: 这可行。我的代码中有一个小错误,正在向该主题添加其他引号。下面的请求就像一个魅力

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

1 个答案:

答案 0 :(得分:0)

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

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