How to show notifications at provided time frame and frequency in Android? - android

I'm trying to make a feature where user can select the time frame and interval in which he would receive notifications to his phone. I'm using AlarmManager for repeating notifications, however it only covers the start time of notifications and the interval/frequency of notifications.
How could I make the notifications go off only from user selected hours, let's say every day 1 PM - 8 PM (13:00 - 20:00) every 2 hours?
I was thinking about using WorkManager to start AlarmManager at selected start hour and to cancel AlarmManager on user selected end hour, that way WorkManager would handle the everyday start/end hours and AlarmManager would send a notification with provided frequency. But I'm not sure if it's a "correct" approach.

So, I think what you are proposing would work, but another option is to us a non-repeating alarm that sets the next alarm according to the users desired schedule that is persisted some how (e.g. shared preferences).

Related

Android notifications on different days of the week

I want to build notifications that are showed recurrently in specific days of the week, for example, I want X notification every monday and friday of each week. I know how to set intervals with alarms, but not how to select specific days
You can set repeating alarm for every day and show notification only those days that are needed on onReceive() function of the alarm BroadcastReceiver.
The logical way to control push notification timing is from server side but if you want to do it on your phone you must work with android os date and time and Java CALENDAR class.

Android, set custom timing of AlarmManager... Please advise

Hi I need to set AlarmManager to run a reminder for me to take medication. I need to repeat it by custom amount of days and custom amount of times to take in the day.
So is there an efficient way to set the AlarmManager or CommonsWare's Implementation of the AlarmManager to remind me "twice a day starting at 9AM for the next 5 days" to remind me to take medication? Pls advice and tnx in advance for any constructive help in sample code and in relevant tutorials.
I haven't looked into Mark's AlarmManager implementation, but there is no way, in general, to get the bare AlarmManager to do what you are trying to do. You can schedule a single alarm, at a specific time, or a repeating alarm, that repeats at fixed intervals. If you want something that handles complex schedules like the one you describe, you'll have to write or find code that does it.
You want to use a PendingIntent with the AlarmManager. The idea is to schedule the pendingIntent with the alarmManager, have that trigger an intentService or broadcast, setup another pendingIntent with the alarmManager for the next desired event. You want to keep in mind that you'll need the BOOT_RECEIVED permission in case the user reboots their device. I have complex scheduling in Audio Control and this is exactly what I do.
Here is a pretty decent tutorial of what I mean:
http://android-er.blogspot.com/2010/10/simple-example-of-alarm-service-using.html
You need to schedule an alarm to the next time you want to take the medicine - according to your algorithm (for example if its twice a day, and you got to the pending intent callback for the first time today, then schedule the next alarm to start after [6,7,8,9,10...] hours).
You will need to save both last time of the alarm launch and the user settings in shared prefs/file/DB.
You need to handle process down (android killed it or the device was rebooted). In the case of device reboot you should use the boot receiver to start your service, but you need to remember that from android 3.1 the user has to use at least one time the GUI in order for you to intercept the boot completed receiver. The boot completed receiver should look when was the last time that the alarm launched, and according to the user settings set the next alarm launch.
In the case of android killed your service, you will need to make research, i can't help here.
see example

Set repeating alarm once a month

I am using alarm manager along with broadcastReceiver. I am able to set an alarm and all that. But I am stuck with how to approach my problem. I need to be able to set a repeating alarm and the trick here is to have it repeat every 14th of a month at 4:00 pm (so monthly alarm).
How do I go about it? I know how to make it repeating every day or every week as it is easy to calculate the how many milli seconds in a week, but when we are talking monthly, every month has different number of days so I can't set it with a fixed interval.
Any help here?
Thank you
Based on the req. you provided above, I would only schedule one alarm at a time, just calculate the new time values when the previous one is triggered. One thing you will want to do as well is setup a service to listen for boot complete event. Alarms do not persist through restarts. you can find information on how to do that here How to start an Application on startup?

Everyday notifications at certain time

I would like to achieve this:
After first turning on the application, user receives notifications, every day at 2pm, if certain condition is true. If condition is false, we are not showing a notification this day. The condition is checked at 2pm, it downloads some data from the Internet.
So far I used AlarmManager and its method setRepeating() with 24h interval. AlarmManager fires up a Service. In this Service I'm downloading the data, checking condition and if it's true - showing Notification. Since downloading can last more than 5 seconds, I've declared android:process=":background" for this Service, to run it in separate process and not block my UI.
This approach has two drawbacks:
1: If user opens application let's say at 4pm (and the condition is true), he will receive the notification immediately. From setRepeating() documentation:
If the time occurs in the past, the alarm will be triggered
immediately, with an alarm count depending on how far in the past the
trigger time is relative to the repeat interval.
I would like that user will not receive a notification this day, only the next day and so on.
2: I'm worried that my notifications will not show after user switch the phone off. From AlarmManager documentation:
Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.
I don't know if it's possible to make it work all the time.
If you have any ideas how to make it better, you're welcome.
1: I'm not quite sure if i understood your question, but I think all you have to do is if it already is past 2pm add a day to 2pm:
GregorianCalendar twopm = new GregorianCalendar();
twopm.set(GregorianCalendar.HOUR_OF_DAY, 14);
twopm.set(GregorianCalendar.MINUTE, 0);
twopm.set(GregorianCalendar.SECOND, 0);
twopm.set(GregorianCalendar.MILLISECOND, 0);
if(twopm.before(new GregorianCalendar())){
twopm.add(GregorianCalendar.DAY_OF_MONTH, 1);
}
alarmManager.setRepeating(type, twopm.getTimeInMillis(), 1000*60*60*24, intent);
2: You could register a BroadcastReceiver for booting and start your alarm again there. Take a look at this: Android BroadcastReceiver on startup - keep running when Activity is in Background

Creating alarm with AlarmManager for a specific period of time

Is there any way to use AlarmManager to activate an alarm for a specific period of time? I have start-time and end-time values stored in the database. I want to start an alarm at start-time that will make the device silent and alarm should end at end-time when the device volume will be normal again.
One way is set alarm at start-time & then set another alarm at end-time. But the problem is the time period may overlap that will need additional logic to be implemented if I go with 2 different alarms(one at start-time, another at end-time). Is there any procedure in Android to cope with this situation? Or implementing logic is the only way to overcome this issue?
Why not just in your Intent for the PendingIntent pass an extra like "endTime" long type for the time you want to end the alarm. (im assuming its repetitive) then in your broadcast receiver get that extra, compare to System.currentTimeMillis() and if it is less then current time cancel the alarm and exit?

Categories

Resources