How to start notification at custom timing? - android

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);

Related

Send Notification at a specific time

I need to, for example, at 1PM to send a notification. how can i do this and repeat it periodically.
I need this code to be implemented with the "repeat code" :
Intent intent = new Intent();
PendingIntent pi = PendingIntent.getActivity(this, 0, intent , 0);
Notification notification = new NotificationCompat.Builder(this)
.setTicker("Ticker Title")
.setSmallIcon("icon")
.setContentTitle("Notification Content Title")
.setContentText("Output")
.setContentIntent(pi)
.setAutoCancel(true)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
Intent myIntent = new Intent(this , NotifyService.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(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(), 12*60*60*1000 , pendingIntent);
using the notify service and alarm manager you can do the required.
Use alarm manager class to start an intent on your desired time.
AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 02);
calendar.set(Calendar.MINUTE, 00);
// setRepeating() lets you specify a precise custom interval--in this case,
// 1 day
alarmMgr.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis()/1000,
AlarmManager.INTERVAL_DAY, alarmIntent);
Here, you need to provide the time at which you want to call the intent. In my code, it is 2AM every night. Also, for repeat interval, I selected a day because my process needed to be called on daily basis.

How to set reminders in android using background service?

Hi I am working on a application where I am getting reminder details from user as reminder Date and reminder name and storing those in database. Now I want to know how can I start reminder through background service? Or Is there any other way if yes please tell me. What should I do? Please help me. Suggest something or tutorial will be great idea.
Thanks in advance!
For this you will require two things
Alarm manager: To set the time for notification or alarm (daily, weekly basis)
Service: To launch your notification when alarm manager goes off
Do this in your activity class where you want to set the reminder
Intent myIntent = new Intent(this , NotifyService.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
//get time from database and initialise the variables.
int minute;
int hour;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.HOUR, hour);
calendar.set(Calendar.AM_PM, Calendar.AM); //set accordingly
calendar.add(Calendar.DAY_OF_YEAR, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);
This will trigger Alarm each day at your set time.
Now, you should create a Service as NotifyService and put the following code in its onCreate():
#Override
public void onCreate() {
NotificationManager mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.notification_icon, "Notify Alarm strart", System.currentTimeMillis());
Intent myIntent = new Intent(this , MyActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "Notify label", "Notify text", contentIntent);
mNM.notify(NOTIFICATION, notification);

date time pickers wrong values for alarm manager?

I am using AlarmManager to set alarm and i want to save its id in my sqlite db, whenever i set the alarm, it fires immediately, i googled the solution, but did'nt find according to my requirement, may be i am getting wrong date and time from my date time picker widgets,
I am using date and time pickers as:
final DatePicker datePicker = (DatePicker)dialog.findViewById(R.id.datePicker);
final TimePicker timePicker = (TimePicker)dialog.findViewById(R.id.timePicker);
Then a dialoge appears, i change date and time, and then then get it as :
Calendar calendar = Calendar.getInstance();
calendar.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(),
timePicker.getCurrentHour(), timePicker.getCurrentMinute(), 0);
timeSetForAlert = calendar.getTimeInMillis()/1000;
And then i pass this to service handler like,
Intent myIntent = new Intent(thisContext, MyReceiver.class);
myIntent.putExtra("timeSetForAlert", timeSetForAlert);
pendingIntent = PendingIntent.getBroadcast(thisContext, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, timeSetForAlert, pendingIntent);
I am skipping broadcast receiver here which takes intent and start alarm service, while alarm service is as:
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
long timeSetForAlert = intent.getLongExtra("timeSetForAlert", 0);
mManager = (NotificationManager) thisContext.getSystemService(thisContext.NOTIFICATION_SERVICE);
Intent intent1 = new Intent(thisContext,MainActivity.class);
Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", timeSetForAlert);
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity( thisContext,0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(thisContext, "Notification..", "This is a test message!", pendingNotificationIntent);
mManager.notify(startId, notification);
Toast.makeText(thisContext, startId+"", Toast.LENGTH_SHORT).show();
}
Please suggest:
1) - Why alarm fires immediately
2) - where should i save it in my db
try using this
Calendar calendar = Calendar.getInstance();
calendar.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(),
timePicker.getCurrentHour(), timePicker.getCurrentMinute(), 0);
timeSetForAlert = calendar.getTimeInMillis();//try without dividing with 1000
ya it can be solved like this.
accept both the answers..
code:
Instead of this
pendingIntent = PendingIntent.getBroadcast(thisContext, 0, myIntent,0);
PendingIntent pi = PendingIntent.getBroadcast(
getApplicationContext(), uniqueValue, intent, 0);
instead of "0" use unique value as second parameter to get multipleBroadcast.

Set notification to specific time

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);
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!
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.

android multiple alarm setting

I want to set a multiple alarm in android. please help me in achieving this task.
Thanks &Regards
Pawan Pathak
You probably need to use multiple intents.
In this example I set 2 alarms, one after 10 seconds and another one after 15 seconds.
Hope it helps.
// set first alarm
Calendar time1 = Calendar.getInstance();
time1.add(Calendar.SECOND, 10);
// set intent to be fired
PendingIntent sender1 = PendingIntent.getBroadcast(this, 1, new Intent(this, AlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
// set alarm manager
AlarmManager alarm1 = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarm1.set(AlarmManager.RTC_WAKEUP, time1.getTimeInMillis(), sender1);
// set second alarm
Calendar time2 = Calendar.getInstance();
time2.add(Calendar.SECOND, 15);
// set intent to be fired
PendingIntent sender2 = PendingIntent.getBroadcast(this, 2, new Intent(this, AlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
// set alarm manager
AlarmManager alarm2 = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarm2.set(AlarmManager.RTC_WAKEUP, time2.getTimeInMillis(), sender2);

Categories

Resources