Skip to content

[ANDROID] - Notifications do not trigger onForegroundEvent #530

Open
@LcsGrz

Description

@LcsGrz

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 send a notification from iterable with the app open but in the background.
  • Firebase catches it and executes onMessage or the callback of setBackgroundMessageHandler
  • As it is a notification from iterable, I don't execute any action with Notifee, otherwise it would be duplicated.
  • Then if I press that notification, it opens the application but nothing happens, no Notifee event is executed.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions