I want to set my notification in android on particular date and time, I am trying it by using date in java, but my notification is fired before time. so what can I go to get it fired on specified time. Thanks in advance!
Here is my code for notification:
Calendar cal = java.util.Calendar.getInstance();
cal.set(2012, 00, 30);
Date date = cal.getTime();
date.setHours(17);
date.setMinutes(30);
date.setSeconds(15);
long time = date.getTime();
Log.e("date is",""+date);
long when = time;
Notification notification = new Notification(notificationIcon,tickerText,date.getTime());
notification.when = date.getTime();
RemoteViews contentView = new RemoteViews("com.LayoutDemo",R.layout.userscreen);
notification.contentView= contentView;
Intent intent = this.getIntent();
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.contentIntent= contentIntent;
notification.flags= Notification.FLAG_AUTO_CANCEL;
int NOTIFICATION_ID =1;
nManager.notify(NOTIFICATION_ID,notification);
The field "when" of a notification is used to sort the notification in the status bar. It is not used to fire the notification at the specified time.
If you want to trigger an action at a specified time use AlarmManager: http://developer.android.com/reference/android/app/AlarmManager.html
use Android alarm services and in that set pending intent
Intent myIntent = new Intent(AndroidAlarmService.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With