Notifications don't seem to work after being forwarded to a view from VaadinServiceInitListener

Specifically I have:

private void beforeEnter(BeforeEnterEvent beforeEnterEvent) {
    beforeEnterEvent.forwardTo("myView");
}

However when it’s forwarded it doesn’t matter where I locate the notication.show(“test”) it doesn’t appear. I’ve tried in the constructor of MyView as well as several events such as beforeLeave, beforeEnter afterNavigation, and so on. Nothing seems to fire the notification. It works if you refresh the page, but it will fail if you are forwarded to it .The same seems to be true for dialog.

I highly doubt that VaadinServiceInitListener was designed to redirect users and therefore the above cases with Notification and Dialog won’t work because they require a UI available in the current “scope” to be active (see the UI::getCurrent calls in the code)

The UI is actually available at some point. But that being said what is the best option to have some kind of notification? I ask because it’s specifically for when a customer is behind on their payment and we really want their attention.

I think it was in afterNavigation (I forget now). I any case even if not is there a way to show a some kind of popup notification on a forward.

Showing a notification or dialog within afterNavigation works normally… I’m personally not sure why forwardTo would interfere with it in your case… what is your exact usage at the moment? Like calling your DB in the init listener and afterwards redirecting the user depending on a state to specific views to show them the information?

It’s a test project with virtually no code.

@PageTitle(“Billing”)
@Route(value = “billing”)
public class BillingView extends VerticalLayout {
public BillingView() {
Notification notification = Notification.show(“Billing”);
notification.addThemeVariants(NotificationVariant.LUMO_ERROR);
notification.setPosition(Notification.Position.MIDDLE);

    new Dialog("Test", new Span("here")).open();
}

}

public class AuthenticationControlService implements VaadinServiceInitListener {

@Override
public void serviceInit(ServiceInitEvent serviceInitEvent) {
    serviceInitEvent.getSource().addUIInitListener(uiEvent -> {
        final UI ui = uiEvent.getUI();
        ui.addBeforeEnterListener(this::beforeEnter);
    });
}

private void beforeEnter(BeforeEnterEvent beforeEnterEvent) {
    System.out.println("Forward");
    beforeEnterEvent.forwardTo("billing");
}

https://github.com/vaadin/flow/issues/14570#issuecomment-1276192956 found the issue + workaround below

If I’m reading it correctly it looks like the notification is added in a push/thread. Interesting.

Another workaround could be to move your notification logic to the billing view and e.g. add a query param to the UI “forwardTo(“billing?notify”)”

The ui.access solution seems to work consistently

Interesting

Thank you for that solution, it’s very helpful. I prefer the ui.access to a parameter because it works in all scenarios.

I’m adding it to the AfterNavigationObserver

Thank you again for the help. It’s very much appreciated.