【问题标题】:firebase cloud messaging Request contains an invalid argumentfirebase 云消息传递请求包含无效参数
【发布时间】:2018-03-06 06:11:32
【问题描述】:

使用Firebase 云消息管理员API 发送消息时出现错误。

错误信息如下

原因:com.google.api.client.http.HttpResponseException: 400 Bad Request { “错误”: { “代码”:400, "message": "请求包含无效参数。", “错误”:[ { "message": "请求包含无效参数。", “域”:“全球”, “原因”:“错误请求” } ], “状态”:“INVALID_ARGUMENT” } }

让我把我的管理员配置放在这里..

FileInputStream serviceAccount = new FileInputStream("My service accout file.json");

FirebaseOptions options = new FirebaseOptions.Builder().setCredentials(GoogleCredentials.fromStream(serviceAccount))
                    .setDatabaseUrl("https://deliveryeat-1aa42.firebaseio.com").build();

FirebaseApp.initializeApp(options);

消息发送代码如下

// This registration token comes from the client FCM SDKs.
String registrationToken = "YOUR_REGISTRATION_TOKEN";

// See documentation on defining a message payload.
Message message = Message.builder().putData("score", "850").putData("time", "2:45").setToken(registrationToken).build();

// Send a message to the device corresponding to the provided
// registration token.
String response = FirebaseMessaging.getInstance().sendAsync(message).get();
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);

我正在使用的 maven 依赖项如下

<dependency>
    <groupId>com.google.firebase</groupId>
    <artifactId>firebase-admin</artifactId>
    <version>5.9.0</version>
</dependency>

那么任何人都可以帮助我吗?我做错了什么?

【问题讨论】:

    标签: firebase google-cloud-messaging firebase-cloud-messaging


    【解决方案1】:

    另一个原因是您的消息太大:

    通知消息可以包含可选的数据负载。两种消息类型的最大有效负载均为 4KB,但从 Firebase 控制台发送消息时除外,该控制台强制限制为 1024 个字符。

    https://firebase.google.com/docs/cloud-messaging/concept-options

    【讨论】:

    • 感谢您,我的有效负载大小超过了 4KB。减少它的工作!
    【解决方案2】:

    造成这种情况的一个可能原因是客户端和服务器连接到不同的 firebase 项目。项目名称出现在客户端的 google-services.json 文件和服务器的凭据 json 中。

    Firebase FCM: invalid-argument

    【讨论】:

      【解决方案3】:

      我怀疑您的 registrationToken 格式无效。它应该是 152 个字符。

      要确认这一点,请尝试使用setTopic("test") 而不是setToken(registrationToken) 来构建您的消息。

      【讨论】:

      • 谢谢鲍勃。它正在使用 setTopic。但它仍然无法与registrationToken一起使用。任何想法。
      • 你是如何获得token的?是 152 个字符吗?
      • 我只是放了虚拟令牌。
      • 例如12345678911111234567891111123456789111112345678911111234567891111123456789111112345678911111234567891111123456789111112345678911111234567891111123456789 跨度>
      • 它必须是从客户端 SDK 之一获得的有效令牌。例如on Android,方法为FirebaseInstanceId.getInstance().getToken()。在 iOS 上获取令牌的文档are here.
      【解决方案4】:

      就我而言,问题在于通知数据有效负载中不允许使用某些键。具体来说,禁止使用“from”键。

      Firebase data message payload

      【讨论】:

        【解决方案5】:

        我的项目名称在 URL 中是大写的(奇怪,因为我的项目名称实际上是大写的)

        【讨论】:

          【解决方案6】:

          我在尝试将 'content_available' 标志设置为 true 时收到此错误(这是文档所说的:https://firebase.google.com/docs/cloud-messaging/http-server-ref)。

          事实证明,您需要将其改为“内容可用”。

          【讨论】:

            猜你喜欢
            • 2019-01-20
            • 2021-02-11
            • 1970-01-01
            • 2018-12-28
            • 1970-01-01
            • 2019-06-12
            • 1970-01-01
            • 2019-07-12
            • 1970-01-01
            相关资源
            最近更新 更多