firebase 云消息传递请求包含无效参数

firebase cloud messaging Request contains an invalid argument

我在使用 Firebase 云消息管理 API 发送消息时遇到错误。

错误信息如下

Caused by: com.google.api.client.http.HttpResponseException: 400 Bad Request { "error": { "code": 400, "message": "Request contains an invalid argument.", "errors": [ { "message": "Request contains an invalid argument.", "domain": "global", "reason": "badRequest" } ], "status": "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>

所以有人可以帮助我吗?我做错了什么?

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

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

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

Firebase FCM: invalid-argument

在我的例子中,问题是通知数据负载中不允许使用某些键。具体来说,密钥 'from' 是禁止的。

Firebase data message payload

另一个原因是您的邮件太大:

Notification messages can contain an optional data payload. Maximum payload for both message types is 4KB, except when sending messages from the Firebase console, which enforces a 1024 character limit.

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

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

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

原来你需要它 'content-available' 而不是。