node-pushnotifications
node-pushnotifications copied to clipboard
Sending Custom Data in FCM not working
Sending custom data with FCM is not working. When trying to log the notification received in Expo React Native, no custom data is received. Here's the code:
const settings = {
fcm: {
appName: 'xxxxx',
serviceAccountKey: jsonKey,
credential: null,
}
}
const push = new PushNotifications(settings);
const deviceToken = 'xxxxxxxxxx';
const data = {
title: 'XXXX',
body: 'Message',
custom: {
// Additional data to send with the notification for android
name: 'test'
},
};
push.send(deviceToken, data, (error: Error, result: any) => {
});
The notification is received but the custom param "name" is not there.
Same issue here...
seems to be a problem with expo, doesn't it ?
https://github.com/expo/expo/issues/28656
Hey guys. Let us know if you can confirm that this is an issue with expo. Otherwise we'd be happy to accept PRs for fixing whatever issue this is for your use-case.
i experienced the same problem. i am not using Expo just nativescript for my app.
The problem seems to be due to the migration of the new http api of FCM. my problem was that after migration it seems the data-only notification was not send anymore. Also the android_channel_id property is now just channel_id (although i am currently not using it anymore thus you will not find it in the code below)
anyhow, this is working for me now:
const data = {
collapseKey: Math.random().toString().replace('0.', ''),
title: 'test',
topic: 'all',
body: 'text',
priority: 'high',
custom: {
id:'123',
},
fcm_notification: {
},
icon: 'notification_icon',
sound: 'test.wav',
category: 'alarm',
truncateAtWordEnd: true,
pushType: 'alert',
};
const pushResult = await push.send(result.user.fcmToken, data);
strangely i need to put the empty fcm_notification object into it, because otherwise https://github.com/appfeel/node-pushnotifications/blob/master/src/utils/tools.js#L77 will create these properties for a notification message it seems and then my custom messaging service is not invoked on the android client side. Really strange, but this was doing the trick for me. hopefully this also helps others
@eikaramba Thank you for the insight.
Can you let me know what exactly the params.data of the GcmMessage should look like in order for your custom data to work as expected? When I am extending the sendGcm test locally with {custom: { id: 123}, then the GcmMessage.params.data contains {id: 123}, regardless of if I pass an empty fcm_notification or none at all?
@alex-friedl i don't use gcm, i use fcm. you should switch too i guess. i don't have any more code than what i shared above.
I don't use anything myself anymore, I just try to maintain this is good as I can without having a working notification setup for real life testing 😬 I'll look into the issue you described some more in the coming days.
sry i thought you are using it as a library, my bad. Unfortunately i don't have access to prod environment anymore where i did use that code. but yeah i think the biggest problem was that https://github.com/appfeel/node-pushnotifications/blob/master/src/utils/tools.js#L77 is overriding the config if fcm_notification is not passed. and altough it says "buildGcmNotification" it is also used for fcm notifications.
The only more code that i have is:
const PushNotifications = require("node-pushnotifications");
const settings = {
fcm: {
appName: "xxxx",
serviceAccountKey: firebaseConfig,
credential: null,
},
};
const push = new PushNotifications(settings);
rest is as above. So maybe it is specific to fcm, which should be used anyway as gcm is deprecated.
I was searching for a way how to set up the channeld. I hacked it by setting up:
fcm_notification: {
channelId: 'xxx',
},
This is used in buildGcmNotification method and will result in a structure I need.. But I guess there should be a proper way how to set such a basic setting.