nanase

고객센터 실시간 채팅 실시간 알림 뱃지 기능 소스 공유

· 2026-04-29 (수) 13:42:11 · 3312 · 5
Club Shooter
1777058218_VAvwpuxdfw.webp
1777437562_SLzWX9E3jr.webp  상담내용 카운트 되면서 표시됩니다.
1777437353_YSMz9nVCfe.webp
// action.php 맨 하단
if(isset($_POST['act']) && $_POST['act'] == 'getTotalUnreadCount'){
    $sql = "SELECT COUNT(*) as cnt FROM `aidex_chatMessage` WHERE `type` = '1' AND `isRead` = '0'";
    $row = sql_fetch($sql);

    // 이 줄 앞에 다른 echo나 출력문이 있으면 안 됩니다.
    ob_clean(); // 혹시 모를 앞선 출력을 비움
    echo json_encode(array("jg" => "ok", "count" => (int)$row['cnt']));
    exit;
}


tail.sub.php 
1777437522_4ytCdfH1ao.webp

<?php include_once(G5_PATH.'/contact/index.php');?>

<span id="chat-unread-count" style="
    display:none;
    position:absolute;
    top: 1205px;
    right: 24px;
    background: #ff4d4f;
    color: #fff;
    font-size: 10px;
    font-family: 'Pretendard', sans-serif;
    font-weight: 700;
    padding: 0 5px;
    border-radius: 20px;
    border: 1.5px solid #fff;
    min-width: 18px;
    height: 18px;
    line-height: 15px;
    text-align: center;
    box-shadow: 0 2px 5px rgba(0,0,0,0.2);
    z-index: 9999;
    box-sizing: border-box;
    justify-content: center;
    align-items: center;
">0</span>
</div>

<audio id="alarm-sound" src="<?php echo G5_URL ?>/contact/mp3/notice.mp3" preload="auto"></audio>

<script>
$(function() {
var lastCount = 0;
var isFirstCheck = true;
var audio = document.getElementById('alarm-sound');

// [중요] 브라우저 정책 해결: 사용자가 페이지를 처음 클릭할 때 오디오를 한 번 로드함
$(document).one('click', function() {
    if(audio) {
        audio.play().then(function() {
            audio.pause(); // 권한만 얻고 즉시 멈춤
            audio.currentTime = 0;
        }).catch(function(e) { console.log("Audio Init Fail"); });
    }
});

function updateChatBadge() {
    $.ajax({
        url: "/adm/contact/action.php",
        type: "POST",
        data: { act: "getTotalUnreadCount" },
        cache: false,
        success: function(data) {
            try {
                var res = JSON.parse(data);
                if (res.jg == "ok") {
                    var count = parseInt(res.count);

                    // 새로운 메시지가 왔을 때만 소리 재생
                    if (!isFirstCheck && count > 0 && count > lastCount) {
                        if (audio) {
                            audio.muted = false;
                            var playPromise = audio.play();
                            if (playPromise !== undefined) {
                                playPromise.catch(function(error) {
                                    console.log("재생 차단됨: 페이지 내 아무곳이나 클릭이 필요합니다.");
                                });
                            }
                        }
                    }

                    lastCount = count;
                    isFirstCheck = false;

                    if (count > 0) {
                        $('#chat-unread-count').text(count > 99 ? '99+' : count).css('display', 'flex');
                    } else {
                        $('#chat-unread-count').hide();
                    }
                }
            } catch(e) {
                console.log("파싱 에러");
            }
        }
    });
}

setInterval(updateChatBadge, 10000);
updateChatBadge();
});
</script><span id="chat-unread-count" style="
    display:none;
    position:absolute;
    top: -3px;
    right: -3px;
    background: #ff4d4f;
    color: #fff;
    font-size: 10px;
    font-family: 'Pretendard', sans-serif;
    font-weight: 700;
    padding: 0 5px;
    border-radius: 20px;
    border: 1.5px solid #fff;
    min-width: 18px;
    height: 18px;
    line-height: 15px;
    text-align: center;
    box-shadow: 0 2px 5px rgba(0,0,0,0.2);
    z-index: 9999;
    box-sizing: border-box;
    justify-content: center;
    align-items: center;
">0</span>
</div>

<audio id="alarm-sound" src="<?php echo G5_URL ?>/contact/mp3/notice.mp3" preload="auto"></audio>

<script>
$(function() {
var lastCount = 0;
var isFirstCheck = true;
var audio = document.getElementById('alarm-sound');

// [중요] 브라우저 정책 해결: 사용자가 페이지를 처음 클릭할 때 오디오를 한 번 로드함
$(document).one('click', function() {
    if(audio) {
        audio.play().then(function() {
            audio.pause(); // 권한만 얻고 즉시 멈춤
            audio.currentTime = 0;
        }).catch(function(e) { console.log("Audio Init Fail"); });
    }
});

function updateChatBadge() {
    $.ajax({
        url: "/adm/contact/action.php",
        type: "POST",
        data: { act: "getTotalUnreadCount" },
        cache: false,
        success: function(data) {
            try {
                var res = JSON.parse(data);
                if (res.jg == "ok") {
                    var count = parseInt(res.count);

                    // 새로운 메시지가 왔을 때만 소리 재생
                    if (!isFirstCheck && count > 0 && count > lastCount) {
                        if (audio) {
                            audio.muted = false;
                            var playPromise = audio.play();
                            if (playPromise !== undefined) {
                                playPromise.catch(function(error) {
                                    console.log("재생 차단됨: 페이지 내 아무곳이나 클릭이 필요합니다.");
                                });
                            }
                        }
                    }

                    lastCount = count;
                    isFirstCheck = false;

                    if (count > 0) {
                        $('#chat-unread-count').text(count > 99 ? '99+' : count).css('display', 'flex');
                    } else {
                        $('#chat-unread-count').hide();
                    }
                }
            } catch(e) {
                console.log("파싱 에러");
            }
        }
    });
}

setInterval(updateChatBadge, 10000);
updateChatBadge();
});
</script>



 
3명이 반응했습니다
|

댓글 5개

2026-04-30 (목) 08:46:37
감사합니다 ^^
감사 합니다.
Tell.sub.php 소스중  url: "/adm2/contact/action.php", 이 내용이 두번 들어가는데 adm2가 아닌 adm으로 숫자만 빼서 넣어주세요.

위에 본문에는 수정해놨습니다.
감사합니다~~~!!!
감사합니다.
댓글을 작성하시려면 로그인이 필요합니다.

그누보드5 팁자료실

2,788건
+
제목 글쓴이 날짜 조회
22-06-20 조회 8,208
09-11 조회 136
09-04 조회 760
08-09 조회 1,616
08-05 조회 2,778
07-30 조회 1,613
07-30 조회 1,513
07-15 조회 2,791
07-01 조회 2,979
06-30 조회 2,965
06-22 조회 3,694
06-17 조회 2,849
06-14 조회 1,587
05-29 조회 1,679
05-16 조회 1,634
05-04 조회 3,127
04-29 조회 3,313
04-28 조회 3,137
04-27 조회 3,100
04-25 조회 2,132
04-24 조회 3,858
04-23 조회 3,255
04-14 조회 2,274
04-08 조회 3,117
04-01 조회 4,814
03-31 조회 3,824
03-16 조회 3,982
03-14 조회 5,149
03-13 조회 4,154
03-10 조회 3,934
03-09 조회 4,058