如何取消通知?

时间:2016-02-09 11:11:36

标签: java android notifications broadcastreceiver android-notifications

我已为我的活动创建了通知。现在,如果用户删除了某些事件,我想取消onGoing通知。所以通知不应该发生。我用广播接收器创建了通知接收器。

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

我该怎么做?

这是NotificationReceiver:

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);

}

1 个答案:

答案 0 :(得分:1)

您可以取消重复闹铃以避免通知。设置新警报时。为它分配一个唯一的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);