cancelAll() 和 cancel() 不要关闭我的通知

cancelAll() and cancel() don't dismiss my notification

这个问题困扰我很久了,我有一个应用程序有很多功能,其中之一就是闹钟

我的通知就在那里,永远不会消失尽管我正在从通知管理器调用 cancelAll() / cancel(),而且我已将 autoCancel 设置为 true!它还保持可点击状态,我只需要它在触发操作后消失!

(我正在使用 Android Studio 在模拟器上进行测试,不确定这是否是问题所在)

我搜索了很多东西,尝试了很多东西,但没有任何效果,所以非常感谢您的帮助:)

我的通知设置如下:

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

// ...

private void initNotification() {
    setNotificationChannel();

    notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), notificationChannel.getId());

    Intent click_intent = new Intent(getApplicationContext(), RingtonePlayingService.class)
            .putExtra("intent_action", "click")
            .putExtra("REQUEST_CODE", alarmRequestCode)
            .putExtra("ID", alarmId);
    PendingIntent click_pending = PendingIntent.getService(this, MainActivity.getRequestCode(), click_intent, PendingIntent.FLAG_UPDATE_CURRENT);

    Intent dismiss_intent = new Intent(getApplicationContext(), RingtonePlayingService.class)
            .putExtra("intent_action", "dismiss")
            .putExtra("REQUEST_CODE", alarmRequestCode)
            .putExtra("ID", alarmId);
    PendingIntent dismiss_pending = PendingIntent.getService(getApplicationContext(),MainActivity.getRequestCode(), dismiss_intent, PendingIntent.FLAG_UPDATE_CURRENT);

    Intent snooze_intent = new Intent(getApplicationContext(), RingtonePlayingService.class)
            .putExtra("intent_action", "snooze")
            .putExtra("REQUEST_CODE", alarmRequestCode)
            .putExtra("ID", alarmId);
    PendingIntent snooze_pending = PendingIntent.getService(getApplicationContext(),MainActivity.getRequestCode(), snooze_intent, PendingIntent.FLAG_UPDATE_CURRENT);

    dismissAction = new NotificationCompat.Action(R.drawable.small_delete,
            "Dismiss", dismiss_pending);
    snoozeAction = new NotificationCompat.Action(R.drawable.bell_ring,
            "Snooze", snooze_pending);

    notificationBuilder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
            .setSmallIcon(R.drawable.alarm)
            .setContentTitle("Alarm On!")
            .setContentText("Click the notification to dismiss")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(click_pending)
            .setDeleteIntent(click_pending)
            .addAction(dismissAction)
            .addAction(snoozeAction)
            .setAutoCancel(true);
}

private void showNotification() {
    notificationManager.notify(alarmRequestCode, notificationBuilder.build());
}

private void setNotificationChannel() {
    channelId = "alarm_channel_id";
    channelName = "alarm_notification_channel";
    int importance = NotificationManager.IMPORTANCE_DEFAULT;
    notificationChannel = new NotificationChannel(channelId, channelName, importance);
    notificationChannel.enableLights(true);
    notificationChannel.setLightColor(Color.RED);
    notificationManager.createNotificationChannel(notificationChannel);
}

当按下操作按钮时,这是触发的代码部分:

if (action != null) {
        switch (action) {
            case "click":
                alarmManager.cancel(cancelPendingIntent);
                notificationManager.cancelAll();
                player.stop();
                changeAlarmValuesToOff();
                break;
            case "snooze":
                alarmManager.cancel(cancelPendingIntent);
                notificationManager.cancelAll();
                player.stop();
                setNewSnoozeAlarm();
                break;
            case "dismiss":
                alarmManager.cancel(cancelPendingIntent);
                notificationManager.cancelAll();
                player.stop();
                changeAlarmValuesToOff();
                break;
            default:
        }
    }

我也尝试使用 cancel() 使用 alarmRequestCode 这是用于警报和通知的唯一 ID 但仍然没有用

一切正常,操作按预期执行,只是通知保留在那里并保持可点击并执行操作,如下面的屏幕截图所示

这里的问题不在通知设置本身,问题是通知在Serviceclass中运行。虽然服务仍然 运行,但无法关闭通知。

我是如何解决这个问题的: 在服务 class 中,执​​行操作后 (我调用 cancelAll() 的地方) 我改为调用 stopSelf()。 然后覆盖 onDestroy() 并在其中调用 cancelAll()

干杯