【问题标题】:Invalid topic value provided - While sending FCM to a topic提供的主题值无效 - 将 FCM 发送到主题时
【发布时间】:2018-07-03 08:11:14
【问题描述】:

我正在尝试向主题发送 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",
      }
   }
}

编辑2: 这行得通。我的代码中有一个小错误,它为该主题添加了额外的引号。下面的请求就像一个魅力

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 我指的是这个。 firebase.google.com/docs/cloud-messaging/android/… 能否请您修复评论中的链接。谢谢
    • 使用您提到的请求格式给出错误 - “收到无效的 JSON 有效负载。未知名称 \"to\": 找不到字段。\n收到无效的 JSON 有效负载。未知名称 \"data\": 不能查找字段。",
    • 试试这个:{ "to": "/topics/news", "data": { "title":"Hellow World", "message":"This is the Topic Message", " type1":"100", "type2":"abc" } }
    猜你喜欢
    • 2018-03-22
    • 1970-01-01
    • 2017-04-26
    • 2017-06-27
    • 2019-11-06
    • 1970-01-01
    • 2019-04-16
    • 2021-11-03
    • 2017-05-07
    相关资源
    最近更新 更多