【问题标题】:Node - Push notification when browser closed节点 - 浏览器关闭时推送通知
【发布时间】:2013-05-17 12:03:54
【问题描述】:

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

请帮我解决这个问题。

【问题讨论】:

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


【解决方案1】:

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

【讨论】:

  • 有没有办法在浏览器关闭时发送通知??
  • 不,浏览器关闭时无法发送通知。进程或服务应该在后台运行。我不确定您的初始要求。但是,您可以通过创建将在后台运行以保持套接字连接处于活动状态的服务来在移动设备上实现此目的。
猜你喜欢
  • 2015-09-28
  • 2019-02-14
  • 1970-01-01
  • 2018-01-24
  • 2020-12-16
  • 1970-01-01
  • 1970-01-01
  • 2016-12-26
  • 1970-01-01
相关资源
最近更新 更多