Skip to content

Instantly share code, notes, and snippets.

@anitawoodruff
Forked from johnmellor/index.html
Last active February 5, 2018 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anitawoodruff/b41cad489dba75cdbaf2ea942450cd1f to your computer and use it in GitHub Desktop.
Save anitawoodruff/b41cad489dba75cdbaf2ea942450cd1f to your computer and use it in GitHub Desktop.
Notification text action demo
<!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>
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