2

我现在面临这个问题很长时间了,我有一个功能很多的应用程序,其中一个是警报

尽管我正在从通知管理器呼叫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);
}

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

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:
        }
    }

I also tried using cancel() using the alarmRequestCode which is the unique ID used for both alarm and notification and still didn't work

Everything is working fine, the actions perform as desired only that the notification stays right there and stays clickable and performing actions as shown in the screenshot below

Screenshot of the notification

4

1 回答 1

5

The problem here is not in the notification settings itself, the problem is that the notification is running in a Service class. While the service is still running the notification cannot be dismissed.

How I solved this: In the service class, after the action is performed (Where I was calling cancelAll()) I call stopSelf()instead. Then override onDestroy() and call cancelAll() in it instead!

Cheers

于 2018-05-05T14:02:12.963 回答