0

I'm trying to figure out how to run some code when an alarmmanager alarm fires. I currently can schedule the alarms, and right now I am sending a broadcast when the alarm fires and I have a broadcast receiver to pick it up. The problem is when the user closes the app by swiping it out of recent apps, my broadcast receivers get stopped, so even when the broadcast is sent, there is no receiver to pick it up. How can I run code straight from the alarm?

Here is my code I am using right now to create a notification at a certian time:

Creating the alarm:

 alarmManager.SetExactAndAllowWhileIdle(AlarmType.ElapsedRealtimeWakeup, SystemClock.ElapsedRealtime() + ((long)(App.checkInTimes[i].TimeOfDay.TotalSeconds - DateTime.Now.TimeOfDay.TotalSeconds) * 1000), PendingIntent.GetBroadcast(Android.App.Application.Context, 0, new Intent("android.intent.action.CREATE_CHECKIN_NOTIFICATION"), PendingIntentFlags.UpdateCurrent));

And the broadcast receiver to create the notification:

[IntentFilter(new[] { "android.intent.action.CREATE_CHECKIN_NOTIFICATION" })]
public class NotificationReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        //Send Check In Notification
        //Setup notification
        Notification.Builder builder = new Notification.Builder(Android.App.Application.Context);
        builder.SetContentTitle("Please Check In Now");
        builder.SetContentText("Tap here to check in");
        builder.SetSmallIcon(Resource.Drawable.exclamationPoint);
        builder.SetPriority(2);
        long[] pattern = { 1000, 1000, 1000, 1000 };
        builder.SetVibrate(pattern);
        builder.SetLights(Android.Graphics.Color.Red, 1500, 1500);
        Intent launchIntent = new Intent(Android.App.Application.Context, typeof(CheckInScreen));
        PendingIntent pendingIntent = PendingIntent.GetActivity(Android.App.Application.Context, 1, launchIntent, PendingIntentFlags.UpdateCurrent);
        builder.SetContentIntent(pendingIntent);

        //Build notification
        Notification notification = builder.Build();
        notification.Flags = NotificationFlags.AutoCancel;

        //Get Notification Manager
        NotificationManager notificationManager = Android.App.Application.Context.GetSystemService(Context.NotificationService) as NotificationManager;
        //Publish Notification
        notificationManager.Notify(0, notification);}
Joe Fioti
  • 421
  • 4
  • 15

1 Answers1

0

From this:

The problem is your BroadcastReceiver does not have the [BroadcastReceiver] attribute.

Looking at your code, this is exactly the problem. You are missing said attribute.

Try adding

[BroadcastReceiver]

before your class definition.

jamesfdearborn
  • 769
  • 1
  • 10
  • 26