Mr.Boter
Mr.Boter

Reputation: 13

android notifications dont appear on its time

hey guys i am working on android application , i am trying to creat a notification for a specific time like ( 03 - august -2012 at 5:00 pm ) , so i used the calender to set the time and then using calendar.getTimeInMillis i get the time of the notification , but unfortunatlly when i run my application on the emulator once the application starts the notification appears without caring about the specfic time, i.e its not 4:00 pm the notification appears on the top and time stamped with 5:00pm , heres my notification code :

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

int icon = R.drawable.ieee;
CharSequence tickerText = "Hello";

//   long when = System.currentTimeMillis();

//setting custom time 
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.DAY_OF_MONTH,03);
calendar.set(Calendar.HOUR_OF_DAY,17);
calendar.set(Calendar.MINUTE,26);
calendar.set(Calendar.SECOND, 00);
long when = calendar.getTimeInMillis();        // notification time

Notification notification=new Notification (icon, tickerText, when);

Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(1, notification);

now everything is running , but there's a little problem ,,, for example if the date&time now is 9/9/2012 , 9:00 am ,,, and i set the alarm for tomorrow at 10am , okey it will work , but when i open the application the day after tomorrow first thing the app will do is to do notification of the old alarm , i tried this by setting old alarm dates and when i open the app the first thing it does is showing old alarms , so any ideas about solving this ?

Upvotes: 1

Views: 257

Answers (2)

Ryan Gray
Ryan Gray

Reputation: 824

Because it will not wait when you haven't told it to. You can't set the Calendar time and then expect the notification service to wait.

You need to instead using a TimerTask or AlarmManager for this to generate an event at the time you want. Then create the notification inside your implementation of either one.

Upvotes: 2

Mr.Boter
Mr.Boter

Reputation: 13

ahaaa , i guess i got your point , or i hope so lol , its like setting the alarm manager on a specific date , so when this date comes the alarm manager sets a flag to certain value "enum i.e" and then within this code segment there will be a checking function which each time it checks this flag if its up then do certain task , right ? http://androidideasblog.blogspot.com/2011/07/alarmmanager-and-notificationmanager.html this will helps ALOT !!

Upvotes: 0

Related Questions