node-pushnotifications icon indicating copy to clipboard operation
node-pushnotifications copied to clipboard

Sending Custom Data in FCM not working

Open imarc7 opened this issue 1 year ago • 8 comments

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.

imarc7 avatar Jul 29 '24 17:07 imarc7

Same issue here...

hajjarjoseph avatar Jul 29 '24 17:07 hajjarjoseph

seems to be a problem with expo, doesn't it ?

https://github.com/expo/expo/issues/28656

arnaudambro avatar Aug 08 '24 08:08 arnaudambro

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.

alex-friedl avatar Aug 10 '24 15:08 alex-friedl

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 avatar Nov 01 '24 17:11 eikaramba

@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 avatar Feb 02 '25 10:02 alex-friedl

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

eikaramba avatar Feb 02 '25 11:02 eikaramba

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.

alex-friedl avatar Feb 02 '25 11:02 alex-friedl

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.

eikaramba avatar Feb 02 '25 11:02 eikaramba

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.

nesro avatar Jun 30 '25 12:06 nesro