HTML5中的Web Notification桌面通知

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/ 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值