-
-
Save anitawoodruff/b41cad489dba75cdbaf2ea942450cd1f to your computer and use it in GitHub Desktop.
Notification text action demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<!-- View this at https://rawgit.com/anitawoodruff/b41cad489dba75cdbaf2ea942450cd1f/raw/ --> | |
<meta name=viewport content="initial-scale=1"> | |
<a href="https://gist.github.com/anitawoodruff/b41cad489dba75cdbaf2ea942450cd1f">View code</a><br><br> | |
<button id="myButton">Show notification</button> | |
<script> | |
navigator.serviceWorker.register('sw.js'); | |
document.getElementById('myButton').addEventListener('click', event => { | |
navigator.serviceWorker.ready.then(swRegistration => { | |
Notification.requestPermission(permission => { | |
if (permission != 'granted') | |
console.log("Grant Notifications permission then try again"); | |
swRegistration.showNotification("New Title", { | |
body: new Array(8 + 1).join("Testing text type actions.. "), | |
icon: 'https://tests.peter.sh/resources/icons/13.png', | |
actions: [{action: 'myAction', title: 'reply', type: 'text'}] | |
}); | |
}); | |
}); | |
}); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self.addEventListener('install', () => skipWaiting()); | |
self.addEventListener('activate', () => clients.claim()); | |
self.addEventListener('notificationclick', event => { | |
console.log("notificationclick with reply=" + event.reply); | |
registration.showNotification("Echo", {body: 'You said: ' + event.reply}); | |
event.notification.close(); | |
event.waitUntil(clients.matchAll().then(cs => { | |
for (var client of cs) { | |
if (client.url == registration.scope) | |
return client.focus(); | |
} | |
return clients.openWindow(registration.scope); | |
})); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment