invertase / notifee

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

[ANDROID] - Notifications do not trigger onForegroundEvent #972

Closed LcsGrz closed 7 months ago

LcsGrz commented 10 months ago

Notifications do not trigger onForegroundEvent (Android)

I did several tests, I do not understand how to solve it. In my project I am using iterable, firebase messaging and notifee.

the question is that when a new notification arrives from iterable I receive it from firebase and it is shown automatically. In android when I press that notification with the app open, nothing happens, no notifee event is executed where I can give an action in the code, the opposite happens in IOs, if the onForegroundEvent function of notifee is executed.

How can I implement an onPress in android, or how can I solve the problem?

I don't know if this issue should go here or in another of the packages I mentioned. I am going to show the versions of the libraries and how I implemented it.

    "@iterable/react-native-sdk": "1.3.17",
    "@notifee/react-native": "7.8.2",
    "@react-native-firebase/analytics": "18.7.3",
    "@react-native-firebase/app": "18.7.3",
    "@react-native-firebase/crashlytics": "18.7.3",
    "@react-native-firebase/dynamic-links": "18.7.3",
    "@react-native-firebase/messaging": "18.7.3",
    "@react-native-firebase/perf": "18.7.3",
    "@react-native-firebase/remote-config": "18.7.3"
// Firebase notification events setup
  FirebaseMessaging().setBackgroundMessageHandler(handleRemoteNotification);
  FirebaseMessaging().onMessage(handleRemoteNotification);
  FirebaseMessaging().onTokenRefresh(updatePushNotificationToken);
  FirebaseMessaging().setAutoInitEnabled(true);
// Push android notifications setup
  await notifee.createChannel({
    ...channel,
    vibration: true,
    importance: AndroidImportance.HIGH,
    sound: 'default',
  });

 // Handle onNotificationPress while the app is running
  notifee.onForegroundEvent(({ type, detail }) => {
    console.log('\n\n', '# foreground', type, '\n\n');
    if (!detail.notification) return;

    switch (type) {
      case EventType.PRESS:
      case EventType.ACTION_PRESS:
        handlePushNotificationPressed(detail.notification);
    }
  });

  // Handle onNotificationPress while the app is closed
  notifee.onBackgroundEvent(async ({ type, detail }) => {
    console.log('\n\n', '# background', type, '\n\n');

    if (!detail.notification) return;

    switch (type) {
      case EventType.PRESS:
      case EventType.ACTION_PRESS: {
        handlePushNotificationPressed(detail.notification);

        // Remove the notification
        if (detail.notification?.id) {
          await notifee.cancelNotification(detail.notification.id);
        }
      }
    }
  });
   // Iterable setup
   const config = new IterableConfig();
  config.autoPushRegistration = true;

  const initialized = await Iterable?.initialize(
        env.iterableApiKey,
        config,
      );

    Iterable?.setEmail(user);

the code looks too simple, it is worth noting that when I receive an event from firebase and the notification is iterable I do not show it a second time with notifee


EDIT 1:

to explain a little bit about the execution flow

I was testing and the difference I can find between this notification and the one I launch from notifee is the channelID, which is the firebase default, although I don't think it has any relevance fcm_fallback_notification_channel

wamry commented 10 months ago

I have the same issue

LcsGrz commented 10 months ago

Any update on this? :(

susonthapa commented 9 months ago

This might help. On Android, remote notifications are handled a bit differently. If Iterable or FirebaseService is displaying the notification then they will receive the onPress events. You will have to either hook into Iterable or FirebaseService to know which action was pressed. I know Iterable has a couple of handlers that will be called when you send the notifications through Iterable on Android.

github-actions[bot] commented 8 months 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.

github-actions[bot] commented 7 months 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.