Let's say a notification using AlarmManager
was set at a specific date. What if I want to delete that notification with a button press before it has even appeared. The NotificationManager.cancel(id)
method if I'm not mistaken will cancel a notification that is currently showing. What if I want to delete it before it has even appeared? Like how you remove an item in an arraylist or how you delete a row in a database.
Example I have a Person
object with id,name
and everytime I add a Person
to the database, a Notification
will be set on a specified date.
Here is the set notification method to be called with a calendar instance of a specified date and a person object:
public void setNotification(Calendar calendar,Person person){
AlarmManager alertManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent intent = new Intent();
intent.setAction("android.media.action.DISPLAY_NOTIFICATION");
intent.addCategory("android.intent.category.DEFAULT");
intent.putExtra("myTag",person);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, person.getId(), intent, PendingIntent.FLAG_ONE_SHOT);
alertManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
My alarm receiver class (Yes I'm implementing DAO design pattern. I have a PersonDao and PersonDaoImpl with the latter being a singleton class):
public class AlarmReceiver extends BroadcastReceiver {
final static String GROUP_KEY = "GROUP_KEY";
@Override
public void onReceive(Context context, Intent intent) {
//retrieve info:
PersonDAO personDao = PersonDAOImpl.getInstance(context);
Person person = (Person)intent.getSerializableExtra("myTag");
String name = person.getName();
long[] vibrationPattern = {0, 300, 0, 300};
Intent notificationIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(ViewDetails.class);
stackBuilder.addNextIntent(notificationIntent);
int requestCode = person.getId();
PendingIntent pendingIntent = stackBuilder.getPendingIntent(requestCode, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification = builder.setContentTitle("Deadline due today")
.setContentText("name: " + name)
.setTicker(name+"'s debt is due today")
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(Notification.PRIORITY_MAX)
.setContentIntent(pi)
.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(requestCode, notification);
}
Example the date set on the alarm reciever was September 12 2016 3:00pm. The current date is Septermber 10 2016. What I want to do is when I delete the person in the database, the notification will be deleted/canceled also therefore on September 12 2016 3:00pm there will no longer be an alarm notification.
If you want to cancel something that you scheduled via AlarmManager
, call cancel()
on AlarmManager
, passing in an equivalent PendingIntent
to the one that you used to schedule the work in the first place. Here, by "equivalent PendingIntent
", I mean:
getActivity()
vs. getService()
vs. getBroadcast()
) Intent
By "equivalent Intent
", I mean:
Uri
) for whatever of those properties you had set on the original Intent
for the original PendingIntent
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.