NickTaylor98
NickTaylor98

Reputation: 225

Firebase. Error "Request contains an invalid argument."

Use on backend side following libraries (from package.json).

"firebase": "^8.3.3",
"firebase-admin": "^9.6.0",

Try to send multicast message to multiple users.

import * as admin from 'firebase-admin';

const createNotificationMessage = (tokens: string[], data?: { [key: string]: string }): admin.messaging.MulticastMessage => {
  return {
    data,
    tokens,
    apns: {
      payload: {
        aps: {
          contentAvailable: true,
        },
      },
    },
  };
};

const sendMulticast = (payload: admin.messaging.MulticastMessage) =>
  admin.messaging().sendMulticast(payload);

const sendNotifications = async () => {
try {
      const data = getData(); // here we get main data
      const userTokens = getTokens(); // here we get tokens
      await sendMulticast(createNotificationMessage(userTokens, data));
    } catch (error) {
      console.log(error);
    }
   }

I put 4 tokens to message before sending. But I got this error message in response

[{"success":false,"error":{"code":"messaging/invalid-argument","message":"Request contains an invalid argument."}},{"success":false,"error":{"code":"messaging/invalid-argument","message":"Request contains an invalid argument."}},{"success":false,"error":{"code":"messaging/invalid-argument","message":"Request contains an invalid argument."}},{"success":false,"error":{"code":"messaging/invalid-argument","message":"Request contains an invalid argument."}}]

What I tried to do:

  1. Send messages through method send one by one. Result: the same error on every message
  2. Tried to set header apns-priority to 5. The same error
  3. Tried to set custom properties in aps object - content-available, content_available. The same error
  4. Delete apns property from payload. Works well and there is no errors but I need silent notifications in iOS applications that's why option contentAvailable is required.

One note: this code worked well till 9 April 2021.

Upvotes: 3

Views: 4422

Answers (1)

NickTaylor98
NickTaylor98

Reputation: 225

After full day search the reason of this errors, I found a solution for my problem.

const createNotificationMessage = (tokens: string[], data?: { [key: string]: string }): admin.messaging.MulticastMessage => {
  return {
    data,
    tokens,
    apns: {
      payload: {
        aps: {
          contentAvailable: true,
          badge : 0
        },
      },
    },
  };
};

Don't know why firebase shows an error because according to official website, parameter badge is optional string.

Upvotes: 4

Related Questions