サービスワーカーの実装に間違っていることがわかりません。Webプッシュサービス時々バグ
理想的には、「ウェブサイトはバックグラウンドでリフレッシュされました」という問題に直面しています。
コードでこの問題は発生していませんでしたが、プッシュ通知の利用者数が約600に達すると、すべてのコードで問題が発生し始めました。バックグラウンド更新の問題はありませんが、常に動作しません
var init = { method: 'GET',
headers: {
"Content-Type" : "application/javascript"
},
mode: 'cors',
cache: 'no-cache'
};
var worker = self;
var the_endpoint = '';
fetch(the_endpoint, init).then(function(response) {
return response.json();
}).then(function(data){
worker.addEventListener('push', function(event) {
event.waitUntil(
worker.registration.showNotification(data.title, {
body : data.body,
icon : data.icon,
requireInteraction : true
})
);
});
if(data.link != '') {
worker.addEventListener('notificationclick', function(event) {
event.notification.close();
var url = data.link;
event.waitUntil(
clients.matchAll({
type: 'window'
})
.then(function(windowClients) {
for (var i = 0; i < windowClients.length; i++) {
var client = windowClients[i];
if (client.url === url && 'focus' in client) {
return client.focus();
}
}
if (clients.openWindow) {
return clients.openWindow(url);
}
})
);
});
}
});
試み2。
var init = { method: 'GET',
headers: {
"Content-Type" : "application/javascript"
},
mode: 'cors',
cache: 'no-cache'
};
var worker = self;
var the_endpoint = '';
self.addEventListener('push', function(event) {
event.waitUntil(
fetch(the_endpoint, init).then(function (response) {
return response.json().then(function (data) {
var response = self.registration.showNotification(data.title, {
body: data.body,
icon: data.icon,
requireInteraction: true
});
if (data.link != '') {
worker.addEventListener('notificationclick', function (event) {
event.notification.close();
var url = data.link;
event.waitUntil(
clients.matchAll({
type: 'window'
}).then(function (windowClients) {
for (var i = 0; i < windowClients.length; i++) {
var client = windowClients[i];
if (client.url === url && 'focus' in client) {
return client.focus();
}
}
if (clients.openWindow) {
return clients.openWindow(url);
}
})
);
});
}
return response;
});
})
);
});