self.addEventListener("push", function (event) {
    var pushParam = event.data.json();
    var title = "";
    var subject = "";
    if (pushParam.ntype == "mail") {
        subject = pushParam.subject;
        if (pushParam.mailcount == "1") {
            title = "[메일] " + pushParam.mailfrom;
        } else {
            title = "[메일 " + pushParam.mailcount + "건] " + pushParam.mailfrom;
        }
    } else if (pushParam.ntype == "appointment") {
        title = "[일정] " + pushParam.calname;
        subject = pushParam.subject;
    } else if (pushParam.ntype == "article") {
        if (pushParam.notifyType == 1) {
            title = "[게시판 > " + pushParam.boardName + " > 새글] " + pushParam.author;
        } else if (pushParam.notifyType == 2) {
            title = "[게시판 > " + pushParam.boardName + " > 답변] " + pushParam.author;
        } else {
            title = "[게시판 > " + pushParam.boardName + " > 댓글] " + pushParam.author;
        }
        subject = pushParam.subject;
    } else if (pushParam.ntype == "approval") {
        var actDate = new Date(pushParam.actDate);
        var actDateStr = actDate.toLocaleDateString();
        if (pushParam.actType == 1) {
            title = "[결재 요청] " + pushParam.subject;
            subject = "기안자: " + pushParam.drafter + ", 작성일: " + actDateStr;
        } else if (pushParam.actType == 2) {
            title = "[결재 승인] " + pushParam.subject;
            subject = pushParam.comment;
        } else if (pushParam.actType == 3) {
            title = "[결재 반려] " + pushParam.subject;
            subject = pushParam.comment;
        } else if (pushParam.actType == 4) {
            title = "[결재 보류] " + pushParam.subject;
            subject = pushParam.comment;
        }
    }

    if (title.length > 0) {
        var options = {
            body: subject,
            icon: "Images/WebPush/mc7.png",
            badge: "Images/WebPush/mc7badge.png",
            data: pushParam
        };
        event.waitUntil(self.registration.showNotification(title, options));
    }
});

self.addEventListener("notificationclick", function (event) {
    var url = "";
    var pushParam = event.notification.data;
    if (pushParam.ntype == "mail") {
        if (pushParam.mailcount == 1) {
            url = self.registration.scope + "Mail/View.aspx?msg=" + pushParam.msgid + "&page=0&webpush=true";
        } else {
            url = self.registration.scope + "Mail/List.aspx?webpush=true";
        }
    } else if (pushParam.ntype == "appointment") {
        url = self.registration.scope + "Calendar/Default.aspx?webpush=true";
    } else if (pushParam.ntype == "article") {
        url = self.registration.scope + "Board/ViewSelector.aspx?article=" + pushParam.articleId + "&webpush=true";
    } else if (pushParam.ntype == "approval") {
        url = self.registration.scope + "Approval/Default.aspx?type=" + pushParam.apvType + "&webpush=true";
    }
    event.notification.close();
    event.waitUntil(
        clients.openWindow(url)
    );
});