Created
February 22, 2018 15:52
-
-
Save ThePendulum/93782910eefc3cd75eec68b1d7a98b61 to your computer and use it in GitHub Desktop.
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
/* | |
* | |
* Push Notifications codelab | |
* Copyright 2015 Google Inc. All rights reserved. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* https://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License | |
* | |
*/ | |
/* eslint-env browser, serviceworker, es6 */ | |
'use strict'; | |
/* eslint-disable max-len */ | |
const applicationServerPublicKey = 'BH8-hIchXKMI6AKSee8gD0hhPThRqaEhIEtMJwcTjEQhiOKdG-_2tTIO-6hOAK4kwg5M9Saedjxp4hVE-khhWxY'; | |
/* eslint-enable max-len */ | |
function urlB64ToUint8Array(base64String) { | |
const padding = '='.repeat((4 - base64String.length % 4) % 4); | |
const base64 = (base64String + padding) | |
.replace(/\-/g, '+') | |
.replace(/_/g, '/'); | |
const rawData = window.atob(base64); | |
const outputArray = new Uint8Array(rawData.length); | |
for (let i = 0; i < rawData.length; ++i) { | |
outputArray[i] = rawData.charCodeAt(i); | |
} | |
return outputArray; | |
} | |
self.addEventListener('push', function(event) { | |
console.log('[Service Worker] Push Received.'); | |
console.log(`[Service Worker] Push had this data: "${event.data.text()}"`); | |
const title = 'Push Codelab'; | |
const options = { | |
body: 'Yay it works.', | |
icon: 'images/icon.png', | |
badge: 'images/badge.png' | |
}; | |
event.waitUntil(self.registration.showNotification(title, options)); | |
}); | |
self.addEventListener('notificationclick', function(event) { | |
console.log('[Service Worker] Notification click Received.'); | |
event.notification.close(); | |
event.waitUntil( | |
clients.openWindow('https://developers.google.com/web/') | |
); | |
}); | |
self.addEventListener('pushsubscriptionchange', function(event) { | |
console.log('[Service Worker]: \'pushsubscriptionchange\' event fired.'); | |
const applicationServerKey = urlB64ToUint8Array(applicationServerPublicKey); | |
event.waitUntil( | |
self.registration.pushManager.subscribe({ | |
userVisibleOnly: true, | |
applicationServerKey: applicationServerKey | |
}) | |
.then(function(newSubscription) { | |
// TODO: Send to application server | |
console.log('[Service Worker] New subscription: ', newSubscription); | |
}) | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment