1.controller
/** * 互助小修订单处理web端通知 * * @param response */ @RequestMapping("messageOrderHZXX") public void messageOrderHZXX(HttpServletResponse response) { ReturnInfo info = new ReturnInfo(); Map<String, Object> map = new HashMap<>(); String sql = "select count(*) from t_repairs_account_order where state = 0 "; Long ncRepairsOrderCount0 = ncRepairsAccountOrderService.countBySql(sql, map); sql = "select count(*) from t_repairs_account_order where state = 2 "; Long ncRepairsOrderCount2 = ncRepairsAccountOrderService.countBySql(sql, map); sql = "select count(*) from t_repairs_account_order where state = 4 "; Long ncRepairsOrderCount4 = ncRepairsAccountOrderService.countBySql(sql, map); StringBuilder str = new StringBuilder(); if (ncRepairsOrderCount0 > 0) { str.append(ncRepairsOrderCount0 + "个互助小修-平台审核派单中-订单 "); } if (ncRepairsOrderCount2 > 0) { str.append(ncRepairsOrderCount2 + "个互助小修-平台审核定损金额中-订单 "); } if (ncRepairsOrderCount4 > 0) { str.append(ncRepairsOrderCount4 + "个互助小修-平台设置公示-订单 "); } Long s = ncRepairsOrderCount0 + ncRepairsOrderCount2 +ncRepairsOrderCount4; if (s > 0) { info.setMsg(str.toString()); info.setResult(true); } else { info.setResult(false); info.setObj(str.toString()); } this.writeJson(response, info); }
2.jsp
在后台登录或者首页方法中调取 setTimeout(messageOrderHZXX,100);function messageOrderHZXX() { $.ajax({ //几个参数需要注意一下 type: "GET", dataType: "json", url: "messageOrderHZXX.html", data: {}, success: function (data) { if (window.Notification) { if(data.result){ var popNotice = function () { if (Notification.permission == "granted") { var notification = new Notification("您有新的订单了,请及时处理~~!:", { body: data.msg, icon: '<%=basePath%>upload/ncRepairs/chehoulian.jpg' }); notification.onclick = function() { event.preventDefault(); // prevent the browser from focusing the Notification's tab window.open('<%=basePath%>ncRepairs/toNcRepairsAccountOrderList.html'); } } }; if (Notification.permission == "granted") { popNotice(); } else if (Notification.permission != "denied") { Notification.requestPermission(function (permission) { popNotice(); }); } //设置一分钟长轮询一次 setTimeout(messageOrderHZXX,60000); } } else { alert('浏览器不支持Notification'); } } }); }
https://www.zhangxinxu.com/wordpress/2016/07/know-html5-web-notification/