Rucsi Rucsi - 4 months ago 7x
Android Question

How to send a notification on specific dates by checking the date

I have an application which I want to send notifications in odd days (like 1,3,5 days of month). After research, I understood that I need a service. But I can't figure out how to get the new date every day, or how to notice the service that the day has changed... Something like a listener on day changed... I searched a lot, and only found references on how to schedule a notification at a certain date, but that's not what I need, as I want to verify date every day. Thank you very much for any help !


You can do this with the help of Android Alarms.

The steps that you need to do :

  1. Start an Alarm for the date and time at which you need to show the notification.

  2. Subscribe for Reboot Broadcast receiver and set the same alarm again (since the alarm that you set will be gone once the device is restarted)

  3. Once the alarm is ticked, you can show the notification from the Alarm Broadcast receiver and then set for the new alarm.

To schedule an ALarm :

AlarmManager mgr=
Intent i=new Intent(ctxt, ScheduledService.class);
PendingIntent pi=PendingIntent.getService(ctxt, 0, i, 0);

                 SystemClock.elapsedRealtime() + DIFF_PERIOD, PERIOD, pi);

Where PERIOD : 48hours in millisecond

DIFF_PERIOD : time to your nearest tick. That is if you are in day 2 and you need to trigger the alarm in day 3, then this value will be : 24hrs in millisecond

To answer your comment.

To find the day of the month, you can use the follwing :

Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);