When click on notification app does not run MainActivity in Android [Xamarin.Forms]

I have been trying to implement repeating local notifications in my android app and I have achieved this pretty well (I believe) but when I want to click on my notification and execute a logic in it it 1. Does not start the MainActivity (Does not launch me to the app) 2. Does not execute any logic

In my MainActivity.cs file I have the LaunchMode set to SingleTop as well I have the respective override of the method OnNewIntent() that will recieve my notification when I click on it, but it just simply does not execute that method even

I send my notification with a BroadcastReceiver that I call in other class that is NOT the MainActivity.cs (beacuse I have seen many people does that) thanks to my implementation of the Interface that communicates with my shared code project

method that calls the notification:

public void ScheduleNotification(string title, string message, long seconds)
        if (!channelInitialized)

        NotificationReciever.title = title;
        NotificationReciever.message = message;
        AlarmManager alarmManager = (AlarmManager)AndroidApp.Context.GetSystemService(Context.AlarmService);
        Intent intent = new Intent(AndroidApp.Context, typeof(NotificationReciever));
        intent.PutExtra(TitleKey, title);
        intent.PutExtra(MessageKey, message);

        PendingIntent pendingIntent = PendingIntent.GetBroadcast(AndroidApp.Context, 0, intent, PendingIntentFlags.UpdateCurrent);

        alarmManager.SetRepeating(AlarmType.ElapsedRealtimeWakeup, SystemClock.ElapsedRealtime() + seconds * 1000, 2000, pendingIntent);

My broadcast receiver:

[BroadcastReceiver(Enabled =true)]
    public class NotificationReciever : BroadcastReceiver
        const string channelId = "default";
        public static string title = "";
        public static string message = "";
        const string channelName = "Default";
        const string channelDescription = "The default channel for notifications.";
        const int pendingIntentId = 0;

        public const string TitleKey = "title";
        public const string MessageKey = "message";

        int messageId = -1;
        NotificationManager manager;
        public override void OnReceive(Context context, Intent intent)
            PendingIntent pendingIntent = PendingIntent.GetActivity(AndroidApp.Context, pendingIntentId, intent, PendingIntentFlags.OneShot);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(AndroidApp.Context, channelId)
               .SetLargeIcon(BitmapFactory.DecodeResource(AndroidApp.Context.Resources, Resource.Drawable.OlympiaLogo))
               .SetDefaults((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate);

            manager = (NotificationManager)context.GetSystemService(AndroidApp.NotificationService);
            var notification = builder.Build();
            manager.Notify(messageId, notification);

And finally my MainActivity.cs (the one that doesn't get executed when I click on notification):

[Activity(Label = "LocalNotificationsApp", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, LaunchMode = LaunchMode.SingleTop)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity

    protected override void OnCreate(Bundle savedInstanceState)
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;


        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        CrossCurrentActivity.Current.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

        LoadApplication(new App());
        //AndroidNotificationManager.alarmManager = (AlarmManager)GetSystemService(Context.AlarmService);
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);

    protected override void OnNewIntent(Intent intent)

    void CreateNotificationFromIntent(Intent intent)
        if (intent?.Extras != null)
            string title = intent.Extras.GetString(AndroidNotificationManager.TitleKey);
            string message = intent.Extras.GetString(AndroidNotificationManager.MessageKey);
            if(title != null && message != null)
                DependencyService.Get<INotificationManager>().ReceiveNotification(title, message);

please I just even dont understand the problem, I need urgent help!

Sign In or Register to comment.