Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

AlarmManager not firing when the app is closed ...

MuhiMuhi Member ✭✭

The functionality is setting up a reminder for a user.
This solution works fine if the app is in forground / background.
But if I close the app I get no alert.

I'm using the AlarmManger and calling the SetAndAllowWhileIdle method. With RtcWakeup , the time , pendingIntent.
I have a handler class : BroadcastReceiver this is set to Enabled = true.
In the OnReceive override I get the extra from the intent and I build a local notification out of it in order to notify the user.

I have looked around a lot and tried a lot of settings on the phone battery savings settings .. etc but no luck..

If some one could point me in a direction that would help a lot.

Thanks

Best Answers

  • LandLuLandLu Member, Xamarin Team Xamurai
    Accepted Answer

    All of the code will be frozen when your application is closed including the alarm manager.
    If you want it still alive, we could use foreground service:
    https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/services/foreground-services
    This may be a little out of date, here is my approach to create the notification:

    [Service]
    public class SampleService : Service
    {
        public static string CHANNEL_ID = "com.channelid";
        public static string CHANNEL_NAME = "com.channelname";
        public override void OnCreate()
        {
            base.OnCreate();
    
    
        }
    
        public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
        {
            registerNotificationChannel();
            int notifyId = (int)JavaSystem.CurrentTimeMillis();
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
            mBuilder.SetSmallIcon(Resource.Mipmap.ic_launcher);
            if (Build.VERSION.SdkInt < BuildVersionCodes.N)
            {
                mBuilder.SetContentTitle("app name");
            }
            StartForeground(notifyId, mBuilder.Build());
            return StartCommandResult.Sticky;
        }
    
        private void registerNotificationChannel()
        {
            if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                NotificationManager mNotificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
                NotificationChannel notificationChannel = mNotificationManager.GetNotificationChannel(CHANNEL_ID);
                if (notificationChannel == null)
                {
                    NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                            CHANNEL_NAME, NotificationImportance.High);
                    channel.EnableLights(true);
                    channel.LightColor = Color.Red;
                    channel.LockscreenVisibility = NotificationVisibility.Public;
                    mNotificationManager.CreateNotificationChannel(channel);
                }
            }
        }
    
        public override IBinder OnBind(Intent intent)
        {
            throw new NotImplementedException();
        }
    }
    
  • LandLuLandLu Member, Xamarin Team Xamurai
    Accepted Answer

    You could put the alarm manager in the service directly.
    The foreground service is used to keep our application alive even though the main application has been quit by the user.
    And then it ensures that the alarm manager could trigger the code in the broadcast receiver class.

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai
    Accepted Answer

    All of the code will be frozen when your application is closed including the alarm manager.
    If you want it still alive, we could use foreground service:
    https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/services/foreground-services
    This may be a little out of date, here is my approach to create the notification:

    [Service]
    public class SampleService : Service
    {
        public static string CHANNEL_ID = "com.channelid";
        public static string CHANNEL_NAME = "com.channelname";
        public override void OnCreate()
        {
            base.OnCreate();
    
    
        }
    
        public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
        {
            registerNotificationChannel();
            int notifyId = (int)JavaSystem.CurrentTimeMillis();
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
            mBuilder.SetSmallIcon(Resource.Mipmap.ic_launcher);
            if (Build.VERSION.SdkInt < BuildVersionCodes.N)
            {
                mBuilder.SetContentTitle("app name");
            }
            StartForeground(notifyId, mBuilder.Build());
            return StartCommandResult.Sticky;
        }
    
        private void registerNotificationChannel()
        {
            if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                NotificationManager mNotificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
                NotificationChannel notificationChannel = mNotificationManager.GetNotificationChannel(CHANNEL_ID);
                if (notificationChannel == null)
                {
                    NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                            CHANNEL_NAME, NotificationImportance.High);
                    channel.EnableLights(true);
                    channel.LightColor = Color.Red;
                    channel.LockscreenVisibility = NotificationVisibility.Public;
                    mNotificationManager.CreateNotificationChannel(channel);
                }
            }
        }
    
        public override IBinder OnBind(Intent intent)
        {
            throw new NotImplementedException();
        }
    }
    
  • MuhiMuhi Member ✭✭

    @LandLu Thanks for the response. So I'm assuming I can user the Alarm manager in the foreground service to set the time for my local notification to be sent ?

  • LandLuLandLu Member, Xamarin Team Xamurai
    Accepted Answer

    You could put the alarm manager in the service directly.
    The foreground service is used to keep our application alive even though the main application has been quit by the user.
    And then it ensures that the alarm manager could trigger the code in the broadcast receiver class.

  • MuhiMuhi Member ✭✭

    @Landlu Thanks I did end up putting it in the service but I didn't like it there. Thanks for all your help

Sign In or Register to comment.