-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapp-subscription.js
153 lines (134 loc) · 5.58 KB
/
app-subscription.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
import $ from 'jquery';
import { serviceWorkerEnabled, createToast } from 'app';
function deleteSubscription(url) {
window.location.href = url;
}
function testNotification(url) {
fetch(url, {
credentials: 'include',
method: 'GET',
mode: 'cors',
}).then(function(response) {
return response.json();
}).then(function(json) {
if ('undefined' !== typeof json.message) {
createToast(json.message);
} else {
createToast('Sent');
}
})
.catch(function(error) {
console.log(error);
});
}
function getSubscription() {
if (true == serviceWorkerEnabled) {
navigator.serviceWorker.ready.then(function(ServiceWorkerRegistration) {
if ('pushManager' in ServiceWorkerRegistration) {
ServiceWorkerRegistration.pushManager.getSubscription()
.then(function(PushSubscription) {
if (PushSubscription && 'object' === typeof PushSubscription) {
$(document).find('.actions').each(function() {
var deleteButton = $(this).find('button');
var unsubscribeButton = $(this).find('.unsubscribe-button');
if (PushSubscription.endpoint == unsubscribeButton.data('endpoint')) {
unsubscribeButton.removeClass('d-none');
deleteButton.addClass('d-none');
}
});
}
});
}
});
}
}
function pushManagerUnsubscribe(url) {
if (true == serviceWorkerEnabled) {
navigator.serviceWorker.ready.then(function(ServiceWorkerRegistration) {
if ('pushManager' in ServiceWorkerRegistration) {
ServiceWorkerRegistration.pushManager.getSubscription()
.then(function(PushSubscription) {
if (PushSubscription && 'object' === typeof PushSubscription) {
PushSubscription.unsubscribe()
.then(function() {
deleteSubscription(url);
})
.catch(function(error) {
console.log(error);
});
}
});
}
});
}
}
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 getSubscriptionCreate() {
if (true == serviceWorkerEnabled) {
navigator.serviceWorker.ready.then(function(ServiceWorkerRegistration) {
if ('pushManager' in ServiceWorkerRegistration) {
ServiceWorkerRegistration.pushManager.getSubscription()
.then(function(PushSubscription) {
if (PushSubscription && 'object' === typeof PushSubscription) {
var toJSON = PushSubscription.toJSON();
var endpoint = document.getElementById('data_endpoint');
endpoint.value = PushSubscription.endpoint;
var publicKey = document.getElementById('data_public_key');
publicKey.value = toJSON.keys.p256dh;
var authenticationSecret = document.getElementById('data_authentication_secret');
authenticationSecret.value = toJSON.keys.auth;
var contentEncoding = document.getElementById('data_content_encoding');
contentEncoding.value = (PushManager.supportedContentEncodings || ['aesgcm'])[0];
}
});
}
});
}
}
function pushManagerSubscribe() {
if (true == serviceWorkerEnabled) {
navigator.serviceWorker.ready.then(function(ServiceWorkerRegistration) {
if ('pushManager' in ServiceWorkerRegistration) {
ServiceWorkerRegistration.pushManager.permissionState({userVisibleOnly: true}).then(function(permissionState) {
if (permissionState == 'prompt' || permissionState == 'granted') {
ServiceWorkerRegistration.pushManager.subscribe(
{'applicationServerKey': urlBase64ToUint8Array(applicationServerKey), 'userVisibleOnly': true}
)
.then(function(PushSubscription) {
if (PushSubscription && 'object' === typeof PushSubscription) {
getSubscriptionCreate();
}
});
}
});
}
});
}
}
var allowNotifications = document.getElementById('allow-notifications');
if (allowNotifications) {
allowNotifications.addEventListener('click', function(event) {
event.preventDefault();
pushManagerSubscribe();
});
}
$(document).ready(function() {
getSubscription();
$(document).on('click', '.unsubscribe-button', function(event) {
event.preventDefault();
pushManagerUnsubscribe($(this).attr('href'));
});
$(document).on('click', '.test-notification-push', function(event) {
event.preventDefault();
testNotification($(this).attr('href'));
});
});