9

I would like my notification to run at 12:00pm everyday. How do you replace the when value with a time?

NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify = new Notification(R.drawable.icon,"Its Time to Eat",when);

Context context = GrubNOWActivity.this;
CharSequence title = "Its Time to Eat";
CharSequence details = "Click Here to Search for Restaurants";
Intent intent = new Intent(context,Search.class);
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
notify.setLatestEventInfo(context, title, details, pending);
nm.notify(0,notify);
Adam Wagner
  • 15,469
  • 7
  • 52
  • 66
Kerde Severin
  • 95
  • 1
  • 2
  • 6

2 Answers2

41

You can use an alarm manager

Intent myIntent = new Intent(ThisApp.this , myService.class);     
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(ThisApp.this, 0, myIntent, 0);

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent);  //set repeating every 24 hours

and put the notification inside myService class!

SillySam
  • 98
  • 7
Alex Fragotsis
  • 1,248
  • 18
  • 36
1

The when parameter is for sorting the notifications in the status bar. You should code your app such that it fires the notification at the desired time.

r.v
  • 4,697
  • 6
  • 35
  • 57