-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpush.js
95 lines (86 loc) · 2.85 KB
/
webpush.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Check Browser Supports serviceWorker or not
/**
* Function that checks whether browser support web push
* @return boolean
*/
function checkBrowserCompatibity() {
if (!('serviceWorker' in navigator)) {
// Service Worker isn't supported on this browser, disable or hide UI.
alert('Please update browser.');
return false;
}
// Check Browser Supports serviceWorker or not
if (!('PushManager' in window)) {
// Push isn't supported on this browser, disable or hide UI.
alert('Please update browser.');
return false;
}
return true;
}
function urlBase64ToUint8Array(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 (var i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray;
}
/**
* Register Service Worker
*
*/
function registerServiceWorker() {
return navigator.serviceWorker.register('./service-worker.js')
.then(function(registration) {
console.log('Service worker successfully registered.');
// return registration;
// Subscribe user for push
const subscribeOptions = {
userVisibleOnly: true,
applicationServerKey: urlBase64ToUint8Array(VAPID_PUBLIC_KEY)
};
return registration.pushManager.subscribe(subscribeOptions);
}).then(function(pushSubscription) {
console.log('Received PushSubscription: ', JSON.stringify(pushSubscription));
return pushSubscription;
}).catch(function(err) {
console.error('Unable to register service worker.', err);
});
}
/**
* Ask Notification Permission to User
*
*/
function askPermission() {
return new Promise(function(resolve, reject) {
const permissionResult = Notification.requestPermission(function(result) {
resolve(result);
});
if (permissionResult) {
permissionResult.then(resolve, reject);
}
})
.then(function(permissionResult) {
console.log(permissionResult);
if (permissionResult === 'granted') {
//3. Register Service Worker once Permission `granted`
registerServiceWorker();
} else {
throw new Error('We weren\'t granted permission.');
}
});
}
const VAPID_PUBLIC_KEY = "BBPWZxXxaWEbkaG_D6DneIMYl0D6OyQRK3sN8ZXhbotUx4us3aUn6eB8sH3Gh1cOw3HyOzJoHC7Vf5KTvUZXFu0";
//1. Check Browser Compatibility
if(checkBrowserCompatibity()) {
//2. Ask Permission
// VAPID PUBLIC KEY
askPermission();
if(true) {
// registerServiceWorker();
}
}