Forum General


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

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!


  • vivek00712vivek00712 Member ✭✭

    Comment the below line in your code.
    PendingIntent pendingIntent = PendingIntent.GetActivity(AndroidApp.Context, pendingIntentId, intent, PendingIntentFlags.OneShot);

    Use below code at the same place. When you tap on the notification, it will open the app from the last point.
    var manager = (NotificationManager)AndroidApp.Context.GetSystemService(Context.NotificationService);

  • soufianemarlysoufianemarly Member ✭✭✭
    edited June 2020

    add this to the mainactivity

            if (Intent.Extras != null)
                foreach (var key in Intent.Extras.KeySet())
                    if (key != null)
                        var value = Intent.Extras.GetString(key);
                        Log.Debug(TAG, "Key: {0} Value: {1}", key, value);

    and then you can redirect the notification to a certain page with


                   MessagingCenter.Send(Application.Current, "goToIntent", new yourpage());

    hope this will help

Sign In or Register to comment.