3

I've developed an app to schedule multiple local notifications to remind users to do something. Every month in the current year there should an notification be raised.

These local notifications are scheduled using an "AlarmManager". A notification is created and raised in the OnRetrieve of a "BroadcastReceiver".

It works all fine until the app is terminated (by user) or the device is rebooted.

After some research I found the solution to reschedule the alarm / local notifications if the device is rebooted => using a BroadcastReceiver with "ActionBootCompleted" as intent filter and then reschedule the notifications in the "OnReceive".

Unfortunately I can't find a decent solution to reschedule the alarm / local notifications if the app is terminated.

What is the best approach for this case?

StackFlower
  • 691
  • 1
  • 13
  • 29

1 Answers1

5

Try to run this as a background service. When the user opens up the app for the first time, call the service OnCreate(). Make sure the service is START_STICKY so it cannot be stopped unless you explicitly tell it to. Then place your AlarmManagers inside the service.

Ethan
  • 225
  • 2
  • 13
  • Actually, the service will be terminated when the user closes the application, but as the service is sticky, it will auto-restart. Downside of this approach is that it consumes resources, as the service is running on the device. – Martin C. Jan 25 '15 at 20:40
  • Yes, the service is restarted, and if you want to consume resources you can use a timer that will check the date after certain amounts of time (every hour, every day, etc.) – Ethan Jan 25 '15 at 20:49
  • @Ethan, is it desired that there will be a running service all the time? I can imagine this is a bit overkill just to schedule some local push notifications. It constantly consumes +/- 30mb RAM – StackFlower Jan 25 '15 at 21:54
  • That's the only way to schedule the push notifications after your app has been destroyed. Other wise your app would have to be open all the time to detect the time and schedule them. If you don't want to have the Service running all the time, then maybe you can start and stop a service from another service, which would only contain a simple timer to start and stop the first service. I have never done it before, and quite honestly it seems a little redundant to do that, but I cannot see any other way. – Ethan Jan 25 '15 at 22:30
  • It would have been clear to some of us with a provision of codes –  Oct 19 '17 at 10:27
  • @Ethan, help me on this please https://stackoverflow.com/questions/47052256/how-can-i-call-a-broadcastreceiver-inside-a-service-with-a-local-notification-x – Lutaaya Huzaifah Idris Nov 01 '17 at 15:03
  • Or please provide us an example demeostratting this . Thanks – Lutaaya Huzaifah Idris Nov 01 '17 at 15:16
  • Or please provide us an example demeostratting this . Thanks – Lutaaya Huzaifah Idris Nov 01 '17 at 15:16