Search code examples
c#androidxamarin.androidalarmmanagerandroid-alarms

Xamarin - Alarm triggers but Receiver doesn't get called


So I set up an Alarm using AlarmManager, adb logcat shows that the alarm triggers but it won't execute the Receiver's OnReceive function.

Manifest:

<receiver android:name=".AlarmReceiver" android:enabled="true">
    <intent-filter>
        <action android:name="com.companyname.APPNAME.SEND.REPORT"></action>
    </intent-filter>
</receiver>

Alarm Creation:

        if (Exists(context)) return;
        AlarmManager alarmMgr = (AlarmManager)context.GetSystemService(Context.AlarmService);
        Intent intent = new Intent("com.companyname.APPNAME.SEND_REPORT");
        PendingIntent alarmIntent = PendingIntent.GetBroadcast(context, 1337, intent, PendingIntentFlags.CancelCurrent);

        Calendar calendar = Calendar.Instance;
        calendar.TimeInMillis = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

        alarmMgr.SetInexactRepeating(AlarmType.RtcWakeup, calendar.TimeInMillis, 1000 * 10, alarmIntent);

Receiver:

public class AlarmReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        Classes.Log.Write("Reports alarm received.");
        PowerManager pm = (PowerManager)context.GetSystemService(Context.PowerService);
        PowerManager.WakeLock wl = pm.NewWakeLock(WakeLockFlags.Partial, "SendReports");
        wl.Acquire();

        Classes.Network.SendReportsAsync();

        wl.Release();
        Classes.Log.Write("Reports alarm finished.");
    }
}

ADB Logcat logs:

07-23 11:08:46.723  1312  1863 V AlarmManager: Triggering alarm #0: 0 when =1595491710351 package =com.companyname.APPNAME operation =*walarm*:com.companyname.APPNAME.SEND_REPORT flags =0x0

Receiver logs are never written to my log file and no reports are sent, meaning the Receiver was never executed.

Thanks in advance.

UPDATE: This is after I changed the Intent to typeof(AlarmReceiver), that's the output

07-23 17:43:20.616  1312  1863 V AlarmManager: Triggering alarm #0: 0 when =1595515400614 package =com.companyname.ReportOne operation =*walarm*:com.companyname.ReportOne/md5057b6bb0fb853ea7a7a76aecdac4ee0b.AlarmReceiver flags =0x0

AlarmReceiver still won't execute

UPDATE 2:

This is the current code for initiating the Alarm

public static void Init(Context context)
    {
        if (Exists(context)) return;
        AlarmManager alarmMgr = (AlarmManager)context.GetSystemService(Context.AlarmService);
        Intent intent = new Intent(context, typeof(AlarmReceiver));
        PendingIntent alarmIntent = PendingIntent.GetBroadcast(context, 1337, intent, PendingIntentFlags.CancelCurrent);

        // Set the alarm to start at 7:00 a.m.
        Calendar calendar = Calendar.Instance;
        calendar.TimeInMillis = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

        Classes.Log.Write(string.Format("Initializing Alarm, next alarm will fire at {0}",
            DateTimeOffset.FromUnixTimeMilliseconds(GetMilliseconds()).ToString("g")));

        alarmMgr.SetInexactRepeating(AlarmType.RtcWakeup, calendar.TimeInMillis, 1000 * 10, alarmIntent);
    }

Solution

  • So after some research I realized that in Xamarin you shouldn't be registering your receivers from the manifest file, Xamarin.Android provides an Attribute for that.

    [BroadcastReceiver(Enabled = true, Exported = false)]
    public class AlarmReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            Classes.Log.Write("Reports alarm received.");
            PowerManager pm = (PowerManager)context.GetSystemService(Context.PowerService);
            PowerManager.WakeLock wl = pm.NewWakeLock(WakeLockFlags.Partial, "SendReports");
            wl.Acquire();
    
            //Classes.Network.SendReportsAsync();
    
            wl.Release();
            Classes.Log.Write("Reports alarm finished.");
        }
    }