question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. Itย collects links to all the places you might be looking at while hunting down a tough bug.

And, if youโ€™re still stuck at the end, weโ€™re happy to hop on a call to see how we can help out.

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

See original GitHub issue

๐Ÿ› 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:

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

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:8

github_iconTop GitHub Comments

1reaction
devpersoncommented, Nov 17, 2022

Itโ€™s better to move to Xamarin OneSignal

0reactions
thomasgallikercommented, Nov 18, 2022

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

OnNotificationOpened not fired on Android when ...
OnNotificationOpened not fired on Android when notification is tapped while app is closed. (Xamarin.Forms) #430.
Read more >
c# - Xamarin.Forms Firebasepushnotification plugin ...
I'm trying to handle a notifications opening on android that I've sent through Google's FCM on the SharedProject level by using "Plugin.
Read more >
Implement push notification tapping on android platform
I have implemented push notification on my Xamarin forms application using FCM. Here I am using notifications for the messages. So when I...
Read more >
Push notifications made easy in Xamarin
On iOS by default you need to tap the notification to receive the data. OnNotificationOpened โ€“ When a push notification is opened you...
Read more >
Getting Started with Push Notifications
When a push is received in the foreground on iOS, how the notification is displayed to the user is controlled by foreground presentation...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found