5

I know how to start a notification X milliseconds after some click event. A code like this

        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                triggerNotification();
            }
        };
        timer.schedule(timerTask, 3000);

Where the code for notification looks like this

CharSequence title = "Hello";
CharSequence message = "Hello, Android!";

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon, "A New Message!", System.currentTimeMillis());

Intent notificationIntent = new Intent(this, AndroidAlarmService.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.setLatestEventInfo(AndroidAlarmService.this, title, message, pendingIntent);
notification.defaults = Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(NOTIFICATION_ID, notification);

How can I set notification to appear on specific date at specific time, let say October 1, 7 PM?

sandalone
  • 41,141
  • 63
  • 222
  • 338

1 Answers1

16

I think that the best way will be to create a service that sets the notification and then activate the service using an AlarmManager.
Here is my code for doing that. That's the code for the AlarmManager:

private void startAlarm() {
    AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
    Calendar calendar =  Calendar.getInstance();
    calendar.set(int year, int month, int date, int hour, int minute, int second);
    long when = calendar.getTimeInMillis();         // notification time
            Intent intent = new Intent(this, ReminderService.class);
            PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
            alarmManager.set(AlarmManager.RTC, when, pendingIntent);
        }

Here is the Service:

public class ReminderService extends IntentService {
    private static final int NOTIF_ID = 1;

    public ReminderService(){
        super("ReminderService");
    }

    @Override
      protected void onHandleIntent(Intent intent) {
        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        long when = System.currentTimeMillis();         // notification time
        Notification notification = new Notification(R.drawable.icon, "reminder", when);
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.flags |= notification.FLAG_AUTO_CANCEL;
        Intent notificationIntent = new Intent(this, YourActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent , 0);
        notification.setLatestEventInfo(getApplicationContext(), "It's about time", "You should open the app now", contentIntent);
        nm.notify(NOTIF_ID, notification);
    }

}
eladrich
  • 755
  • 1
  • 5
  • 15
  • And what can be the structure of the `when` variable if as `bergnam` said at October 1, 7 PM ? `long when = 01/10/2011 7PM;` ? – androniennn Sep 24 '11 at 18:53
  • @bergnam just simply needs to use a Date object set the date and then call .getTime() – eladrich Sep 24 '11 at 18:58
  • `SimpleDateFormat dateLongue = new SimpleDateFormat("DD MM yyyy HH:mm:ss");` and how and why calling .getTime ? – androniennn Sep 24 '11 at 19:02
  • @androniennn I added the code needed for setting the date, I can't check it right now but I think it'll work. – eladrich Sep 24 '11 at 19:04
  • 1
    bergnam, did my answer satisfy you? if it did please mark it as correct. @androniennn: `Calendar calendar = Calendar.getInstance(); calendar.set(int year, int month, int date, int hour, int minute, int second); long when = calendar.getTimeInMillis();` – eladrich Sep 26 '11 at 11:51
  • The post of bergnam helped me more than him :D. Thanks for helping, if i can i would accept your answer :\. Thank you :). – androniennn Sep 26 '11 at 14:46
  • Hi, marked as an answer. I had everything in my code except the SimpleDateFormat part which I used to implement desired start of alarm. Thanks! – sandalone Sep 26 '11 at 16:54
  • Hi @eladrich. I read your answer,but this method is not working fine for me.I tried it to set for particular date for eg.I tried for current i.e 2011/02/14 15:31 PM.It doesn't work for that.I am using broadcast receiver for it.Please help me out – Shashank_Itmaster Feb 14 '12 at 09:54
  • @Shashank_Itmaster Hi, don't forget that the year 0 is 1900 so the year 2011 is 111 when you set it using my method. Why did you use a broadcast receiver? – eladrich Feb 29 '12 at 13:31
  • @eladrich:- I got your point.But why are asking me such question,I can't able to get that out.Is there any problem related to this one?.If yes, then pls tell me about that.Thanks – Shashank_Itmaster Mar 07 '12 at 04:04
  • @Shashank_Itmaster There isn't any problem with Broadcast Receiver that I know of. I was just curious, didn't mean to get you worried ;) – eladrich Mar 07 '12 at 10:25
  • It works, but how to add several notifications in loop for specified date&time ? – valerybodak Nov 22 '12 at 09:06
  • Will this code work alone when the device reboots? or we have to start the `service` at boot up? – Shajeel Afzal Jul 11 '13 at 14:51