I am trying to get a reminder/alarm Service` working in my note/todo app. I am able to set a reminder for a particular item and the alarm triggers and displays a notification successfully.
My problem is how can my Service know which note/todo item set that particular reminder. I'd like for the user to be able to click on the notificaiton in the status bar and have the item which trigger it come up. But I have no way of passing that information to the Service as they don't accept Bundles from the PendingIntent.
I currently set the alarm with the following:
private void createAlarm() {
Intent i = new Intent(this, AlarmService.class);
PendingIntent sender = PendingIntent.getService(this, 0, i, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, mReminderCal.getTimeInMillis(), sender);
}
I just need a way to send along the _id of the item in my database so my Service can launch the item with that same _id when the notification is clicked.
I hope my question isn't too confusing.
Thanks!
Why don't you put all your need into Intent data? Something like this:
final Intent intent = new Intent(context, UpdatesActivity.class);
intent.putExtra(ID, "foo");
final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
Then on receiving end you do
String id = getIntent().getStringExtra(ID);
Related
i have created a dietchart arrayList with specific timeperiod, now i want to create a reminder which will notify when it is time for a specific diet with sound.
Although this question is little bit old but it can still help to other users. You have to use alarm manager for triggering at specific time.
Intent intent = new Intent(this, MyService.class);
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP,<here set time when you want to trigger>,<here you can set time to repeat after trigering continusosly>,pintent);
the app i'm doing have to schedule an notifications if the user choose to
every time the user close the app the must must check and make the notification based on the user choice
the problem is if the user choose to have notification every time he close the app my code will put new notification for the same the thing so when the notification is fired there will be many notification instead of just one
i need to know if i can check if there is scheduled notification just like the one that i'm trying to make to avoid duplication and have one notification only
here is my code:
private void createScheduledNotification(long dateop,int days,String vaa,String naa)
{
Calendar calendars = Calendar.getInstance();
calendars.setTimeInMillis(dateop);
if(days<30){
calendars.add(Calendar.HOUR_OF_DAY, days *24);
}
else{
calendars.add(Calendar.MONTH, days/30);
}
AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(getBaseContext().ALARM_SERVICE);
int id = (int) System.currentTimeMillis();
Intent intent = new Intent(this, TimeReceiver.class);
intent.putExtra("username", naa);
intent.putExtra("vaccine", vaa);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendars.getTimeInMillis(), pendingIntent);
}
I have the problem removing the alarm which I already set. I used two activities to "ADD" or "EDIT" alarms. When I delete an alarm, I want to use the activity which is made to show list of alarms. However, It doesn't delete the PendingIntent I set.
Here's the part of my source code in List Activity.
private void removeAlarm(int number)
{
final int alarmCode = AlarmCodeCreator.CreateNum(1, clickedPosition, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(ListMainActivity.this,
PushMainActivity.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), alarmCode, intent, 0);
alarmManager.cancel(pendingIntent);
System.out.println(alarmCode);
}
You should use the PendingIntent.FLAG_UPDATE_CURRENT flag and make sure to create the PendingIntent the same way it was originally created.
Same answer here.
I am creating a repeating alarm. This works fine using the following code:
Intent intent = new Intent(this.ctx, AlarmReceiver.class);
intent.setAction("" + notificationId);
intent.putExtra(AlarmReceiver.TITLE, alarmTitle);
intent.putExtra(AlarmReceiver.SUBTITLE, alarmSubTitle);
intent.putExtra(AlarmReceiver.TICKER_TEXT, alarmTicker);
intent.putExtra(AlarmReceiver.NOTIFICATION_ID, notificationId);
PendingIntent sender = PendingIntent.getBroadcast(this.ctx, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = getAlarmManager();
am.setRepeating(AlarmManager.RTC_WAKEUP, triggerTime, 60000, sender);
Now I want to cancel an existing notification message (which pops up every minute for testing purposes), I invoke the following code, passing it an identical notificationId:
Intent intent = new Intent(this.ctx, AlarmReceiver.class);
intent.setAction("" + notificationId);
PendingIntent pi = PendingIntent.getService(this.ctx, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = getAlarmManager();
try {
am.cancel(pi);
} catch (Exception e) {
Log.e(PLUGIN_NAME, "AlarmManager update was not canceled. " + e.toString());
}
I see that pi != null, however the notification keeps coming and is not being cancelled. Both code snippets reside in the same class.
Basically I have two questions:
- How can I cancel a single alarm using its unique ID?
- How can I cancel all alarms, regardless of the ID?
UPDATE: I found that posting my issue here helps to get my mind straight. Changing the cancel code from:
PendingIntent sender = PendingIntent.getService( ... )
To:
PendingIntent sender = PendingIntent.getBroadcast( ... )
Helps to solve question 1. However the code to cancel all events, still does not work:
Intent intent = new Intent(this.ctx, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this.ctx, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = getAlarmManager();
am.cancel(pi);
I'm a noob here. But I think if you want to cancel the alarm, you have to use the exact same instance of PendingIntent. That means "their action, data, type, class, and categories are the same" as defined by filterEquals(Intent) . Therefore, you need to use the same requestCode here, not 0. Maybe you could use a list to store the Notification Ids, and iterate them to cancel all alarms.
I think , you should use same AlarmManager object to cancel the alram and also try to use same PendingIntent.
This is what sets the alarm
public void setSilent(Long taskId, Calendar when){
Intent i = new Intent(mContext, SilentReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(mContext, 1 , i, PendingIntent.FLAG_ONE_SHOT);
mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);
It takes in an id and date to use with the intent.
I am canceling it in another file when the user clicks delete. using
Intent i = new Intent(null, SilentReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(null, 1 , i, PendingIntent.FLAG_ONE_SHOT);
should this work because it has the same request code as the first one or am I doing something wrong?
You have to save a reference to your first PendingIntent. This way you can use:
mAlarmManager.cancel(pi);
And the alarm should be cancelled.
This answer has been repeated many times. Please search before asking.
Be careful to send a PendingIntent to stop the alarm with the same data than the Intent to create the alarm: problem with cancel the alarm manager pending intent