N
Novu2y ago
turtles

ITriggerOverrideFCM Overrides don't match Docs

Having a few issues getting FCM notifications to not trigger twice in the onBackgroundMessage listener of our app using FCM and webPush. Part of the problem that makes this more confusing is that the Docs on https://docs.novu.co/channels/push/fcm/ don't match up with the types provided.
export type ITriggerOverrideFCM = {
type?: 'notification' | 'data';
tag?: string;
body?: string;
icon?: string;
badge?: string;
color?: string;
sound?: string;
title?: string;
bodyLocKey?: string;
bodyLocArgs?: string;
clickAction?: string;
titleLocKey?: string;
titleLocArgs?: string;
data?: Record<string, any>;
};
export type ITriggerOverrideFCM = {
type?: 'notification' | 'data';
tag?: string;
body?: string;
icon?: string;
badge?: string;
color?: string;
sound?: string;
title?: string;
bodyLocKey?: string;
bodyLocArgs?: string;
clickAction?: string;
titleLocKey?: string;
titleLocArgs?: string;
data?: Record<string, any>;
};
If I ignore the types provided and follow the docs I have a notification I want to send that looks like
curl --location --request POST 'https://api.novu.co/v1/events/trigger' \
--header 'Authorization: ApiKey <REPLACE_WITH_API_KEY>' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "JOB_COMPLETE",
"to": {
"subscriberId": "######-####-#####-####-###########",
"email": "[email protected]",
"firstName": "tester",
"phone": "#########"
},
"payload": {
"jobId": "1111",
"jobUuid": "######-####-#####-####-###########"
},
"overrides": {
"fcm": {
"type": "data",
"webPush": {
"fcmOptions": {
"link": "https://mywebsite.com"
}
}
}
}
}'
curl --location --request POST 'https://api.novu.co/v1/events/trigger' \
--header 'Authorization: ApiKey <REPLACE_WITH_API_KEY>' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "JOB_COMPLETE",
"to": {
"subscriberId": "######-####-#####-####-###########",
"email": "[email protected]",
"firstName": "tester",
"phone": "#########"
},
"payload": {
"jobId": "1111",
"jobUuid": "######-####-#####-####-###########"
},
"overrides": {
"fcm": {
"type": "data",
"webPush": {
"fcmOptions": {
"link": "https://mywebsite.com"
}
}
}
}
}'
I get this error in the activity feed
{
"code": "messaging/invalid-payload",
"message": "data must only contain string values"
}
{
"code": "messaging/invalid-payload",
"message": "data must only contain string values"
}
If I remove the "type" data I get this error
{}
{}
There seems to be override and parsing errors
Firebase Cloud Messaging | Novu
Firebase Cloud Messaging is a free notification delivery service provided by Google Firebase.
4 Replies
empe
empe2y ago
It appears that the issue might be related to the type field in your overrides object and the non-string values in the data object. According to a discussion here: https://discord.com/channels/895029566685462578/1072461424061841450, when type is set to data, the message payload is sent, but the title and message fields are omitted from the template. It is recommended not to change the type field and instead add the data key in the overrides object. Additionally, all fields in the data object should be strings. To fix the issue, remove the type field from your overrides, and ensure all fields in your payload object have string values. If the problem persists, let us know
Discord
Discord - A New Way to Chat with Friends & Communities
Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.
turtles
turtlesOP2y ago
Thanks there's some good stuff in there. I think the double notification trigger comes from us wanting a data message so we can handle it manually in the background. It seems like you can only send notification messages with Novu at the moment? From the firebase docs: https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
Use notification messages when you want the FCM SDK to handle displaying a notification automatically when your app is running in the background. Use data messages when you want to process the messages with your own client app code.
Use notification messages when you want the FCM SDK to handle displaying a notification automatically when your app is running in the background. Use data messages when you want to process the messages with your own client app code.
I thought the data override would send it as a data message so we can handle it in the background with a listener in the firebase-messaging-sw.js like this
messaging.onBackgroundMessage(function (payload) {
console.log("in background", payload);
const notificationTitle = payload.notification.title;
const notificationOptions = {
body: payload.notification.body,
icon: "/images/logo.png",
data: {
link: payload.data?.link,
},
};

self.registration.showNotification(notificationTitle, notificationOptions); <-- triggers a second notification
messaging.onBackgroundMessage(function (payload) {
console.log("in background", payload);
const notificationTitle = payload.notification.title;
const notificationOptions = {
body: payload.notification.body,
icon: "/images/logo.png",
data: {
link: payload.data?.link,
},
};

self.registration.showNotification(notificationTitle, notificationOptions); <-- triggers a second notification
however if I send a notification with an override object like this
"overrides": {
"fcm": {
"data": {
"label": "test label",
"flag": "Flag",
"featureType": "a feature",
"linkAddress": "https://awebsite.com/link"
},
"webPush": {
"fcmOptions": {
"link": "https://awebsite.com/link"
},
"notification": {
"icon": "https://awebsite.com/images/PushIcon.png"
}
}
}
"overrides": {
"fcm": {
"data": {
"label": "test label",
"flag": "Flag",
"featureType": "a feature",
"linkAddress": "https://awebsite.com/link"
},
"webPush": {
"fcmOptions": {
"link": "https://awebsite.com/link"
},
"notification": {
"icon": "https://awebsite.com/images/PushIcon.png"
}
}
}
turtles
turtlesOP2y ago
We can see this payload in the messaging.onBackgroundMessage listener
{
"from": "1030072471020",
"messageId": "d1b3cd93-f03e-4e28-bc8b-3cdc3d990c42",
"notification": {
"title": "Infrastructure Flagged",
"body": " has been flagged as .",
"icon": "https://awebsite.com/images/PushIcon.png"
},
"data": {
"flag": "Requiring Maintenance",
"featureType": "Pump",
"linkAddress": "https://awebsite.com/link",
"label": "test label"
},
"fcmOptions": {
"link": "https://awebsite.com/link"
}
}
{
"from": "1030072471020",
"messageId": "d1b3cd93-f03e-4e28-bc8b-3cdc3d990c42",
"notification": {
"title": "Infrastructure Flagged",
"body": " has been flagged as .",
"icon": "https://awebsite.com/images/PushIcon.png"
},
"data": {
"flag": "Requiring Maintenance",
"featureType": "Pump",
"linkAddress": "https://awebsite.com/link",
"label": "test label"
},
"fcmOptions": {
"link": "https://awebsite.com/link"
}
}
The variables in notification part of the object above gets sent as normal push notification to our devices (which we don't want if we add the data object). If we had put those variables in the now omitted "payload" object then the template we specified on the Novu workflow would have filled them in for us. It seems like theres no way to send data messages via novu at the moment, or am I missing something key?
Pawan Jain
Pawan Jain2y ago
hi @turtles Can you please create a bug on github for this? @turtles ping
Want results from more Discord servers?
Add your server