【问题标题】:How to do I make sure that socket.io does not close connections when the app is in background in a Angular PWA如何确保当应用程序在 Angular PWA 中处于后台时 socket.io 不会关闭连接
【发布时间】:2021-04-22 20:35:45
【问题描述】:

socket.on('new-notification', function(data) {

  let room_id = data.room_id;
  var receiver_id = data.receiver_id;
  var booking_request_id = data.booking_request_id;
  var message = {
    to: notification_payload,
    collapse_key: 'green',

    notification: {
      title: 'Tittle',
      body: "New message from  User",
      icon: site_url + 'assets/icons/icon- 72x72.png',
      sound: 'default',
      url: redirect_url,
    },
    data: {
      title: 'Tittle',
      body: "New message from  User",
      icon: site_url + 'assets/icons/icon- 72x72.png',
      sound: 'default',
      url: redirect_url,
    }
  };

  fcm.send(message, function(err, response) {
    if (err) {
      console.log("Something has gone wrong!");
    } else {
      console.log("Successfully sent with response: ", response);
    }
  });
}
});
}
else {

}
}
});
});

我有一个 Angular PWA,它使用 Socket.io Node 和 express 服务器进行实时消息传递和使用 Firebase Cloud Messaging 推送通知。消息传递工作正常,但问题是当用户离开应用程序时,他们将无法接收通知,而只能在积极使用应用程序时接收。我的假设是当用户离开应用程序时 Socket io 正在断开连接。那么即使应用程序在后台运行,有什么方法可以确保套接字保持活动状态。 我已附上我的通知 sn-p。非常感谢您的帮助

【问题讨论】:

标签: node.js socket.io firebase-cloud-messaging angular-pwa


【解决方案1】:

为此,您需要为 Angular 设置一个服务工作者,当设备通常将应用程序置于睡眠模式时,它们会运行并保持您的应用程序处于活动状态以执行某些任务。

你可以关注最新的 Angular 文档来创建 service worker HERE

【讨论】:

  • 感谢您的回复,但这已经是 PWA 意味着 service worker 已经到位
  • 您能否确认 Service Worker 是否正常运行并已连接?据报道,最新的 Angular 8 存在问题,降级有所帮助。
  • 谢谢,但是服务人员工作正常。
猜你喜欢
  • 1970-01-01
  • 2017-03-10
  • 2017-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多