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

Kam*_*mar 8 firebase google-cloud-messaging firebase-cloud-messaging

使用Firebase云消息管理员发送消息时出错API.

错误信息如下

引起:com.google.api.client.http.HttpResponseException:400 Bad Request {"error":{"code":400,"message":"Request包含无效参数.","errors":[{"消息":"请求包含无效参数.","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);
Run Code Online (Sandbox Code Playgroud)

消息发送代码如下

// 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);
Run Code Online (Sandbox Code Playgroud)

我正在使用的maven依赖关系如下

<dependency>
    <groupId>com.google.firebase</groupId>
    <artifactId>firebase-admin</artifactId>
    <version>5.9.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

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

Sel*_*jwa 9

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

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

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


Aur*_*raf 7

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

Firebase FCM:无效参数


Cor*_*son 7

尝试向 iOS 模拟器设备发送推送通知时也会发生此错误。


Bob*_*der 6

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

要确认这一点,请尝试使用setTopic("test")而不是setToken(registrationToken).