firebase cloud messaging Request contains an invalid argument
Asked Answered
W

8

17

I am getting error while sending message using Firebase cloud messaging admin API.

Error message is below

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" } }

Let me put my admin configuration here..

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);

Message sending code is below

// 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 dependencies that i am using is following

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

So can anyone help me in this? What am I doing wrong?

Woodie answered 6/3, 2018 at 6:11 Comment(0)
H
7

I suspect that your registrationToken has an invalid format. It should be 152 characters.

To confirm that, try building your message with setTopic("test") instead of setToken(registrationToken).

Hartman answered 6/3, 2018 at 23:43 Comment(7)
Thank you Bob. it is working with setTopic. But it still not working with registrationToken. any idea.Woodie
How are you obtaining the token? Is it 152 characters?Hartman
i just put dummy token.Woodie
e.g. 12345678911111234567891111123456789111112345678911111234567891111123456789111112345678911111234567891111123456789111112345678911111234567891111123456789Woodie
It has to be a valid token obtained from one of the client SDKs. For example, on Android, the method is FirebaseInstanceId.getInstance().getToken(). The docs for getting the token on iOS are here.Hartman
it is working for the following token... fESEZ9zwM-Y:APB91bGfLrfYavY_j-YM6hOEqs_-KEIuozw-BdjIfxeeGdFit1CQEBDftW8tLMrVCubb6XZ-hLjPx3YPF6KGuR0ve3Pm_fMkDiqQdeLN0QA2HXPPYviCTI93RAhWVBH8gdJo9joM9cvLWoodie
@BobSnyder i am using firebase's await messaging().getToken() it always gives me token with 162 characters. What can i do in this situation? I am also getting the Request contains an invalid argument. while sending push notification from IOS simulator in react nativeEncephalogram
A
17

Yet another cause of this is that your message is too large:

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

Assegai answered 27/6, 2019 at 15:18 Comment(1)
Thanks for this, my payload size exceeded 4KB. Reducing it worked!Granvillegranvillebarker
H
13

This error also occurs when trying to send a push-notification to an iOS simulator device.

Hydrometer answered 2/12, 2022 at 17:25 Comment(1)
How to fix that? if i want to test from IOS simulator?Encephalogram
H
7

I suspect that your registrationToken has an invalid format. It should be 152 characters.

To confirm that, try building your message with setTopic("test") instead of setToken(registrationToken).

Hartman answered 6/3, 2018 at 23:43 Comment(7)
Thank you Bob. it is working with setTopic. But it still not working with registrationToken. any idea.Woodie
How are you obtaining the token? Is it 152 characters?Hartman
i just put dummy token.Woodie
e.g. 12345678911111234567891111123456789111112345678911111234567891111123456789111112345678911111234567891111123456789111112345678911111234567891111123456789Woodie
It has to be a valid token obtained from one of the client SDKs. For example, on Android, the method is FirebaseInstanceId.getInstance().getToken(). The docs for getting the token on iOS are here.Hartman
it is working for the following token... fESEZ9zwM-Y:APB91bGfLrfYavY_j-YM6hOEqs_-KEIuozw-BdjIfxeeGdFit1CQEBDftW8tLMrVCubb6XZ-hLjPx3YPF6KGuR0ve3Pm_fMkDiqQdeLN0QA2HXPPYviCTI93RAhWVBH8gdJo9joM9cvLWoodie
@BobSnyder i am using firebase's await messaging().getToken() it always gives me token with 162 characters. What can i do in this situation? I am also getting the Request contains an invalid argument. while sending push notification from IOS simulator in react nativeEncephalogram
D
7

One possible cause for this is that the client and server are connected to different firebase projects. Project name appears in the google-services.json file on the client and in the credentials json on the server.

Firebase FCM: invalid-argument

Dunsinane answered 28/6, 2018 at 8:50 Comment(0)
J
0

In my case, the problem was that certain keys are not allowed in a notification data payload. Specifically, the key 'from' is forbidden.

Firebase data message payload

Jeopardous answered 12/11, 2018 at 23:1 Comment(0)
C
0

I had my project name uppercased in the URL (strange, because my project name is actually uppercased)

Crabbing answered 1/1, 2021 at 20:9 Comment(0)
C
0

I received this error trying to set the 'content_available' flag to true (which is what the docs say to do: https://firebase.google.com/docs/cloud-messaging/http-server-ref).

Turns out you need it to be 'content-available' instead.

Coracorabel answered 18/2, 2021 at 21:26 Comment(0)
S
0

In my case the problem was in Condition part. I used invalid characters.

For example wrong: 03:00' in topics

Solange answered 14/11, 2022 at 11:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.