1

I am facing this problem for a long time now, I have an application with many functions, one of them is the alarm

My notification just stays there and never disappears although I am calling cancelAll() / cancel() from the notification manager, also I have autoCancel set to true! It also stays clickable, I just need it to disappear after actions are triggered!

(I am testing this on an emulator with Android Studio, not sure if this could be the problem)

I searched a lot an tried so many things and nothing worked so I would appreciate the help :)

I have set my notification as follow:

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);
}

When the actions buttons are pressed this is the part of code triggered:

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

Pierre Ghaly
  • 667
  • 5
  • 17

1 Answers1

4

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

Pierre Ghaly
  • 667
  • 5
  • 17