I want to fire different notifications when a specific alarmManager is reached.
For example I want to display "independence day" when we reach the date of Independence stored in a certain alarmManager, and to display "Steve jobs died" when we reach the date of his death stored in another Alarm Manager, and so on.
So my question is how to link each alarmManager(cal and cal2) with different notification?
I did this until now,
in MainActivity:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE,19);
cal.set(Calendar.MONTH,11);
cal.set(Calendar.YEAR,2015);
cal.set(Calendar.HOUR_OF_DAY, 21);
cal.set(Calendar.MINUTE, 42);
cal.set(Calendar.SECOND, 30);
Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.DATE,19);
cal2.set(Calendar.MONTH,11);
cal2.set(Calendar.YEAR,2015);
cal2.set(Calendar.HOUR_OF_DAY, 21);
cal2.set(Calendar.MINUTE, 43);
cal2.set(Calendar.SECOND, 10);
Intent alertIntent = new Intent(this, AlertReceiver.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
alarmManager.set(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), PendingIntent.getBroadcast(this, 2, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
notice that I made to dates to fire notification and it is working, the notification in onReceive was fired twice, the first time when I reach "cal" and for the second time when I reach "cal2". But as I mentioned above, I need to fire a different notification when I reach cal2
this is the OnReceive:
public class AlertReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
createNotification(context, "title1", "independence day", "event of today");
//here i want to link this notification with AlarmManager containing Cal as a date
createNotification(context, "title2", "steve jobs died", "event of today");
//here i want to link this notification with AlarmManager containing Cal2 as a date
}
public void createNotification(Context context, String msg, String msgText, String msgAlert) {
PendingIntent notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.not)
.setContentTitle(msg)
.setTicker(msgAlert)
.setContentText(msgText);
//intent to fire when notification clicked on
mBuilder.setContentIntent(notificIntent);
//how the person will be notified
mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
//cancel notification when clicked in the taskbar
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager= (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0,mBuilder.build());
}
after applying what mentioned below: i made this 4 events:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent alertIntent = new Intent(this, AlertReceiver.class);
alertIntent.putExtra("title", "event1");
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
Intent alertIntent2 = new Intent(this, AlertReceiver.class);
alertIntent2.putExtra("title", "event2");
alarmManager.set(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), PendingIntent.getBroadcast(this, 2, alertIntent2, PendingIntent.FLAG_UPDATE_CURRENT));
Intent alertIntent3 = new Intent(this, AlertReceiver.class);
alertIntent3.putExtra("title", "event3");
alarmManager.set(AlarmManager.RTC_WAKEUP, cal3.getTimeInMillis(), PendingIntent.getBroadcast(this, 3, alertIntent3, PendingIntent.FLAG_UPDATE_CURRENT));
Intent alertIntent4 = new Intent(this, AlertReceiver.class);
alertIntent4.putExtra("title", "event4");
alarmManager.set(AlarmManager.RTC_WAKEUP, cal4.getTimeInMillis(), PendingIntent.getBroadcast(this, 4, alertIntent3, PendingIntent.FLAG_UPDATE_CURRENT));
and in OnReceive:
createNotification(context,intent.getStringExtra("title"), "event", " event");
sometimes for the first time running the application all the events appear, sometimes the 3rd appear twice, and the first two do not appear, sometimes just the last one appear, so there is something wrong or messy in my code,
whats the problem?
calendar:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE,24);
cal.set(Calendar.MONTH,11);
cal.set(Calendar.YEAR,2015);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 55);
cal.set(Calendar.SECOND, 10);
Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.DATE,24);
cal2.set(Calendar.MONTH,11);
cal2.set(Calendar.YEAR,2015);
cal2.set(Calendar.HOUR_OF_DAY, 23);
cal2.set(Calendar.MINUTE, 55);
cal2.set(Calendar.SECOND, 20);
Calendar cal3 = Calendar.getInstance();
cal3.set(Calendar.DATE,24);
cal3.set(Calendar.MONTH, 11);
cal3.set(Calendar.YEAR,2015);
cal3.set(Calendar.HOUR_OF_DAY, 23);
cal3.set(Calendar.MINUTE, 55);
cal3.set(Calendar.SECOND, 30);
Calendar cal4 = Calendar.getInstance();
cal4.set(Calendar.DATE,24);
cal4.set(Calendar.MONTH, 11);
cal4.set(Calendar.YEAR,2015);
cal4.set(Calendar.HOUR_OF_DAY, 23);
cal4.set(Calendar.MINUTE, 55);
cal4.set(Calendar.SECOND, 40);
You can pass that information along with the intent while scheduling the alarm.
Intent alertIntent = new Intent(this, AlertReceiver.class);
alertIntent.putExtra("Notification Key", 1);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
And in your onReceive check the value and show the notification based on that:
Integer notificationId = intent.getIntExtra("Notification Key", -1);
if (notificationId == 1)
{
// Show indipendente day
}
else
{
// do something else
}
Instead of passing integer value, you can also pass the notification message and show it directly without any if...else condition.
Passing string data:
alertIntent.putExtra("Notification Key", "Independence day");
Getting string back:
String message = intent.getStringExtra("Notification Key");
// Calling the notification
createNotification(context, "title1", message, "event of today");
You could make different intents and make
intent.putExtra("title","independence day");
and also make a separate id for every notification you could do it like this.
alertIntent.putExtra("id", 1);
and in onReceive change to:
createNotification(context, intent.getStringExtra("title"), "event", " event",intent.getIntExtra("id", 0));
and the method
public void createNotification(Context context, String msg, String msgText, String msgAlert,int id) {
//your code and change this line bellow
mNotificationManager.notify(id,mBuilder.build());
}
so it will look like this:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent alertIntent = new Intent(this, MyReceiver.class);
alertIntent.putExtra("title", "event1");
alertIntent.putExtra("id", 1);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
Intent alertIntent2 = new Intent(this, MyReceiver.class);
alertIntent2.putExtra("title", "event2");
alertIntent2.putExtra("id", 2);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), PendingIntent.getBroadcast(this, 2, alertIntent2, PendingIntent.FLAG_UPDATE_CURRENT));
Intent alertIntent3 = new Intent(this, MyReceiver.class);
alertIntent3.putExtra("title", "event3");
alertIntent3.putExtra("id", 3);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal3.getTimeInMillis(), PendingIntent.getBroadcast(this, 3, alertIntent3, PendingIntent.FLAG_UPDATE_CURRENT));
Intent alertIntent4 = new Intent(this, MyReceiver.class);
alertIntent4.putExtra("title", "event4");
alertIntent4.putExtra("id", 4);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal4.getTimeInMillis(), PendingIntent.getBroadcast(this, 4, alertIntent4, PendingIntent.FLAG_UPDATE_CURRENT));
Related
I am using Alarm Manager to schedule different jobs to be executed at different hours of the day. When i am scheduling only one job it is working expectedly, but when i schedule multiple jobs, only the last scheduled gets executed. Below are my codes.
private void triggerAlarm(int hr, int min, String sid, String cid) {
Intent intentToFire = new Intent(getApplicationContext(), AlarmBroadcastReceiver.class);
intentToFire.putExtra("cid", cid);
intentToFire.putExtra("sid", sid);
intentToFire.setAction(ACTION_ALARM);
PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(),
0, intentToFire, 0);
AlarmManager alarmManager = (AlarmManager) getApplicationContext().
getSystemService(Context.ALARM_SERVICE);
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, hr);
c.set(Calendar.MINUTE, min);
c.set(Calendar.SECOND, 0);
long afterTwoMinutes = c.getTimeInMillis();
alarmManager.set(AlarmManager.RTC_WAKEUP, afterTwoMinutes, alarmIntent);
//sendBroadcast(intentToFire);
}
You can use id for your AlarmReceiver I Also Using this and hope it's also works for you
in targetCal Param just pass your calander obje where you selecting time for alarm
like this
private void setAlarm(Calendar targetCal) {
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
final int _id = (int) System.currentTimeMillis();
PendingIntent appIntent = PendingIntent.getBroadcast(this, _id, intent,PendingIntent.FLAG_ONE_SHOT);
// PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),
appIntent);
}
Hope it works for you.
Basically, I want to create two alarms:
Fire daily at 6 PM
Fire monthly at a specific date at 4 PM.
But issues are after executing monthly alarm first one is also executing at 4 PM.
Following way I am creating alarm:
Calendar calendar = Calendar.getInstance();
Calendar calendar1 = Calendar.getInstance();
// For 1st alarm
calendar.set(Calendar.HOUR_OF_DAY, 18);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
// For 2nd alarm
calendar1.set(2017,8,17,16,00,00);
Bundle bundle = new Bundle();
Intent intent = new Intent(context, AlarmReceiver.class);
bundle.putInt("NotificationId1", 1);
bundle.putInt("NotificationId2", 2);
intent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 2,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
alarm.set(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), pendingIntent1);
And after firing monthly alarm I am again defining alarm with the further date.
Issue is after firing monthly alarm first alarm is executing at 4 pm. How to resolve the issue?
Any kind of help will be appreciated.
your first alarm is repeating so create alarm like these to set alarm.
if(Build.VERSION.SDK_INT < 23){
if(Build.VERSION.SDK_INT >= 19){
setExact(...);
}
else{
set(...);
}
}
else{
setExactAndAllowWhileIdle(...);
}
now in your broadcast receiver set next alarm to fire for next day at 4pm
Try this code, I hope you will get your answer.
Bundle bundle1 = new Bundle();
Intent intent1 = new Intent(context, AlarmReceiver.class);
bundle1.putInt("NotificationId1", 1);
intent1.putExtras(bundle1);
Bundle bundle2 = new Bundle();
Intent intent2 = new Intent(context, AlarmReceiver.class);
bundle2.putInt("NotificationId2", 2);
intent2.putExtras(bundle2);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1,
intent2, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 2,
intent2, PendingIntent.FLAG_UPDATE_CURRENT);
Make your both the alarms separate from each other. For this you need 2 intents, 2 pending intent and 2 alarm managers. You can take a reference from - this link
your code should be like-
// define variables
private AlarmManager alarmMgr1;
private AlarmManager alarmMgr2;
private PendingIntent pendingIntent1;
private PendingIntent pendingIntent2;
private Calendar timeFirst;
private Calendar timeSecond;
private Context context;
// init variables
timeFirst = Calendar.getInstance();
timeSecond = Calendar.getInstance();
timeFirst.set(Calendar.HOUR_OF_DAY, 18);
timeFirst.set(Calendar.MINUTE, 0);
timeFirst.set(Calendar.SECOND,0);
timeFirst.set(Calendar.MILLISECOND,0);
timeSecond.set(2017,8,17,16,00,00);
// set values
alarmMgr1 = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmMgr2 = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent myIntent1 = new Intent(context, AlarmReceiver.class);
pendingIntent1 = PendingIntent.getBroadcast(context, (int) System.currentTimeMillis(), myIntent1, PendingIntent.FLAG_UPDATE_CURRENT);
Intent myIntent2 = new Intent(context, AlarmReceiver.class);
pendingIntent2 = PendingIntent.getBroadcast(context, (int) System.currentTimeMillis(), myIntent2, PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr1.setRepeating(AlarmManager.RTC_WAKEUP, timeFirst.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent1);
if (Build.VERSION.SDK_INT >= 23){
alarmMgr2.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeSecond.getTimeInMillis(), pendingIntent2);
}
else if (Build.VERSION.SDK_INT >= 19){
alarmMgr2.setExact(AlarmManager.RTC_WAKEUP, timeSecond.getTimeInMillis(), pendingIntent2);
}
else{
alarmMgr2.set(AlarmManager.RTC_WAKEUP, timeSecond.getTimeInMillis(), pendingIntent2);
}
}
hope it will be helpful and give you a better idea.
You need 2 intent.
Because you are sharing the same intent, any alarm running will also call the same intent.
I hope this helps.
I'm having an issue with my AlarmManger:
The following correctly adds one day (because the time 9:25 has passed) the output is correct yet the AlarmManager calls the Broadcast within a couple seconds. Here's the relevant snippit. Any ideas?
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 21);
cal.set(Calendar.MINUTE, 25);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Intent intent = new Intent(this, NotificationReceiver.class);
NotificationReceiver instance = new NotificationReceiver();
intent.setAction(instance.NOTIFICATION_SEND);
intent.putExtra("TYPE",instance.NOTIFICATION_TYPE_PROGRESS);
intent.putExtra("Ticker","Waiting for arrival.");
intent.putExtra("ID",1);
intent.putExtra("title","Check-In");
intent.putExtra("message","A message");
if(System.currentTimeMillis()>cal.getTimeInMillis()){
if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY||cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY||cal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY){
cal.add(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
Log.d("ALARMS","Set for monday.");
}
else{
cal.add(Calendar.DATE,1);
Log.d("ALARMS","Set for tomorrow. "+cal.get(Calendar.DAY_OF_MONTH));
}
}
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 24332, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),4000,pendingIntent);
Resolved:
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 24, intent, PendingIntent.FLAG_UPDATE_CURRENT);
I'd like to show a dialog at specific date and time (25/12/2012 at 12.00) and I am using this code. I set like 11 month (because 0 is gen) but the alarm does not start. What is my mistake?
Calendar cal=Calendar.getInstance();
cal.set(Calendar.MONTH,11);
cal.set(Calendar.YEAR,2012);
cal.set(Calendar.DAY_OF_MONTH,25);
cal.set(Calendar.HOUR_OF_DAY,12);
cal.set(Calendar.MINUTE,00);
cal.set(Calendar.SECOND,0);
Intent _myIntent = new Intent(context, Notify.class);
PendingIntent _myPendingIntent = PendingIntent.getBroadcast(context, 123, _myIntent, PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), _myPendingIntent);
Look at below code about how i am going to set the alarm for the December month:
// for Alarm 25/12/2012 at 12.00
Calendar myAlarmDate = Calendar.getInstance();
myAlarmDate.setTimeInMillis(System.currentTimeMillis());
myAlarmDate.set(2012, 11, 25, 12, 00, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent _myIntent = new Intent(context, AlarmReceiverNotificationForEveryMonth.class);
_myIntent.putExtra("MyMessage","HERE I AM PASSING THEPERTICULAR MESSAGE WHICH SHOULD BE SHOW ON RECEIVER OF ALARM");
PendingIntent _myPendingIntent = PendingIntent.getBroadcast(context, 123, _myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, myAlarmDate.getTimeInMillis(),_myPendingIntent);
You can update the above code with your intent and class and you will get your desire output.
Hope this help you.
I want to set the alram for phone through program coding. I have an button where by clicking DatePicker come & also time picker. I want what time i set here that time should be set in phones alaram. How can I do that . plz give me the answer . Thank you
Please try this....
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, 0);
cal.set(Calendar.HOUR_OF_DAY, 9);
cal.set(Calendar.MINUTE, 01);
cal.set(Calendar.SECOND, 0);
Intent intent = new Intent(Alarm.this, Alarm1.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 1234567, intent, 0);
PendingIntent sende2 = PendingIntent.getBroadcast(this, 123123, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sende2);
Intent intent1 = new Intent(Alarm.this, Alarm1.class);
PendingIntent sender1 = PendingIntent.getBroadcast(this, 1234567, intent1, 0);
AlarmManager am1 = (AlarmManager) getSystemService(ALARM_SERVICE);
am1.cancel(sender1);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Alarm a = new Alarm ();
a.setAlarm();
b1.setText(prod);
}
});