我有服务人员的代码。
self.addEventListener('activate', function(event) {
database.on("child_added", function(snapshot) {
var lists = firebase.database().ref('notify');
lists.once('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var childKey = childSnapshot.key;
var childData = childSnapshot.val();
if(childData.show == true){
childData.show = false;
firebase.database().ref('notify/' + childKey).set(childData);
// Just notify show == true
var notify = childData;
if (Notification.permission !== 'default'){
var title = notify.title;
var options = {
body: notify.body,
image: notify.image,
icon: notify.icon,
data: [
notify.link
]
};
return self.registration.showNotification(title,
options);
}
}else{
return;
}
});
});
});
});
self.addEventListener('notificationclick', function(event) {
var click_action = event.notification.data[0];
return clients.openWindow(click_action);
});
和index.html
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register("{{url('/')}}" + '/firebase-messaging-sw.js').then((registration) => {
messaging.useServiceWorker(registration);
});
}
</script>
当我打开正在运行的网站服务工作者并转发我的列表数据库时,但是当我关闭它时,它会更改状态“已激活并已停止”。 如何为工作人员实时运行并收听Firebase数据库上的更改提供服务?