取消所有()和取消()不要取消我的通知

时间:2018-05-03 20:27:15

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

我现在面对这个问题很久了,我有一个有很多功能的应用程序,其中一个是闹钟

我的通知只是停留在那里并且永远不会消失虽然我从通知管理器呼叫cancelAll() / cancel(),但我autoCancel设置为true < / STRONG>!它也保持可点击状态,我只是需要它才能在触发动作后消失!

(我在使用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

1 个答案:

答案 0 :(得分:1)

这里的问题不在通知设置本身,问题是通知是在Service类中运行的。虽然该服务仍在运行,但通知无法解除。

我是如何解决这个问题的: 在服务类中,在执行操作(我在哪里呼叫cancelAll()之后,我会调用stopSelf()。 然后覆盖onDestroy()并在其中调用cancelAll()

干杯