首页 文章

发送邮件时Google FCM Invalid_argument

提问于
浏览
5

我正在尝试弄清楚如何使用Firebase向Android发送推送通知,并使用旧版HTTP(https://fcm.googleapis.com/fcm/send)使其工作,但文档建议使用较新的 endpoints (https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send) . 我似乎无法让它工作,因为我一直得到这样的回应:

{
    "error": {
        "code": 400,
        "message": "Request contains an invalid argument.",
        "status": "INVALID_ARGUMENT"
    }
}

使用https://firebase.google.com/docs/cloud-messaging/send-message#send_messages_to_specific_devices处的示例会发生这种情况:

POST https://fcm.googleapis.com/v1/projects/project-916177026973/messages:send HTTP/1.1
cache-control: no-cache
Postman-Token: 81403929-77ba-4568-8681-a854527ccb22
Content-Type: application/json
Authorization: Bearer <token redacted>
User-Agent: PostmanRuntime/6.4.1
Accept: */*
Host: fcm.googleapis.com
accept-encoding: gzip, deflate
content-length: 319
Connection: close

{
  "message":{
    "token" : <token redacted>,
    "notification" : {
      "body" : "This is an FCM notification message!",
      "title" : "FCM Message",
      }
   }
}

我还尝试删除最后一个逗号以使其符合JSON并仍然没有运气 . 有任何想法吗?

1 回答

  • 3

    正如OP指出Firebase现在建议FCM v1 endpoints ,如果你想通过HTTP发送消息 .

    要通过HTTP发送消息,请将HTTP POST请求发送到FCM v1 endpoints 并指定send方法 . endpoints URL必须包含应用的Firebase项目的项目ID,可在Firebase控制台的常规项目设置选项卡中找到 .

    它看起来像这样:

    POST https://fcm.googleapis.com/v1/projects/your_project_id_here/messages:send HTTP/
    

    URL your_project_id_here 的部分是 your 项目ID . 您需要在选项卡 General 下找到项目特定的项目 project Id ,该项目位于Firebase控制台的 Settings 部分 .

    寻找 Project ID . 如果您对正确的 Project ID 有任何疑问,如果网站中的URL指向您的项目,它也是一个部分 . 看起来像:

    https://console.firebase.google.com/project/your_project_id_here/settings/general/android:your_project_name
    

相关问题