1

My app has a Notification that holds a Button.

When the user clicks the Button, the app should open (if it was closed) and show a custom dialog, which I've created by a bunch of views.

Showing the dialog means setting its Visibility to View.VISIBLE.

I use a BroadcastReceiver to receive the Notifications Button click, and it works (I've used Toast messages to check it, and it does - the Receiver is getting called when the Button is clicked, and it reaches to the function that changes the dialogs Visibility to View.VISIBLE)

When I click the Button, the app does open, but the dialog won't show.

Also, I've noticed that when I click the Button, the Notification panel does not hide (unlike when clicking the Notification itself), and I thought maybe it has something to do with it.

So I found this answer, but it didn't work for me (below there's another answer, much newer, but I didn't understand what he did there or how can I do it).

I also checked using Toast messages whether the Visibility parameter of the dialog is changing or not, and it is changing to View.VISIBLE (0), and still, the dialog is not shown.
If I'm in the app and trying to show the dialog, it works, the dialog shows on an in-app button click, the issue occurs when trying to show the dialog as soon as opening the app using the Notifications Button click

Is the Notification Panel the problem?
If it is, why is it happening and how can I hide the notification panel?
If it's not, what is it, and how can I solve this?

Edit

I was asked for the code that inits and shows the notification, so here it is:

@RequiresApi(api = Build.VERSION_CODES.M)
private void updateNotification() {
    isBatteryOptimizationActive = App.isBatteryOptimizationActive();

    clearAllTimerNotificationActionButtons();

    if (isBatteryOptimizationActive) {
        initBatteryOptimizationActiveNotification();
    } else {
        if (notificationJustStarted) {
            initNotificationBase();
        }
        initAndStartPomodoroTimerNotification();
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        NotificationManager manager = getSystemService(NotificationManager.class);
        manager.notify(TIMER_NOTIFICATION_ID, pomodoroNotificationBuilder.build());
    }

}
private void initNotificationBase() {
    pomodoroNotificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,
            0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);

    pomodoroNotificationBuilder
            .setContentTitle(getString(R.string.timer_notification_background_allowed_title))
            .setOngoing(true)
            .setAutoCancel(false)
            .setSmallIcon(R.drawable.tomato)
            .setContentIntent(pendingIntent)
            .setNotificationSilent()
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);
    notificationJustStarted = false;
}

@RequiresApi(api = Build.VERSION_CODES.M)
private void initAndStartTimerNotification() {
    String enableScreenOverlayText = "",
            actionButtonTitle;
    PendingIntent pendingIntent;
    if (!Settings.canDrawOverlays(this)) {
        /** Initialize the notification in case "draw over other apps" setting is disabled */
        Intent notificationIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
        pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
        actionButtonTitle = "Enable";
        enableScreenOverlayText = String.format("To be able stopping the timer from the notification:\nClick Enable -> Search for %s -> Allow Display over other apps", getString(R.string.app_name));
    } else {
        /** Initialize the notification in case "draw over other apps" setting is enabled */
        Intent stopActionIntent = new Intent(this, PomodoroService.class);
        stopActionIntent.setAction(ACTION_STOP_SERVICE);
        stopActionIntent.putExtra(POMODORO_SERVICE_STOP_FOR_REAL_EXTRA_NAME, false);
        pendingIntent = PendingIntent.getService(this, 1, stopActionIntent, PendingIntent.FLAG_IMMUTABLE);
        actionButtonTitle = "Stop";
    }


    pomodoroNotificationBuilder
            .setContentText(String.format("%s is in progress\nThis session remains: %s until finished", goalName, PublicMethods.formatStopWatchTime(millisRemaining)))
            .setStyle(new NotificationCompat.BigTextStyle().bigText(enableScreenOverlayText))
            .addAction(R.drawable.stop, actionButtonTitle, pendingIntent);
}
9
  • Can you share the code of what you try to show the notification? Commented May 2, 2022 at 16:01
  • I don't understand... the code that initializes the notification and shows it? Commented May 2, 2022 at 16:01
  • Yes. That is it Commented May 2, 2022 at 16:02
  • just a moment, I'll do it. can you get into the chat I've opened? Commented May 2, 2022 at 16:10
  • Where is it? I could not find it Commented May 2, 2022 at 16:11

1 Answer 1

0

How can I hide the notification panel?

I see that you are receiving the action button click in the Receiver. So, then it becomes an easy task to dismiss it. In the receiver, add this line:

NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
manager.cancel(TIMER_NOTIFICATION_ID);

When I click the Button, the app does open, but the dialog won't show.

You need to pass an intent extra that you are wanting to show the dialog. Then, you can, in the onCreate() get that extra and then show the dialog accordingly.

10
  • I'm already using intent with this extra, and in the receiver, I'm calling the function that shows the dialog. Do you believe that I should somehow move it to the onCreate() method? Commented May 3, 2022 at 8:30
  • I don't correctly get you. How can you set the visibility of a dialogue to visible it can only be shown and hidden Commented May 3, 2022 at 9:08
  • what do you mean? as I explained in my question, the dialog is custom, meaning that it's just a bunch of views glued up to create the dialog... I change the visibility of the "parent view" of the dialog to toggle its visibility Commented May 3, 2022 at 9:20
  • and about the notification panel, I think you didn't understand me there as well. if I'm not wrong, I think the code you suggested is supposed to dismiss my app's specific notification, and I seek to hide the whole notification panel... Commented May 3, 2022 at 9:22
  • This conversation has been moved to chat Commented May 5, 2022 at 4:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.