Skip to content

Instantly share code, notes, and snippets.

@anitawoodruff
Last active October 26, 2016 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/a859c8e6c50082114cef2c1ccf7e5c8b to your computer and use it in GitHub Desktop.
Save anitawoodruff/a859c8e6c50082114cef2c1ccf7e5c8b to your computer and use it in GitHub Desktop.
Testing multiple subscribe() calls
<!doctype html>
<!-- View this at https://rawgit.com/anitawoodruff/b41cad489dba75cdbaf2ea942450cd1f/raw/ -->
<head>
<meta name=viewport content="initial-scale=1">
</head>
<body>
<a href="https://gist.github.com/anitawoodruff/a859c8e6c50082114cef2c1ccf7e5c8b">View code</a><br><br>
<button id="btn">Subscribe</button>
<script>
navigator.serviceWorker.register('sw-resubscribe.js');
document.getElementById('btn').addEventListener('click', event => {
navigator.serviceWorker.ready.then(
function(serviceWorkerRegistration) {
serviceWorkerRegistration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: new Uint8Array([
0x04, 0x55, 0x52, 0x6A, 0xA5, 0x6E, 0x8E, 0xAA, 0x47, 0x97, 0x36, 0x10, 0xC1,
0x66, 0x3C, 0x1E, 0x65, 0xBF, 0xA1, 0x7B, 0xEE, 0x48, 0xC9, 0xC6, 0xBB, 0xBF,
0x02, 0x18, 0x53, 0x72, 0x1D, 0x0C, 0x7B, 0xA9, 0xE3, 0x11, 0xB7, 0x03, 0x52,
0x21, 0xD3, 0x71, 0x90, 0x13, 0xA8, 0xC1, 0xCF, 0xED, 0x20, 0xF7, 0x1F, 0xD1,
0x7F, 0xF2, 0x76, 0xB6, 0x01, 0x20, 0xD8, 0x35, 0xA5, 0xD9, 0x3C, 0x43, 0xFD
]).buffer
}).then(
function(pushSubscription) {
console.log('Subscribed from document!');
console.log('endpoint: ' + pushSubscription.endpoint);
console.log('p256dh: ' + pushSubscription.getKey('p256dh'));
console.log('auth: ' + pushSubscription.getKey('auth'));
}, function(error) {
console.log(error);
}
);
});
navigator.serviceWorker.ready.then(swr => {
Notification.requestPermission(permission => {
if (permission != 'granted')
alert("Grant Notifications permission then try again");
swr.showNotification("New Title", {
body: new Array(8 + 1).join("This is to provoke a notificationclick event.. "),
icon: 'https://tests.peter.sh/resources/icons/13.png',
});
});
});
});
</script>
</body>
self.addEventListener('install', () => skipWaiting());
self.addEventListener('activate', () => clients.claim());
self.addEventListener('notificationclick', event => {
console.log('notification clicked');
event.notification.close();
registration.pushManager.subscribe({
userVisibleOnly: true
}).then(
function(pushSubscription) {
console.log('Subscribed from sw');
console.log('endpoint: ' + pushSubscription.endpoint);
console.log('p256dh: ' + pushSubscription.getKey('p256dh'));
console.log('auth: ' + pushSubscription.getKey('auth'));
}, function(error) {
console.log(error);
}
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment