Created
May 20, 2019 14:20
-
-
Save elcolie/1d533b0db68c5c117aa304ab16befcf5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<html> | |
<script> | |
// Utils functions: | |
function urlBase64ToUint8Array (base64String) { | |
var padding = '='.repeat((4 - base64String.length % 4) % 4) | |
var base64 = (base64String + padding) | |
.replace(/\-/g, '+') | |
.replace(/_/g, '/') | |
var rawData = window.atob(base64) | |
var outputArray = new Uint8Array(rawData.length) | |
for (var i = 0; i < rawData.length; ++i) { | |
outputArray[i] = rawData.charCodeAt(i) | |
} | |
return outputArray; | |
} | |
function loadVersionBrowser (userAgent) { | |
var ua = userAgent; | |
var tem; | |
var M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || []; | |
if (/trident/i.test(M[1])) { | |
tem = /\brv[ :]+(\d+)/g.exec(ua) || []; | |
return {name: 'IE', version: (tem[1] || '')}; | |
} | |
if (M[1] === 'Chrome') { | |
tem = ua.match(/\bOPR\/(\d+)/); | |
if (tem != null) { | |
return {name: 'Opera', version: tem[1]}; | |
} | |
} | |
M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?']; | |
if ((tem = ua.match(/version\/(\d+)/i)) != null) { | |
M.splice(1, 1, tem[1]); | |
} | |
return { | |
name: M[0], | |
version: M[1] | |
}; | |
}; | |
var applicationServerKey = "REPLACE-THIS-ACCORDING-TO-YOUR-SETUP"; | |
// In your ready listener | |
if ('serviceWorker' in navigator) { | |
// The service worker has to store in the root of the app | |
// http://stackoverflow.com/questions/29874068/navigator-serviceworker-is-never-ready | |
var browser = loadVersionBrowser('chrome'); | |
navigator.serviceWorker.register('navigatorPush.service.js?version=1.0.0').then(function (reg) { | |
reg.pushManager.subscribe({ | |
userVisibleOnly: true, | |
applicationServerKey: urlBase64ToUint8Array(applicationServerKey) | |
}).then(function (sub) { | |
var endpointParts = sub.endpoint.split('/'); | |
var registration_id = endpointParts[endpointParts.length - 1]; | |
var data = { | |
'browser': browser.name.toUpperCase(), | |
'p256dh': btoa(String.fromCharCode.apply(null, new Uint8Array(sub.getKey('p256dh')))), | |
'auth': btoa(String.fromCharCode.apply(null, new Uint8Array(sub.getKey('auth')))), | |
'name': 'XXXXX', | |
'registration_id': registration_id | |
}; | |
console.log(data); | |
}) | |
}).catch(function (err) { | |
console.log(':^(', err); | |
}); | |
} | |
</script> | |
Hello | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment