Maria Syed
Maria Syed

Reputation: 11

How do I remove react native firebase notification click action on android?

I am using react-native-firebase to set up foreground & background push notifications in my application. I set it up according to the docs: https://rnfirebase.io/docs/v4.3.x/notifications/receiving-notifications#App-in-Foreground-and-background

The app has a firebase.messaging().onMessage() listener attached and when it receives a notification in foreground, I use firebase.notifications.displayNotification(notification) to display the notification.

Then the onNotificationOpened listener receives the notification and navigates the user to a different screen.

The problem is that, the notification object received from onNotificationOpened has an action android.intent.action.VIEW which launches the MainActivity every time it is clicked. I have no idea where this action string is coming from, since neither the client app nor the backend sets any click action. Also this android.intent.action.VIEW happens to be the intent filter action for my MainActivity.

I don't want a ContentIntent action tied to my notification, I want my app to handle the navigation to the correct component.

Also there is the problem that clicking on the notification unmounts and re-mounts my main component and that causes the notification listeners to be removed.

I have tried setting MainActivity android:launchMode=singleTop in AndroidManifest but the activity still relaunches. I have also tried using singleTask, singleInstance and standard

This is my display notification function that is called onMessage

export async function displayNotification(message: RemoteMessage) {
  try {
    const notification = new firebase.notifications.Notification();

    const { title, message: messageBody, ...messageData } = message.data;

    notification
      .setNotificationId(message.messageId)
      .setTitle(title)
      .setBody(messageBody)
      .setData({
        ...messageData
      })
      .setSound('default');

    if (isAndroid) {
      // Build a channel for notifications on Android

      const channel = await createAndroidChannel();

      notification.android
        .setBigText(messageBody)
        .android.setChannelId(channel.channelId)
        .android.setAutoCancel(true) // Remove on press
    }

    await notifications.displayNotification(notification);
    return notification;
  } catch (error) {

  }
}

Expected result is: user has app in foreground > user receives push notification > clicks on notification > does NOT relaunch Main Activity but redirects to particular screen if needed via onNotificationOpened listener

Actual result is: after user clicks on foreground notification > Main Activity is relaunched and Main screen re-mounts causing notification listeners to be removed.

Upvotes: 1

Views: 5380

Answers (1)

Aarsh Oza
Aarsh Oza

Reputation: 192

The problem here is there is already an instance of your app running either when your app is in foreground or ink background. So when you press on a notification, it again creates another instance of the mainActivity and relaunches it.To solve this problem you have to understand about the launch modes of android

Please refer to this link for description

Try setting launchMode as singleTop in AndroidManifest.xml file. Then reinstall and test the application. And this should solve your issue.!

Upvotes: 1

Related Questions