FirebasePushNotificationPlugin icon indicating copy to clipboard operation
FirebasePushNotificationPlugin copied to clipboard

OnNotificationOpened called every time App is launched on Android, without tapping notifications, Xamarin Forms

Open astralmaster opened this issue 5 years ago • 8 comments

🐛 Bug Report

Configuration

In release build, whenever I launch my App by tapping its icon, the CrossFirebasePushNotification.Current.OnNotificationOpened is fired even though no notification has been tapped on or received. This does not happen while debugging the app. However, If I stop debugging, and just tap the debug build version of the App and it opens, still the same event is fired. Only when connected to Visual Studio and started in debug mode from there is the event not fired.

Plugin.FirebasePushNotification version 3.3.13-beta (tried version 3.3.10 but still the same) Xamarin Forms v4.6.0.616-pre4

MainApplication.cs

 [Application]
    public class MainApplication : Application
    {
        protected MainApplication(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
        {
        }

        public override void OnCreate()
        {
            base.OnCreate();
            
          
              //Set the default notification channel for your app when running Android Oreo
	        if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
	        {
		        //Change for your default notification channel id here
		        FirebasePushNotificationManager.DefaultNotificationChannelId = "DefaultChannel";

		        //Change for your default notification channel name here
		        FirebasePushNotificationManager.DefaultNotificationChannelName = "General";
	        }

            FirebasePushNotificationManager.NotificationActivityType = typeof(MainActivity);
            
            //If debug you should reset the token each time.
#if DEBUG
            FirebasePushNotificationManager.Initialize(this,true,true,false);
#else
              PushNotificationManager.Initialize(this,false,true,false);
#endif


            CrossFirebasePushNotification.Current.OnNotificationOpened += async (s,p) =>
            {
           
                Toast.MakeText(Application.Context, "Why on earth is this toast appearing when i just launched the app normally", ToastLength.Short).Show();
        
          
            };

            
             

        }


    }

MainActivity.cs

     protected override void OnNewIntent(Intent intent)
        {
            base.OnNewIntent(intent);
            FirebasePushNotificationManager.ProcessIntent(this,intent);
        }

 protected override void OnCreate(Bundle savedInstanceState)
        {
            base.Window.RequestFeature(WindowFeatures.ActionBar);
            base.SetTheme(Resource.Style.AppTheme);

            FormsAppCompatActivity.ToolbarResource = Resource.Layout.Toolbar;
            FormsAppCompatActivity.TabLayoutResource = Resource.Layout.Tabbar;

            base.OnCreate(savedInstanceState);

          
            // Inicializando FFImageLoading
            FFImageLoading.Forms.Platform.CachedImageRenderer.Init(enableFastRenderer: true);
            var config = new Configuration
            {
                ExecuteCallbacksOnUIThread = false,
                HttpClient = new HttpClient(new AndroidClientHandler() 
                {
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate, 
                }),
            };
            FFImageLoading.ImageService.Instance.Initialize(config);

    
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

            // Inicializando Xamarin.Essentials
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);

            LoadApplication(new App());

            FirebasePushNotificationManager.ProcessIntent(this,Intent);

        }

What am I missing?

Platform:

  • [ ] :iphone: iOS
  • [X ] :robot: Android
  • [ ] :checkered_flag: WPF
  • [ ] :earth_americas: UWP
  • [ ] :apple: MacOS
  • [ ] :tv: tvOS
  • [ ] :monkey: Xamarin.Forms

astralmaster avatar Jul 02 '20 17:07 astralmaster

Update: The same event is also fired every time the application comes to foreground from background (being 'minimized')

astralmaster avatar Jul 02 '20 19:07 astralmaster

Same problem even on 3.1.6, basically whenever I launch my app the OnNotificationOpened is automatically triggered

For now I fixed simply by filtering p.Data, if it contains what I'm expecting from a push I do stuff, otherwise return

Let's hope for an official fix so I can drop my quick&dirty one :)

Shark107 avatar Jul 04 '20 08:07 Shark107

Yes, that's what I am doing as well. I found another issue with the same exact symptoms and over there they resolved it by doing the same exact thing. However a real fix was never posted.

astralmaster avatar Jul 05 '20 10:07 astralmaster

This should be the top most priority as with this bug not able to use the library. I can't filter out notification as it is dynamic. So whenever user opens app, it goes into notification opened event. Lol

SagarPanwala avatar Aug 27 '20 18:08 SagarPanwala

Any update for this issue?

devperson avatar Jun 29 '22 21:06 devperson

I'm experience this situation too. In my case I received and opened a push notification, then I closed the app (navigate back on an older Android), switched the system language from German to English (or anything else) and started the app again. FirebasePushNotificationManager.ProcessIntent in OnCreate fired the OnNotificationOpened event which opens a popup page in my case... I guess many other people also experience such situations.

thomasgalliker avatar Nov 16 '22 10:11 thomasgalliker

It's better to move to Xamarin OneSignal

devperson avatar Nov 17 '22 10:11 devperson

Guys, I might have found a solution for this problem. It seems as Android recycles the Intent which launched an Activity (and with the Intent it recycles the Extras in it). I tested several scenarios and it looks like the following code can be used in OnCreate an OnNewIntent (instead of just calling FirebasePushNotificationManager.ProcessIntent):

private static void CheckAndProcessIntent(Activity activity, Intent intent)
{
    var launchedFromHistory = intent.Flags.HasFlag(ActivityFlags.LaunchedFromHistory);
    if (!launchedFromHistory)
    {
        FirebasePushNotificationManager.ProcessIntent(activity, intent, enableDelayedResponse: false);
    }
}

Is this something, they should integrate into this plugin? Or anyone still doing maintenance here?

thomasgalliker avatar Nov 18 '22 06:11 thomasgalliker