Search code examples
androidxamarinandroid-activityxamarin.androidandroid-notifications

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 (because 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)
            CreateNotificationChannel();

        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);
        intent.AddFlags(ActivityFlags.ClearTop);

        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)
               .SetContentIntent(pendingIntent)
               .SetContentTitle(title)
               .SetContentText(message)
               .SetLargeIcon(BitmapFactory.DecodeResource(AndroidApp.Context.Resources, Resource.Drawable.OlympiaLogo))
               .SetSmallIcon(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;

        base.OnCreate(savedInstanceState);

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

        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)
    {
        CreateNotificationFromIntent(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);
        }
    }
}

Solution

  • Try creating a brand new intent and using the main activity instead of reusing the activity passed into OnReceived

    [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)
        {
            // pull stuff from old intent.
            string title = intent.Extras.GetString(TitleKey);
            string message = intent.Extras.GetString(MessageKey);
    
            // Instead of using intent that is passed in. Create a new one.
            Intent mainIntent = new Intent(AndroidApp.Context, typeof(MainActivity));
            var bundle = new Bundle();
            bundle.PutString(TitleKey, title);
            bundle.PutString(MessageKey, message);
            mainIntent.PutExtras(bundle);
            mainIntent.AddFlags(ActivityFlags.ClearTop);
    
            PendingIntent pendingIntent = PendingIntent.GetActivity(AndroidApp.Context, pendingIntentId, intent, PendingIntentFlags.UpdateCurrent);
    
            NotificationCompat.Builder builder = new NotificationCompat.Builder(AndroidApp.Context, channelId)
                .SetContentIntent(pendingIntent)
                .SetContentTitle(title)
                .SetContentText(message)
                .SetLargeIcon(BitmapFactory.DecodeResource(AndroidApp.Context.Resources, Resource.Drawable.OlympiaLogo))
                .SetSmallIcon(Resource.Drawable.OlympiaLogo)
                .SetDefaults((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate);
    
            manager = (NotificationManager)context.GetSystemService(AndroidApp.NotificationService);
            var notification = builder.Build();
            manager.Notify(messageId, notification);
        }
    }