目前在幫網站加上PWA功能,但研究好幾天還是搞不定,特來求救...
SW啟動狀態,但好像要到網站才會顯示通知,是否還要加上Firebase?
而A2HS沒反應?
<script>
if( 'serviceWorker' in navigator )
{
window.addEventListener('load', function() {
navigator.serviceWorker.register('sw.js');
if( Notification.permission !== 'granted' )
Notification.requestPermission();
});
}
var deferredPrompt;
window.addEventListener('beforeinstallprompt', function(e)
{
e.preventDefault();
deferredPrompt = e;
document.querySelectorAll("div").addEventListener('click', function()
{
deferredPrompt.prompt();
deferredPrompt = null;
});
});
</script>
sw.js
if( Notification.permission === 'granted' )
fetch('sw').then(function(response) {
response.text().then(function(text) {
if( text ) self.registration.showNotification("系統通知", { body: "您有 "+text+" 筆未讀訊息", icon: "logo.png" });
});
});
self.addEventListener('notificationclick', function(event)
{
event.notification.close();
event.waitUntil(clients.openWindow('https://...'));
});