节点 - 浏览器关闭时推送通知

时间:2013-05-17 12:03:55

标签: node.js google-chrome google-chrome-extension push-notification socket.io

我是node和socket.io的新手。我刚刚通过推送通知完成了Chrome扩展。在我遇到问题时,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

这是我的代码

服务器

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();
  }
 }

请帮助我解决这个问题。

1 个答案:

答案 0 :(得分:0)

Chrome推送通知仅在浏览器运行时有效。此外,一旦关闭所有浏览器窗口,套接字将处于脱机状态,这就是您收到此响应的原因。