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:
send one by one. Result: the same error on every messageapns-priority to 5. The same errorcontent-available, content_available. The same errorapns 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With