forked from lahiiru/browser-push
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
166 lines (151 loc) · 5.19 KB
/
main.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'use strict';
/*
* Visit Github page[Browser-push](https://lahiiru.github.io/browser-push) for guide lines.
*/
const applicationServerPublicKey = 'BHJbjjDO-6F-eXTqtVVu_aHZ_yssYATwe-3bU8pqXwe8Xik0lkq6LI9rOojUbVqh46GQrxoObj4A4zv_T9hBpZ0';
var domain = "web.tech.lahiru";
var ua = window.navigator.userAgent,
safariTxt = ua.indexOf ( "Safari" ),
chrome = ua.indexOf ( "Chrome" ),
version = ua.substring(0,safariTxt).substring(ua.substring(0,safariTxt).lastIndexOf("/")+1);
var actionButton = document.querySelector('.action-button');
let isSubscribed = false;
let swRegistration = null;
console.log('%c WebPush script injected.', 'background: green; color: white; display: block; font-size:20px');
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;
}
function updateBtn() {
if (Notification.permission === 'denied') {
alert('Push Messaging Blocked.');
updateSubscriptionOnServer(null);
return;
}
if (isSubscribed) {
actionButton.innerText='Disable Push Messaging';
} else {
actionButton.innerText='Enable Push Messaging';
}
}
function updateSubscriptionOnServer(subscription) {
/* TODO: Send the subscription object to application server.
* notifications are sent from the server using this object.
*/
if (subscription) {
console.log(JSON.stringify(subscription));
}
}
function subscribeUser() {
const applicationServerKey = urlB64ToUint8Array(applicationServerPublicKey);
swRegistration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: applicationServerKey
}).then(function(subscription) {
console.log('User is subscribed:', subscription);
updateSubscriptionOnServer(subscription);
isSubscribed = true;
updateBtn();
// Show subscription for debug
window.prompt('Subscription details:',JSON.stringify(subscription));
})
.catch(function(err) {
console.log('Failed to subscribe the user: ', err);
updateBtn();
});
}
function unsubscribeUser() {
swRegistration.pushManager.getSubscription().then(function(subscription) {
if (subscription) {
alert("You are not subscribed now.")
return subscription.unsubscribe();
}
}).catch(function(error) {
console.log('Error unsubscribing', error);
}).then(function() {
isSubscribed = false;
updateSubscriptionOnServer(null);
console.log('User is unsubscribed.');
updateBtn();
});
}
function subscribe() {
if (isSubscribed) {
unsubscribeUser();
} else {
subscribeUser();
}
// Set the initial subscription value
swRegistration.pushManager.getSubscription().then(function(subscription) {
isSubscribed = !(subscription === null);
updateSubscriptionOnServer(subscription);
});
}
// For safari
function requestPermissions() {
window.safari.pushNotification.requestPermission('https://apps.wearetrying.info/push-api', domain, {}, function(subscription) {
console.log(subscription);
if(c.permission === 'granted') {
updateSubscriptionOnServer(subscription);
}
else if(c.permission === 'denied') {
// TODO:
}
});
}
function nonSafariInit(){
if ('serviceWorker' in navigator && 'PushManager' in window) {
console.log('Service Worker and Push is supported');
actionButton.addEventListener('click',function(){
subscribe();
});
navigator.serviceWorker.register('sw.js').then(function(swReg) {
console.log('Service Worker is registered', swReg);
actionButton.innerText='Enable Push Messaging';
swRegistration = swReg;
}).catch(function(error) {
console.error('Service Worker Error', error);
});
} else {
console.warn('Push messaging is not supported');
alert('Push Not Supported');
}
}
// For safari
function safariIniti() {
var pResult = window.safari.pushNotification.permission(domain);
if(pResult.permission === 'default') {
//request permission
requestPermissions();
} else if (pResult.permission === 'granted') {
console.log("Permission for " + domain + " is " + pResult.permission);
var token = pResult.deviceToken;
// Show subscription for debug
window.prompt('Subscription details:',token);
} else if(pResult.permission === 'denied') {
alert("Permission for " + domain + " is " + pResult.permission);
}
}
/*
* Call relevant methods.
*/
if(chrome ==-1 && safariTxt > 0) {
if(parseInt(version, 10) >=7){
console.log("Safari browser detected.");
safariIniti();
} else {
console.log("Safari unsupported version detected.");
}
}
else {
console.log("Non Safari browser detected.");
nonSafariInit();
}