pending intent alarm manager can't called twice - java

I am writing code to call the push notifications twice. But it only works once.
As you can see I am calling it at different times. What should I do to allow both notifications to work?
Intent intent = new Intent(MainActivity.this,ReminderBroadcast.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0,intent,0);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(MainActivity.this,0,intent,0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
AlarmManager alarmManager2 = (AlarmManager) getSystemService(ALARM_SERVICE);
long timeAtButtonClick = System.currentTimeMillis();
long tenSecondsInMillis = 1000 * 10;
long nextInMillis = 1000 * 20;
alarmManager.set(AlarmManager.RTC_WAKEUP,timeAtButtonClick + tenSecondsInMillis, pendingIntent);
alarmManager2.set(AlarmManager.RTC_WAKEUP,timeAtButtonClick + nextInMillis, pendingIntent2);

You should declare different PendingIntent requestCode try this
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(MainActivity.this,1,intent,0);

Related

adding a alarm cancel button in the notification

I have been working on setting alarms using AlarmManager.
Set:
Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), reqCode, myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, now.getTimeInMillis(), pendingIntent);
cancel:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), reqCode, myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pendingIntent);
in my alarm receiver I want to be able to receive a notification which has a button that allows me to call cancelAlarm(). however I don't see the best approach to this and looking for the best and most reliable way of doing this.
I get the requestCodes so I am able tell which alarm to cancel

Android AlarmManager not cancelling alarms correctly

I'm working on an app which will allow users to set multiple notifications, and cancel any notification they choose.
The problem I have is that when I cancel a pending intent using the AlarmManager it is cancelling all alarms. Each of my pending intents has it's own unique request code.
For instance I would call this to create the alarm and then delete:
setAlarm(5062, 1453269670) // Set alarm 1
setAlarm(5063, 1453774418) // Set alarm 2
cancelAlarm(5062) // Cancel alarm 1
cancelAlarm(5063) // Cancel alarm 2
Setting mutiple alarms always works, I can set as many as I like and they all produce the notification. However if I was to cancel alarm 1, it also cancels alarm 2.
I know that the pending intent has to be the same when setting and cancelling, and each pending intent has it's own unique request code, so I don't know why it's not working. I've spent many hours googling, but none of the suggested answers have helped me.
void setAlarm(int request_code, long alarm_time) {
Intent intent = new Intent(context, NotificationReceiver.class);
intent.putExtra("request_code", request_code);
PendingIntent pendingIntent = PendingIntent.getService(context, request_code, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarm_time, pendingIntent);
}
void cancelAlarm(int request_code) {
Intent intent = new Intent(context, NotificationReceiver.class);
intent.putExtra("request_code", request_code);
PendingIntent pendingIntent = PendingIntent.getService(context, request_code, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
pendingIntent.cancel();
alarmManager.cancel(pendingIntent);
}
The answer is simple
Intent firstIntent = new Intent(this, Receiver.class);
intent.setAction("action 1");
intent.putExtra("extra", "extra1");
firstPendingIntent = PendingIntent.getBroadcast(this, 0, firstIntent, 0);
Intent secondIntent = new Intent(this, Receiver.class);
intent.setAction("action 2");
intent.putExtra("extra", "extra2");
secondPendingIntent = PendingIntent.getBroadcast(this, 0, secondIntent, 0);
if you compare two PendingIntents created in ABOVE code they are NOT EQUAL
But in code BELOW:
Intent firstIntent = new Intent(this, Receiver.class);
intent.setAction("action 1");
intent.putExtra("extra", "extra1");
firstPendingIntent = PendingIntent.getBroadcast(this, 0, firstIntent, 0);
Intent secondIntent = new Intent(this, Receiver.class);
intent.setAction("action 1");
intent.putExtra("extra", "extra2");
secondPendingIntent = PendingIntent.getBroadcast(this, 0, secondIntent, 0);
if you compare two PendingIntents they gonna be EQUAL, so doesn't matter what you pass in putExtra method

How to repeat alarm any given day/time?

Using Broadcast receiver, how can i make the alarmmanager repeat evrey given day/days/time ?
public void startAlert(String time, String title) {
long timeInMillis = Long.decode(time);
intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("myTitle", title);
alarmmanager = (AlarmManager) getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getBroadcast(this, (int) Long.parseLong(time), intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmmanager.set(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);
}
Use setRepeating instead of set.
Here is the proper syntax -
alarmmanager.setRepeating(AlarmManager.RTC_WAKEUP,
startUpTime, AlarmManager.INTERVAL_DAY, pendingIntent);
I think you can use setRepeating with customized Date like this another question.
alarmmanager.setRepeating(AlarmManager.RTC_WAKEUP,
startUpTime, AlarmManager.INTERVAL_DAY, pendingIntent);

Save variable with alarm manager event

I'm building a study planner application for Android, i have the AlarmManager set up and working to trigger a notification when the time elapses, which works fine.
When setting the alarm, the alarm details are saved into an SQLite database, when the alarm is set off, i need to recall the alarm information, is there some form of variable i can save into the database and link to the specific alarm so that i can use it in an SQLite query to find the correct entry.
UPDATE Code below:
Long nIdLong = System.currentTimeMillis();
String nId = nIdLong.toString();
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
alarmIntent.putExtra("nID", nId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent , 0);
AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(getActivity().ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
AlarmReciever:
public void onReceive(Context context, Intent intent)
{
long dbId = intent.getLongExtra("nID", 0);
String notificationId = Long.toString(dbId);
Log.i("App", "called receiver method");
try{
Toast.makeText(context, notificationId, Toast.LENGTH_LONG).show();
}catch(Exception e){
e.printStackTrace();
}
}
Thanks in advance
You're sending a String through the intent, it needs a Long, this code should work.
Long nIdLong = System.currentTimeMillis();
String nId = nIdLong.toString();
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
alarmIntent.putExtra("nID", nIdLong);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent , 0);
AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(getActivity().ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
While setting the alarm, first retrieve the id which will be created while inserting alarm details to SQLite via
long uniqueId = db.insert(your query);
And put that in the intent for PendindIntent for AlarmManager
Intent alarmIntent = new Intent(this, AlarmEventReceiver.class);
alarmIntent.putExtra("dbId", uniqueId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent , 0);
Inside the onReceive() of AlarmEventReceiver, you can get the id & retrieve the data from DB.
#Override
public void onReceive(Context context, Intent intent) {
long dbId = intent.getLongExtra("dbId", 0);
}

Cancel alarm with different context

I've a alarm created in an OnBootReceiver like this:
public class OnBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 10);
Intent i = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), savedIntervalAutomaticMilisInt, pendingIntent);
}
}
But to cancel it, I use this code in an Activity:
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
scManager.clearErrors();
So to set it up I use the context from the BroadcastReceiver's Context parameter, but to cancel it I use the this context from the Activity. My question: Will the alarm still be cancelled, even though the context is slightly different?
Will the alarm still be cancelled, even though the context is slightly different?
Context does not matter in this case -- it is merely a way to get to an AlarmManager.
Your choice of PendingIntent.FLAG_CANCEL_CURRENT in your cancel-the-alarm logic may cause a problem. If you run into difficulties (e.g., adb shell dumpsys alarm indicates your alarm survived the cancel), try replacing PendingIntent.FLAG_CANCEL_CURRENT with 0.
Give this a try for a global (application-wide) context object: android.content.ContextWrapper.getApplicationContext()

Categories

Resources