如何取消正在进行的通知



我已经为我的事件创建了通知。现在,如果用户删除了一些事件,我想取消正在进行的通知。因此,不应发生通知。我已经创建了带有广播接收器的通知接收器。

如果用户想要更改通知时间,我还想更新通知。

我该怎么做?

这是通知接收器:

public class NotificationReceiver  extends BroadcastReceiver {

public static int MY_NOTIFICATION_ID = 0;
NotificationManager notificationManager;
Notification myNotification;
EventTableHelper db;
@Override
public void onReceive(Context context, Intent intent) {

    Toast.makeText(context, "Time is set", Toast.LENGTH_LONG).show();
    db = new EventTableHelper(context);
    List<EventData> testSavings = db.getAllEvents();
    for (EventData ts : testSavings) {
        String log = "from date:" + ts.getFromDate()
                + " ,to date: " + ts.getToDate()
                + " ,location: " + ts.getLocation()
                + " ,title " + ts.getTitle();
        Calendar c = Calendar.getInstance();
        Date date = new Date();
        Date date1 = new Date();
        Log.d("Result: ", log);
        SimpleDateFormat df = new SimpleDateFormat("E MMM dd hh:mm:ss zzzz yyyy");
        SimpleDateFormat df2 = new SimpleDateFormat("hh:mm a");
        try {
            date = df.parse(ts.getFromDate());
            date1 = df.parse(ts.getToDate());
        } catch (ParseException ex) {
        }
        String timeFrom = df2.format(date);
     //   String startTime = String.valueOf(timeFrom);
        String timeTo = df2.format(date1);
       // String endTime = String.valueOf(timeTo);

        String location = ts.getLocation();
        String title = ts.getTitle();

        Intent myIntent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                context,
                0,
                myIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        if(location.equals(""))
        {
            String msg = "From : " + timeFrom + "nTo : " + timeTo;
            myNotification = new NotificationCompat.Builder(context)
                    .setContentTitle("Event : " + title)
                    .setContentText(msg)
                    .setWhen(System.currentTimeMillis())
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true)
                    .setSmallIcon(R.drawable.eventicon)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                    .setDefaults(Notification.DEFAULT_SOUND)
                    .build();
        }
        else
        {
            String msg = "From : " + timeFrom + "nTo : " + timeTo + "nAt : " + location;
            myNotification = new NotificationCompat.Builder(context)
                    .setContentTitle("Event : " + title)
                    .setContentText(msg)
                    .setWhen(System.currentTimeMillis())
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true)
                    .setSmallIcon(R.drawable.eventicon)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                    .setDefaults(Notification.DEFAULT_SOUND)
                    .build();
        }
        Log.i("Notify", "Notification");
        notificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
        myNotification.flags=Notification.FLAG_AUTO_CANCEL;
        MY_NOTIFICATION_ID ++;
    }
}

}

使用警报管理器创建通知。

 public void notificationOnDay(Calendar c)
{
    Intent intent = new Intent(getBaseContext(),NotificationReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY *7, pendingIntent);
}

您可以取消重复闹钟以避免通知。当您设置新闹钟时。为其分配一个唯一的 ID,以便当您想要取消警报时,您可以像这样取消它

AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReciever.class);
PendingIntent sender = PendingIntent.getBroadcast(this, alarm_id_to_cancel, intent, 0);
am.cancel(sender);