MAUI: Notification Tapping in android platform

Sreejith Sreenivasan 896 Reputation points
2023-11-06T14:39:12.74+00:00

I am using Plugin.Firebase and Plugin.Firebase.Crashlytics packages for implementing push notification in my MAUI application. I referred this blog and it was working fine on the Android platform.

Now I am working on the push notification tapping on Android platform and I am referring this blog by the same person. On this blog they are suggested to register the NotificationTapped event on MauiProgram.cs. Below is the code for that:

CrossFirebaseCloudMessaging.Current.NotificationTapped += (sender, e) => 
{
    System.Diagnostics.Debug.WriteLine("Tapped");
};

I have added it on MauiProgram.cs and pushed a test notification. But when I tap on the notification this event is not firing.

Below is my complete MauiProgram.cs codes:

  public static class MauiProgram
    {
        public static MauiApp CreateMauiApp()
        {
            CrossFirebaseCloudMessaging.Current.NotificationTapped += (sender, e) =>
            {
                System.Diagnostics.Debug.WriteLine("Tapped");
            };

            var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiApp<App>()
                .UseMauiRGPopup()
                .RegisterFirebaseServices()
                .UseMauiCommunityToolkit()
                .UseUserDialogs(true, () =>
                {
#if ANDROID
                    var fontFamily = "OpenSans-Regular.ttf";
#else
                    var fontFamily = "OpenSans-Regular";
#endif
                    AlertConfig.DefaultMessageFontFamily = fontFamily;
                    AlertConfig.DefaultUserInterfaceStyle = UserInterfaceStyle.Dark;
                    AlertConfig.DefaultPositiveButtonTextColor = Colors.Purple;
                    ConfirmConfig.DefaultMessageFontFamily = fontFamily;
                    ActionSheetConfig.DefaultMessageFontFamily = fontFamily;
                    ToastConfig.DefaultMessageFontFamily = fontFamily;
                    SnackbarConfig.DefaultMessageFontFamily = fontFamily;
                    HudDialogConfig.DefaultMessageFontFamily = fontFamily;
                })
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                })
                .UseFFImageLoading()
                .UseMauiCompatibility()
                .ConfigureMauiHandlers(handlers =>
                {
#if __ANDROID__
                    //Handler section
#elif IOS
                    //Handler section
#endif
                });
            return builder.Build();
        }

        private static MauiAppBuilder RegisterFirebaseServices(this MauiAppBuilder builder)
        {
            builder.ConfigureLifecycleEvents(events => {
#if IOS
                events.AddiOS(iOS => iOS.FinishedLaunching((app, launchOptions) => {
                    CrossFirebase.Initialize(CreateCrossFirebaseSettings());
                    CrossFirebaseCrashlytics.Current.SetCrashlyticsCollectionEnabled(true);
                    return false;
                }));
#else
            events.AddAndroid(android => android.OnCreate((activity, _) =>
                CrossFirebase.Initialize(activity, CreateCrossFirebaseSettings())));
                CrossFirebaseCrashlytics.Current.SetCrashlyticsCollectionEnabled(true);
#endif
            });

            builder.Services.AddSingleton(_ => CrossFirebaseAuth.Current);
            return builder;
        }

        private static CrossFirebaseSettings CreateCrossFirebaseSettings()
        {
            return new CrossFirebaseSettings(
                isAuthEnabled: true,
                isCloudMessagingEnabled: true,
                isAnalyticsEnabled: true);
        }
    }

I have added it on the CreateMauiApp() function like above. Is that the exact place for adding it? I didn't find anything on the MauiProgram.cs when I check the demo project from that blog.

Is this tapping is enough for all the 3 modes? I mean background mode, foreground mode and killed mode?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,730 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sreejith Sreenivasan 896 Reputation points
    2023-11-13T07:54:16.7+00:00

    @Yonglun Liu (Shanghai Wicresoft Co,.Ltd.)

    I have fixed this issue by adding the OnNewIntent with HandleIntent(intent) function like below on MainActivity class.

    protected override void OnNewIntent(Intent intent)
    {
        base.OnNewIntent(intent);
        HandleIntent(intent);
    }
    

    https://github.com/TobiasBuchholz/Plugin.Firebase/issues/226

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.