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.
Related
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).
I am building a game and now I have to send user scores on every first of the month to the server .
I have multiple options like
Timer
ScheduledThreadPoolExecutor
Service
BroadcastReciever with AlarmManager.
but not sure which one to use.
for the following reasons:
I don't think Alarm or Timer will be the best as it doesn't make sense to have an alarm after every month as user may can install the app at any date.
besides that I can't even check the date from user device as what if the user has wrong time set on device?
And what if the user doesn't have the internet connection available at first of the month or what if he didn't launch the app on the first.
I know we can use service for this problem but again is it feasible to always run the service in background which only requires once a month?
I checked other SO post too like this Scheduling recurring task in Android
but its not same as my case as I only have to make the calls once in a month.
Thanks.
use
AlarmManager is your best bet.
Your Issue:-
while scheduling an alarm check the current date and schedule the alarm accodingly EX:- if today is 17th schedule alarm after 14 day.
Regarding getting the current date put the current date on server and get it from there
in my opinion you should write a service and execute timer in your service which will be executed after every 24 hours and than if you don't want to get date and time from user device you can use google API or any other API for getting time and than decide if app needs to update score or not.
I've made a program, which uses broadcastreceiver to create an alarm (which is activated after several days).
When the time comes, it is supposed to play a notification.
I've tried setting the time to few minutes, hours and the notification always plays.
However in real life testing when the time was over one day the notification doesn't work.
Is there a limit to which Broadcast receivers can be set to in the future?
Here is my code: [http://pastebin.com/JnxVExtK]
Let's say today is Sunday 5:00.
If I set the alarm at Sunday 7:09 - It will ring.
But if I set the alarm for Wednesday at 3:00 - it won't work.
And obviously I cannot set the emulator for such long period.
I've been trying the program on my tablet and there too the notification fails to show up if the alarm is set to ring after few days.
fallow the two steps and schedule the task perfectly
1.create date object
Date dateobj=new Date(year-1900,month,day,hour,min);
year - scheduling Year month-scheduling month(0-11) day-scheduling
day (1-30) hour scheduling hour (24 hrs format )(0-24) min
scheduling min 0-59
2. set the alarm to dateobj.getTime() its returns milli seconds
alarmManager.set(AlarmManager.RTC_WAKEUP, dateobj.getTime(),
pendingIntent);
I am creating small Alarm project which has week days option. (I am using alarm manager).
The alarm triggers perfectly on given time and day but the problem is that it also triggers any previous alarm if I change the day or time after to the given alarm time.
For eg.
If the alarm is set to ring at 5:00 AM Monday Wednesday Friday. - And the current time is 6:00PM Sunday.
And another alarm set for for 4:00PM Monday Wednesday Friday - The current time is 6:30PM Sunday.
For testing I changed the day to Tuesday 6:00PM - Immediately the two alarm triggers one by one for Monday's schedule.
How do I stop this specific alarm and trigger only next Monday rather immediately? Do I need to check the dates also???
Let me know!
You can't really. The alarm manager bases everything off of the system time. If you change the system time manually it is going to act just as if all the time had passed between now and whenever you set it for.
Edit: You can find the source code for the stock alarm application. Here is a link to one of the relavent objects Alarms.
I didn't test it any to ensure but from what I can tell they are storing the time the event is supposed to fire, and if it suddenly finds that it is in the past it ignores the alarm instead of letting it trigger. Some other relavent classes to check out are SetAlarm and AlarmClock, search for them on grepcode if you want to follow the way they set and react to the alarms in the stock app.
But it is still important to point out that manually changing the system time can still be used to manipulate the alarms. If for instance you set an alarm for 10:00pm tomorrow, then manually jump the time to 9:59pm tomorrow. The alarm will fire after 1 minute.
compare your alarm time with System.currentTimeMillis()
if your alarm time is smaller then the current time, than no need to set the alarm
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?