CrossGeeks/FirebasePushNotificationPlugin

Events called twice if app was in background (Android)

AlleSchonWeg opened this issue ยท 2 comments

๐Ÿ› Bug Report

The Events OnNotificationReceived and OnNotificationOpened are called twice, if app was in foreground and then goto background.
Testet with Android API 28.

Expected behavior

Event should only excecuted once.

Reproduction steps

  • Publish App on device/emulator
  • Open App and send notification.
  • Bring App to background
  • Send notification again and open notification
  • OnNotificationOpened is called twice (Monitor it with console.write for example)
  • Now App is in foreground. Send notifictaion again.
  • OnNotificationReceived is now called twice

Configuration

Android Emulator with API 28
Version: 3.4.25

Platform:

  • ๐Ÿ“ฑ iOS
  • ๐Ÿค– Android
  • ๐Ÿ WPF
  • ๐ŸŒŽ UWP
  • ๐ŸŽ MacOS
  • ๐Ÿ“บ tvOS
  • ๐Ÿ’ Xamarin.Forms

Some additional info.
The problem is, that the events OnNotificationReceived, OnNotificationOpened are stored in a static variable. If the app is in background and a notifcation is opend a new Android Activity is created. OnCreate is called again and also a new XF-App is created. Which results in a multiple registered events. A workaround is to make the events static and unregister, bevor register:

protected override void OnStart()
{
            CrossFirebasePushNotification.Current.OnNotificationReceived -= Current_OnNotificationReceived;
            CrossFirebasePushNotification.Current.OnNotificationReceived += Current_OnNotificationReceived;
            CrossFirebasePushNotification.Current.OnNotificationOpened -= Current_OnNotificationOpened;
            CrossFirebasePushNotification.Current.OnNotificationOpened += Current_OnNotificationOpened;
}
private static void Current_OnNotificationOpened(object source, FirebasePushNotificationResponseEventArgs e)
{
            Console.WriteLine("OnNotificationOpened Xamarin Forms");
}

private static void Current_OnNotificationReceived(object source, FirebasePushNotificationDataEventArgs e)
{
}

Adding
LaunchMode = LaunchMode.SingleTop
to activity prevents creating a new activity.