Kamran Omar
Kamran Omar

Reputation: 1897

firebase cloud messaging Request contains an invalid argument

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?

Upvotes: 20

Views: 36253

Answers (8)

Cory Robinson
Cory Robinson

Reputation: 5272

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

Upvotes: 17

berkaycelebi
berkaycelebi

Reputation: 31

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

For example wrong: 03:00' in topics

Upvotes: 0

joel.d
joel.d

Reputation: 1631

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.

Upvotes: 0

J. Doe
J. Doe

Reputation: 13083

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

Upvotes: 0

Sello Mkantjwa
Sello Mkantjwa

Reputation: 1915

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

Upvotes: 17

J M Rossy
J M Rossy

Reputation: 1023

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

Upvotes: 0

Aur Saraf
Aur Saraf

Reputation: 3460

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

Upvotes: 7

Bob Snyder
Bob Snyder

Reputation: 38319

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

Upvotes: 7

Related Questions