cancelAll() 和 cancel() 不会忽略我的通知

Pie*_*aly 2 android notificationmanager android-notifications android-studio notification-channel

我已经面临这个问题很长时间了,我有一个具有很多功能的应用程序,其中之一就是闹钟

尽管我正在从通知管理器中调用cancelAll()/cancel()autoCanceltrue ,但我的通知只是停留在那里并且永远不会消失,我也已设置为!它还保持可点击,我只需要它在触发操作后消失!

(我正在使用 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);
}
Run Code Online (Sandbox Code Playgroud)

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

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:
        }
    }
Run Code Online (Sandbox Code Playgroud)

我还尝试使用cancel()用于alarmRequestCode警报和通知的唯一 ID,但仍然不起作用

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

通知截图

Pie*_*aly 5

这里的问题不在于通知设置本身,问题在于通知在类中运行Service。当服务仍在运行时,无法取消通知。

我是如何解决这个问题的: 在服务类中,执行操作后(我调用的位置cancelAll()),stopSelf()改为调用。然后重写onDestroy()并调用cancelAll()它!

干杯