Alarm manager does not trigger broadcast receiver when app is closed

55 views Asked by At

I'm trying to create a recurrent alarm that will trigger my broadcast service(ANDROID), i've searched in a lot of places and tried a lot of combinations but when the app is closed, the broadcast service is not called anymore.

This is the broadcast receiver.

[BroadcastReceiver(Exported = true, Enabled = true)]
[IntentFilter(new[] { "com.companyname.appName.START_ALARM" })]
public class MyAlarmService : BroadcastReceiver
{
    private WakeLock _wakeLock;

    public override void OnReceive(Context? context, Intent? intent)
    {
        PowerManager pm = (PowerManager)Android.App.Application.Context.GetSystemService(Context.PowerService);
        _wakeLock = pm.NewWakeLock(WakeLockFlags.Partial, "YourWakeLockTag");
        _wakeLock.Acquire();

        ExecuteStuff();

        ScheduleNextAlarm(context);

        _wakeLock.Release();
    }

    private async void ExecuteStuff()
    {
        var manageNotifications = new ManageNotifications();

        var notificationFromDB = await manageNotifications.CheckNotificationForSending();

        if (!string.IsNullOrWhiteSpace(notificationFromDB.ExecutionTime))
        {
            var notificationToSend = new NotificationCompat.Builder(Platform.CurrentActivity, "1002")
              .SetSmallIcon(Resource.Drawable.logo)
              .SetContentTitle(notificationFromDB.Title)
              .SetContentText(notificationFromDB.Message)
              .SetPriority(NotificationCompat.PriorityHigh)
              .SetGroup("example_group")
              .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))
              .Build();

            var notificationManager = NotificationManagerCompat.From(Platform.CurrentActivity);
            notificationManager.Notify(2, notificationToSend);
        }
    }

    private void ScheduleNextAlarm(Context context)
    {
        var alarmIntent = new Intent(Platform.CurrentActivity, typeof(MyAlarmService));
        alarmIntent.SetAction("com.companyname.appName.START_ALARM");
        var pendingIntent = PendingIntent.GetBroadcast(Platform.CurrentActivity, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);

        var alarmManager = (AlarmManager)Android.App.Application.Context.GetSystemService(Context.AlarmService);

        long triggerTime = SystemClock.ElapsedRealtime() + 6 * 1000;
        alarmManager.SetExact(AlarmType.RtcWakeup, SystemClock.ElapsedRealtime()+ triggerTime, pendingIntent);
    }
}

and the receiver in the manifest:

<receiver android:name=".MyAlarmService"
          android:exported="true"
          android:process=":remote">
    <intent-filter>
        <action android:name="com.companyname.appName.START_ALARM"/>
    </intent-filter>
</receiver>```

I'm firstly calling the receiver from another method in the same way as the ScheduleNextAlarm method, after the first call, the recurrent alarm should do its job and call the broadcast receiver all the time, it is working only when the app is running, if it is closed from recent apps it is not working anymore. I really don't know what the problem could be.. Also, i have to mention that the Broadcast receiver class is declared in a file in the same folder as the AndroidManifest.xaml.

0

There are 0 answers