0

저는 node 및 socket.io를 처음 사용합니다. 방금 푸시 알림을 사용하여 Chrome 확장 프로그램을 완료했습니다. 크롬 브라우저가 닫히면 알림이 전송되지 않는 문제가 있습니다. 서버가 아래와 같이 알림을 보내고 알림이 전송되지 않습니다.노드 - 브라우저가 닫힐 때 알림 푸시

info - transport end (socket end) 
debug - set close timeout for client kgcuzAOgn5-lrnuQI0Qb 
debug - cleared close timeout for client kgcuzAOgn5-lrnuQI0Qb 
debug - cleared heartbeat interval for client kgcuzAOgn5-lrnuQI0Qb 
debug - discarding transport 

여기 Pls는 날이 문제를 해결하는 데 도움이

서버

io.sockets.on('connection', function (socket) { 
fs.watch('example.xml', function (curr, prev) { 
    fs.readFile('example.xml', function (err, data) { 
    if (err) throw err;  
    parser.parseString(data); 
}); 
}); 

    parser.addListener('end', function(result) { 

    // adding the time of the latest update 
    //result.time = new Date(); 
    socket.volatile.emit('notification' , result); 
    }); 
}); 

클라이언트

var socket = new io.connect('http://website:8000'); 
console.log('client connected'); 

socket.on('notification', function(data){ 
console.log("message recieved1 = "+data); 

    console.log("message recieved = "+data); 
    showNotification(data) 
    chrome.browserAction.setBadgeText({ text : "1" }); 


}); 

function showNotification(data){ 
var havePermission = window.webkitNotifications.checkPermission(); 
    if (havePermission == 0) { 
    // 0 is PERMISSION_ALLOWED  

var notification = webkitNotifications.createNotification(
    'http://website.com/favicon.ico', // icon url - can be relative 
    'Hello!', // notification title 
    data // notification body text 
); 
// Hide the notification after the configured duration. 
    //setTimeout(function(){ notification.cancel(); }, 5000); 

    notification.onclick = function() { 
     window.open("http://website.com/favicon.gif"); 
     notification.close(); 
    resetBadgeText(); 
    } 
    chrome.browserAction.setBadgeText({"text":"1"}); 
    notification.show(); 
    } else { 
     window.webkitNotifications.requestPermission(); 
    } 
} 

내 코드입니다.

+0

아마도이 문제를 해결하려면 다음 작업을 수행 할 수 있습니다. http://stackoverflow.com/questions/3699357/event-calling-before-page-unload – TheHippo

답변

0

Chrome 푸시 알림은 브라우저가 실행 중일 때만 작동합니다. 또한 모든 브라우저 창을 닫으면 소켓이 오프라인 상태가되어 그 이유를 알 수 있습니다.

+0

브라우저를 닫을 때 알림을 보낼 방법이 있습니까? – user1087188

+0

아니요 브라우저를 닫을 때 알림을 보낼 방법이 없습니다. 프로세스 또는 서비스가 백그라운드에서 실행 중이어야합니다. 귀하의 초기 요구 사항에 대해 확실하지 않습니다. 그러나 소켓 연결을 유지하기 위해 백그라운드에서 실행될 서비스를 만들어 모바일 장치에서이 작업을 수행 할 수 있습니다. – user1974500

관련 문제