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

标签 android android-studio android-notifications notificationmanager notification-channel

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

我的通知只是停留在那里,永远不会消失尽管我正在打电话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,但仍然不起作用

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

Screenshot of the notification

最佳答案

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

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

干杯

关于android - cancelAll() 和 cancel() 不会忽略我的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50163353/

相关文章:

android - NotificationListenerService 和 Doze 模式以及 App Standby

intellij-idea - 当我按下“立即同步”和“刷新”按钮时,IDEA/Android Studio 会运行​​哪些 Gradle 任务?

android - 谷歌地图意外重置相机缩小

java - 如何创建像 android 通知这样的内裤

Android 应用程序在 textViews 中显示经纬度

android-studio - 将文件移动到Android Studio中的另一个程序包后,如何防止自动格式化?

android - Android 上的 FCM 通知可以覆盖之前的通知吗?

android - 使用 ioctl 读取 USB 失败,原因为 "value too large for defined data type"

android - 声明在多个 Activity 中使用的常量的最佳实践

android - "Error:Failed to complete Gradle execution. Cause: Gradle build daemon disappeared unexpectedly (it may have been killed or may have crashed)"