-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
303 lines (262 loc) · 10.9 KB
/
app.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
var isPushEnabled = false;
window.addEventListener('load', function() {
var pushButton = document.querySelector('.js-push-button');
if (pushButton) {
pushButton.addEventListener('click', function() {
if (isPushEnabled) {
push_unsubscribe();
} else {
push_subscribe();
}
});
}
// double notification badge only on mobile layout
var navbarToggle = $('.navbar-toggle:visible');
if(navbarToggle) {
var notificationCounters = document.getElementsByClassName('notificationCounter');
if (notificationCounters.length) {
var notificationCounterWrapperNavbar = document.querySelector('.navbar-toggle .notificationCounterWrapper');
var notificationCounter = document.createElement('span');
notificationCounter.className = 'notificationCounter badge badge-notification';
notificationCounter.textContent = notificationCounters[0].textContent;
notificationCounterWrapperNavbar.appendChild(notificationCounter);
}
}
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register(window.swPath)
.then(function(sw) {
console.log('[SW] Service worker enregistré');
push_initialiseState();
initPostMessageListener();
}, function (e) {
console.error('[SW] Oups...', e);
changePushButtonState('incompatible');
});
} else {
console.warn('[SW] Les service workers ne sont pas encore supportés par ce navigateur.');
changePushButtonState('incompatible');
}
});
function changePushButtonState(state) {
var $pushButtons = $('.js-push-button');
for (var i = 0; i < $pushButtons.length; i++) {
var pushButton = $pushButtons[i];
var $pushButton = $(pushButton);
switch (state) {
case 'enabled':
pushButton.disabled = false;
pushButton.title = "Notifications Push activées";
$pushButton.addClass("active");
isPushEnabled = true;
break;
case 'disabled':
pushButton.disabled = false;
pushButton.title = "Notifications Push désactivées";
$pushButton.removeClass("active");
isPushEnabled = false;
break;
case 'computing':
pushButton.disabled = true;
pushButton.title = "Chargement...";
break;
case 'incompatible':
pushButton.disabled = true;
pushButton.title = "Notifications Push non disponibles (navigateur non compatible)";
break;
default:
console.error('Unhandled push button state', state);
break;
}
}
}
function initPostMessageListener() {
var onRefreshNotifications = function () {
var notificationCounters = document.getElementsByClassName('notificationCounter');
if (!notificationCounters.length) {
var notificationCounterWrappers = document.getElementsByClassName('notificationCounterWrapper');
for (var i = 0; i < notificationCounterWrappers.length; i++) {
var notificationCounter = document.createElement('span');
notificationCounter.className = 'notificationCounter badge badge-notification';
notificationCounter.textContent = '0';
notificationCounterWrappers[i].appendChild(notificationCounter);
}
}
for (var i = 0; i < notificationCounters.length; i++) {
notificationCounters[i].textContent++;
}
};
var onRemoveNotifications = function() {
$('.notificationCounter').remove();
};
navigator.serviceWorker.addEventListener('message', function(e) {
var message = e.data;
switch (message) {
case 'reload':
window.location.reload(true);
break;
case 'refreshNotifications':
onRefreshNotifications();
break;
case 'removeNotifications':
onRemoveNotifications();
break;
default:
console.warn("Message '" + message + "' not handled.");
break;
}
});
}
function push_initialiseState() {
// Are Notifications supported in the service worker?
if (!('showNotification' in ServiceWorkerRegistration.prototype)) {
console.warn('[SW] Les notifications ne sont pas supportées par ce navigateur.');
changePushButtonState('incompatible');
return;
}
// Check the current Notification permission.
// If its denied, it's a permanent block until the
// user changes the permission
if (Notification.permission === 'denied') {
console.warn('[SW] Les notifications ne sont pas autorisées par l\'utilisateur.');
changePushButtonState('disabled');
return;
}
// Check if push messaging is supported
if (!('PushManager' in window)) {
console.warn('[SW] Les messages Push ne sont pas supportés par ce navigateur.');
changePushButtonState('incompatible');
return;
}
// We need the service worker registration to check for a subscription
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
// Do we already have a push message subscription?
serviceWorkerRegistration.pushManager.getSubscription()
.then(function(subscription) {
// Enable any UI which subscribes / unsubscribes from
// push messages.
changePushButtonState('disabled');
if (!subscription) {
// We aren't subscribed to push, so set UI
// to allow the user to enable push
return;
}
// Keep your server in sync with the latest endpoint
push_sendSubscriptionToServer(subscription, 'update');
// Set your UI to show they have subscribed for push messages
changePushButtonState('enabled');
})
['catch'](function(err) {
console.warn('[SW] Erreur pendant getSubscription()', err);
});
});
}
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;
}
function push_subscribe() {
// Disable the button so it can't be changed while
// we process the permission request
changePushButtonState('computing');
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
serviceWorkerRegistration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: urlBase64ToUint8Array(vapidPublicKey)
})
.then(function(subscription) {
// The subscription was successful
changePushButtonState('enabled');
// on a la subscription, il faut l'enregistrer en BDD
return push_sendSubscriptionToServer(subscription, 'create');
})
['catch'](function(e) {
if (Notification.permission === 'denied') {
// The user denied the notification permission which
// means we failed to subscribe and the user will need
// to manually change the notification permission to
// subscribe to push messages
console.warn('[SW] Les notifications ne sont pas autorisées par l\'utilisateur.');
changePushButtonState('incompatible');
} else {
// A problem occurred with the subscription; common reasons
// include network errors, and lacking gcm_sender_id and/or
// gcm_user_visible_only in the manifest.
console.error('[SW] Impossible de souscrire aux notifications.', e);
changePushButtonState('disabled');
}
});
});
}
function push_unsubscribe() {
changePushButtonState('computing');
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
// To unsubscribe from push messaging, you need get the
// subscription object, which you can call unsubscribe() on.
serviceWorkerRegistration.pushManager.getSubscription().then(
function(pushSubscription) {
// Check we have a subscription to unsubscribe
if (!pushSubscription) {
// No subscription object, so set the state
// to allow the user to subscribe to push
changePushButtonState('disabled');
return;
}
push_sendSubscriptionToServer(pushSubscription, 'delete');
// We have a subscription, so call unsubscribe on it
pushSubscription.unsubscribe().then(function(successful) {
changePushButtonState('disabled');
})['catch'](function(e) {
// We failed to unsubscribe, this can lead to
// an unusual state, so may be best to remove
// the users data from your data store and
// inform the user that you have done so
console.log('[SW] Erreur pendant le désabonnement aux notifications: ', e);
changePushButtonState('disabled');
});
})['catch'](function(e) {
console.error('[SW] Erreur pendant le désabonnement aux notifications.', e);
});
});
}
function push_sendSubscriptionToServer(subscription, action) {
var req = new XMLHttpRequest();
var url = Routing.generate('pjm_app_api_pushsubscription_' + action);
req.open('POST', url, true);
req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
req.setRequestHeader("Content-type", "application/json");
req.onreadystatechange = function (e) {
if (req.readyState == 4) {
if(req.status != 200) {
console.error("[SW] Erreur :" + e.target.status);
}
}
};
req.onerror = function (e) {
console.error("[SW] Erreur :" + e.target.status);
};
var key = subscription.getKey('p256dh');
var token = subscription.getKey('auth');
req.send(JSON.stringify({
'endpoint': getEndpoint(subscription),
'key': key ? btoa(String.fromCharCode.apply(null, new Uint8Array(key))) : null,
'token': token ? btoa(String.fromCharCode.apply(null, new Uint8Array(token))) : null
}));
return true;
}
function getEndpoint(pushSubscription) {
var endpoint = pushSubscription.endpoint;
var subscriptionId = pushSubscription.subscriptionId;
// fix Chrome < 45
if (subscriptionId && endpoint.indexOf(subscriptionId) === -1) {
endpoint += '/' + subscriptionId;
}
return endpoint;
}