15

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?

KENdi
  • 7,576
  • 2
  • 16
  • 31
Kamran Omar
  • 1,815
  • 9
  • 31
  • 49

8 Answers8

14

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

Sello Mkantjwa
  • 1,798
  • 1
  • 20
  • 36
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

Aur Saraf
  • 3,214
  • 1
  • 26
  • 15
6

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

Bob Snyder
  • 37,759
  • 6
  • 111
  • 158
  • Thank you Bob. it is working with setTopic. But it still not working with registrationToken. any idea. – Kamran Omar Mar 07 '18 at 02:37
  • How are you obtaining the token? Is it 152 characters? – Bob Snyder Mar 07 '18 at 02:39
  • i just put dummy token. – Kamran Omar Mar 07 '18 at 02:40
  • e.g. 12345678911111234567891111123456789111112345678911111234567891111123456789111112345678911111234567891111123456789111112345678911111234567891111123456789 – Kamran Omar Mar 07 '18 at 02:40
  • 1
    It has to be a valid token obtained from one of the client SDKs. For example, [on Android](https://firebase.google.com/docs/cloud-messaging/android/client#retrieve-the-current-registration-token), the method is `FirebaseInstanceId.getInstance().getToken()`. The docs for getting the token on iOS [are here](https://firebase.google.com/docs/cloud-messaging/ios/client#receive-the-current-registration-token). – Bob Snyder Mar 07 '18 at 02:45
  • it is working for the following token... fESEZ9zwM-Y:APB91bGfLrfYavY_j-YM6hOEqs_-KEIuozw-BdjIfxeeGdFit1CQEBDftW8tLMrVCubb6XZ-hLjPx3YPF6KGuR0ve3Pm_fMkDiqQdeLN0QA2HXPPYviCTI93RAhWVBH8gdJo9joM9cvL – Kamran Omar Mar 07 '18 at 03:45
4

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

Cory Robinson
  • 4,616
  • 4
  • 36
  • 53
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

J M Rossy
  • 916
  • 7
  • 8
0

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

J. Doe
  • 12,159
  • 9
  • 60
  • 114
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.

joel.d
  • 1,611
  • 16
  • 21
0

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

For example wrong: 03:00' in topics