invertase / notifee

⚛️ A feature rich notifications library for React Native.
https://notifee.app
Apache License 2.0
1.88k stars 227 forks source link

Issue with notification.data being empty in Notifee #1103

Open dieptang opened 2 months ago

dieptang commented 2 months ago

I am encountering an issue with the notification.data object being empty when receiving notifications in my React Native application. Here are the details:

Notifee Version: 9.0.0
React Native Version: 0.73.9

Issue Description: When I send a notification with both notification and data payloads from my PHP server, the data field in the notification is empty when handling the notification click event in Notifee.

Example FCM Payload Sent from Server:

{
  "notification": {
    "title": "New Booking",
    "body": "You have a new booking request"
  },
  "data": {
    "bookingId": "12345",
    "otherInfo": "Some info"
  }
}

Expected Behavior: I expect the notification.data object to contain the keys bookingId and otherInfo when clicking on the notification.

Current Behavior: The notification.data object is empty when the notification is clicked.

useEffect(() => {
        return notifee.onForegroundEvent(({ type, detail }) => {
            switch (type) {
                case EventType.DISMISSED:
                    console.log('User dismissed notification', detail.notification);
                    break;
                case EventType.PRESS:
                    console.log('User pressed notification', detail.notification);
                    break;

                case EventType.DISPLAY:
                    // Handle notification display event (if applicable)
                    console.log('Notification Displayed:', detail.notification);
                    break;
                case EventType.BACKGROUND:
                    // Handle background event if needed
                    console.log('Notification Background Event:', detail.notification);
                    break;
                default:
                    console.log('Other Event:', type);
            }
        });
    }, []);

I would appreciate your assistance in resolving this issue.

Thank you!

KhoaNguy3n commented 1 month ago

I am also experiencing the same issue!

Notifee Version: 9.1.1 React Native Version: 0.73.7

github-actions[bot] commented 2 weeks ago

Hello 👋, to help manage issues we automatically close stale issues.

This issue has been automatically marked as stale because it has not had activity for quite some time.Has this issue been fixed, or does it still require attention?

This issue will be closed in 15 days if no further activity occurs.

Thank you for your contributions.

rranjan14 commented 1 week ago

Experiencing the same issue in iOS as well.

Notifee Version: ^9.1.2 React Native Version: ~0.75.4 @react-native-firebase/messaging: ^21.4.0

LinhNguyen1059 commented 1 week ago

I have the same issue, and after spending 2 hours figuring it out, you need to set the data when you display a notification so that when you press the notification, you can get back the data you've passed.

messaging().onMessage(remoteMessage => {
  onMessageReceived(remoteMessage);
});

const onMessageReceived = async remoteMessage => {
  const {notification, data} = remoteMessage;

  await notifee.displayNotification({
    title: notification.title,
    body: notification.body,
    android: {
      channelId: CHANNEL_ID,
      importance: AndroidImportance.HIGH,
      smallIcon: 'noti_icon',
      pressAction: {
        id: 'default',
        launchActivity: 'default',
      },
      lightUpScreen: true,
    },
    data, // need to set the data here
  });
};
fazzaamiarso commented 5 days ago

I have the same issue, and after spending 2 hours figuring it out, you need to set the data when you display a notification so that when you press the notification, you can get back the data you've passed.

messaging().onMessage(remoteMessage => {
  onMessageReceived(remoteMessage);
});

const onMessageReceived = async remoteMessage => {
  const {notification, data} = remoteMessage;

  await notifee.displayNotification({
    title: notification.title,
    body: notification.body,
    android: {
      channelId: CHANNEL_ID,
      importance: AndroidImportance.HIGH,
      smallIcon: 'noti_icon',
      pressAction: {
        id: 'default',
        launchActivity: 'default',
      },
      lightUpScreen: true,
    },
    data, // need to set the data here
  });
};

I have the same issue of data not being shown on any handlers when pressed. I'll try this solution and get back

UPDATE: This should be in the documentation. Added the data props and it worked on Foreground and Kill State. However, still having issue when app is running on background.

Notifee Version: 9.1.2
React Native Version: 0.76.2