1

I'm tryingto create an application in which I need to send the user notifications when the app isn't running. I'm doing it with a broadcast reciever which starts working when the date changes. Meaning when the date changes the broadcast reciever uses a handler in order to send a notification. when the app is running it works perfectly however when the app isn't running the broadcast reciever doesn't work and the notfication isn't sent.

here is the code:

first in the manifest I have:

<receiver android:name=".MyBroadcastRecieverDate" >
    <intent-filter>
      <action android:name="android.intent.action.DATE_CHANGED" />
    </intent-filter>
  </receiver>

in Main activity i build and register the broadcast reciever:

this.recieverDate = new MyBroadcastRecieverDate();

protected override void OnResume()
        {
            base.OnResume();
            RegisterReceiver(recieverDate, new IntentFilter(Intent.ActionDateChanged));
        }

and this is the reciever's class:

[BroadcastReceiver(Enabled = true)]
    [IntentFilter(new[] { Intent.ActionDateChanged })]
    class MyBroadcastRecieverDate : BroadcastReceiver
    {
        MyHandler handler;

        public MyBroadcastRecieverDate()
        {
        }

        public override void OnReceive(Context context, Intent intent)
        {
            if(intent.Action.Equals(Intent.ActionDateChanged))
            {
                handler = new MyHandler(context);
                Message msg = new Message();
                msg.Obj = "";
                msg.Arg1=1;
                handler.SendMessage(msg);
            }
        }
    }

If someone can please help me and tell me how to make the reciever work even when the app isn't running it will help me a lot. Thank you.

Bar Fur
  • 15
  • 3

1 Answers1

0

Before Android 8.0 we could use Service to active application in background/closed , check this answer .

However , android limits background execution from 8.0 , an Android application no longer have the ability to run freely in the background , there are several alternatives to service in order to perform background work , i suggest you take a look at Foreground Services .


Refer to Background Execution Limits in Android 8.0.

ColeX
  • 14,062
  • 5
  • 43
  • 240