Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start notification at custom timing?

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);
like image 556
Aniruddh Ambarkar Avatar asked Jan 30 '12 12:01

Aniruddh Ambarkar


2 Answers

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

like image 79
Stefan Avatar answered Sep 27 '22 17:09

Stefan


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);
like image 35
parag Avatar answered Sep 27 '22 18:09

parag