如何删除 android 中的通知

How to delete a notification in android

我创建了一个通知,以便在某个时间通知。现在我想在某个按钮点击或任何用户操作触发之前删除创建的通知。

下面是我调用来创建通知的方法handleNotification()

private void handleNotification() {

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, selYear);
    calendar.set(Calendar.MONTH, selMonthOfYear);
    calendar.set(Calendar.DAY_OF_MONTH, selDayOfMonth);
    calendar.set(Calendar.HOUR_OF_DAY, selHourOfDay);
    calendar.set(Calendar.MINUTE, selMinute);
    calendar.set(Calendar.SECOND, 00);

    Intent alarmIntent = new Intent(getActivity(), MyReceiver.class);
    alarmIntent.putExtra("when", calendar.getTimeInMillis());

    PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, alarmIntent, PendingIntent.FLAG_ONE_SHOT);

    AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 
}

这是我的接收器class MyReceiver.java

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent service1 = new Intent(context, MyService.class);
        service1.putExtra("when", intent.getIntExtra("when",0));
        context.startService(service1);     
    }
}

这是我的服务class NotificationService.java

public class MyService extends Service {

    public static Context context;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        NotificationService.context = this;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        int when = intent.getIntExtra("when", 0);

        Intent resultIntent = new Intent(context, MainActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(MainActivity.class);
        stackBuilder.addNextIntent(resultIntent);

        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("title")
        .setContentText("message")
        .setAutoCancel(true) 
        .setWhen(when)
        .setDefaults(Notification.DEFAULT_SOUND)
        .setContentIntent(resultPendingIntent);

        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(Integer.parseInt(id), mBuilder.build());

        return super.onStartCommand(intent, flags, startId);
    }

}

我尝试了下面的代码但没有成功

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(id);

我该如何实现。如果从服务启动通知时不可能,那么有没有其他方法可以启动通知,甚至在触发之前 delete/cancel 它。

(出于礼貌): 当您不想显示通知时,请禁用您的 alarmManager。 :-)