react-native-sdk icon indicating copy to clipboard operation
react-native-sdk copied to clipboard

[ANDROID] - Notifications do not trigger onForegroundEvent

Open LcsGrz opened this issue 2 years ago • 3 comments

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

LcsGrz avatar Jan 24 '24 14:01 LcsGrz

Hi @LcsGrz, thanks for reaching out. Can you test extending your Firebase service to the Iterable service so it can forward onMessageReceived and onNewToken calls to IterableFirebaseMessagingService.handleMessageReceived and IterableFirebaseMessagingService.handleTokenRefresh, respectively? Reference: https://support.iterable.com/hc/en-us/articles/360035019712-Iterable-s-Android-SDK#handling-firebase-push-messages-and-tokens

jena-chakour avatar Jan 26 '24 00:01 jena-chakour

Hi @LcsGrz, thanks for reaching out. Can you test extending your Firebase service to the Iterable service so it can forward onMessageReceived and onNewToken calls to IterableFirebaseMessagingService.handleMessageReceived and IterableFirebaseMessagingService.handleTokenRefresh, respectively? Reference: https://support.iterable.com/hc/en-us/articles/360035019712-Iterable-s-Android-SDK#handling-firebase-push-messages-and-tokens

Hii, thank you very much for your answer, I was on vacation so I just saw your message :), tomorrow morning I will try what you tell me and I will edit this message

hugs!

LcsGrz avatar Jan 30 '24 00:01 LcsGrz

@jena-chakour Hi :D , I hope you are doing well, I was testing but I am not so sure how I can debug this in android, what I did was to create the java file in the android folder and in the manifest I added the following line

<service android:name=".MyFirebaseMessagingService" />

package com.p;

import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.iterable.iterableapi.IterableFirebaseMessagingService;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d("app", "onMessageReceived");
        IterableFirebaseMessagingService.handleMessageReceived(this, remoteMessage);
    }

    @Override
    public void onNewToken(String s) {
         Log.d( "app","onNewToken");
        IterableFirebaseMessagingService.handleTokenRefresh();
    }
}

and then run npx react-native log-android but nothing happens :(

LcsGrz avatar Jan 30 '24 15:01 LcsGrz