You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vaadin 14中Idle Notification空闲通知不显示问题

解决方法

空闲通知无法弹出是配置和代码写法存在错误导致的,按以下步骤修正即可:

  • 首先修正application.properties中的错误配置。你当前填写的参数类型、取值完全不符合规范,不会生效,替换为以下正确配置:
# 禁用Vaadin内置的空闲会话回收逻辑,交给插件处理
vaadin.closeIdleSessions=false
# 心跳间隔设置为60秒,需小于你配置的90秒空闲判定阈值,避免服务端提前销毁会话
vaadin.heartbeatInterval=60
# 开启自动Push模式,支持服务端主动推送通知到前端
vaadin.pushMode=automatic
  • 修正Push注解配置。你类上标注的@Push没有指定运行模式,Vaadin中该注解默认模式为关闭状态,等于没有开启推送能力,需要修改为@Push(PushMode.AUTOMATIC)。如果已经在上述配置文件中开启了自动Push,这步可以跳过,二选一配置即可。
  • 调整组件初始化时机。不要在MainView构造函数中调用UI.getCurrent()挂载组件,构造函数执行时UI实例还未完成初始化,要么获取不到实例,要么无法和当前用户会话正确绑定。你已经实现了AfterNavigationObserver接口,直接将IdleNotification的初始化逻辑移到接口实现方法中,同时增加重复判断,避免每次导航都生成新的组件实例:
@Override
public void afterNavigation(AfterNavigationEvent event) {
    UI currentUI = event.getUI();
    // 避免重复添加通知组件
    if (currentUI.getChildren().noneMatch(comp -> comp instanceof IdleNotification)) {
        IdleNotification idleNotification = new IdleNotification();
        idleNotification.setSecondsBeforeNotification(90);
        idleNotification.setMessage("Your session will expire in " +
                IdleNotification.MessageFormatting.SECS_TO_TIMEOUT
                + " seconds.");
        idleNotification.addExtendSessionButton("Extend session");
        idleNotification.addRedirectButton("Logout now", "logout");
        idleNotification.addCloseButton();
        idleNotification.setExtendSessionOnOutsideClick(false);
        currentUI.add(idleNotification);
    }
}
  • 额外排查点:如果完成以上修改后通知仍不触发,检查两个常见问题:一是引入的Idle Notification插件版本和当前项目使用的Vaadin主版本是否兼容,版本不匹配会导致前端组件无法正常注册,不会触发空闲监听;二是页面是否存在iframe、拦截鼠标/键盘事件冒泡的自定义组件,插件捕获不到用户操作就会出现状态判定错误,需要保证用户操作事件能正常冒泡到页面根节点。

内容的提问来源于stack exchange,提问作者user11149927

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 12:51:20