-
Notifications
You must be signed in to change notification settings - Fork 894
/
sw-controller.ts
366 lines (329 loc) · 10.8 KB
/
sw-controller.ts
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
import ControllerInterface from './controller-interface';
import Errors from '../models/errors';
import WorkerPageMessage from '../models/worker-page-message';
import FCMDetails from '../models/fcm-details';
const FCM_MSG = 'FCM_MSG';
export default class SWController extends ControllerInterface {
private bgMessageHandler_: (input: Object) => Promise<any>;
constructor(app) {
super(app);
self.addEventListener('push', e => this.onPush_(e), false);
self.addEventListener(
'pushsubscriptionchange',
e => this.onSubChange_(e),
false
);
self.addEventListener(
'notificationclick',
e => this.onNotificationClick_(e),
false
);
/**
* @private
* @type {function(Object)|null}
*/
this.bgMessageHandler_ = null;
}
/**
* A handler for push events that shows notifications based on the content of
* the payload.
*
* The payload must be a JSON-encoded Object with a `notification` key. The
* value of the `notification` property will be used as the NotificationOptions
* object passed to showNotification. Additionally, the `title` property of the
* notification object will be used as the title.
*
* If there is no notification data in the payload then no notification will be
* shown.
* @private
*/
onPush_(event) {
let msgPayload;
try {
msgPayload = event.data.json();
} catch (err) {
// Not JSON so not an FCM message
return;
}
const handleMsgPromise = this.hasVisibleClients_().then(
hasVisibleClients => {
if (hasVisibleClients) {
// Do not need to show a notification.
if (msgPayload.notification || this.bgMessageHandler_) {
// Send to page
return this.sendMessageToWindowClients_(msgPayload);
}
return;
}
const notificationDetails = this.getNotificationData_(msgPayload);
if (notificationDetails) {
const notificationTitle = notificationDetails.title || '';
return (self as any).registration.showNotification(
notificationTitle,
notificationDetails
);
} else if (this.bgMessageHandler_) {
return this.bgMessageHandler_(msgPayload);
}
}
);
event.waitUntil(handleMsgPromise);
}
/**
* @private
*/
onSubChange_(event) {
const promiseChain = this.getToken().then(token => {
if (!token) {
// We can't resubscribe if we don't have an FCM token for this scope.
throw this.errorFactory_.create(
Errors.codes.NO_FCM_TOKEN_FOR_RESUBSCRIBE
);
}
let tokenDetails = null;
const tokenManager = this.getTokenManager();
return tokenManager
.getTokenDetailsFromToken(token)
.then(details => {
tokenDetails = details;
if (!tokenDetails) {
throw this.errorFactory_.create(Errors.codes.INVALID_SAVED_TOKEN);
}
// Attempt to get a new subscription
return (self as any).registration.pushManager.subscribe(
FCMDetails.SUBSCRIPTION_OPTIONS
);
})
.then(newSubscription => {
// Send new subscription to FCM.
return tokenManager.subscribeToFCM(
tokenDetails.fcmSenderId,
newSubscription,
tokenDetails.fcmPushSet
);
})
.catch(err => {
// The best thing we can do is log this to the terminal so
// developers might notice the error.
return tokenManager.deleteToken(tokenDetails.fcmToken).then(() => {
throw this.errorFactory_.create(
Errors.codes.UNABLE_TO_RESUBSCRIBE,
{
message: err
}
);
});
});
});
event.waitUntil(promiseChain);
}
/**
* @private
*/
onNotificationClick_(event) {
if (
!(
event.notification &&
event.notification.data &&
event.notification.data[FCM_MSG]
)
) {
// Not an FCM notification, do nothing.
return;
}
// Prevent other listeners from receiving the event
event.stopImmediatePropagation();
event.notification.close();
const msgPayload = event.notification.data[FCM_MSG];
const clickAction = msgPayload['notification']['click_action'];
if (!clickAction) {
// Nothing to do.
return;
}
const promiseChain = this.getWindowClient_(clickAction)
.then(windowClient => {
if (!windowClient) {
// Unable to find window client so need to open one.
return (self as any).clients.openWindow(clickAction);
}
return windowClient;
})
.then(windowClient => {
if (!windowClient) {
// Window Client will not be returned if it's for a third party origin.
return;
}
// Delete notification data from payload before sending to the page.
const notificationData = msgPayload['notification'];
delete msgPayload['notification'];
const internalMsg = WorkerPageMessage.createNewMsg(
WorkerPageMessage.TYPES_OF_MSG.NOTIFICATION_CLICKED,
msgPayload
);
// Attempt to send a message to the client to handle the data
// Is affected by: https://github.com/slightlyoff/ServiceWorker/issues/728
return this.attemptToMessageClient_(windowClient, internalMsg);
});
event.waitUntil(promiseChain);
}
/**
* @private
* @param {Object} msgPayload
* @return {NotificationOptions|undefined}
*/
getNotificationData_(msgPayload) {
if (!msgPayload) {
return;
}
if (typeof msgPayload.notification !== 'object') {
return;
}
const notificationInformation = Object.assign({}, msgPayload.notification);
// Put the message payload under FCM_MSG name so we can identify the
// notification as being an FCM notification vs a notification from
// somewhere else (i.e. normal web push or developer generated
// notification).
notificationInformation['data'] = {
[FCM_MSG]: msgPayload
};
return notificationInformation;
}
/**
* Calling setBackgroundMessageHandler will opt in to some specific
* behaviours.
* 1.) If a notification doesn't need to be shown due to a window already
* being visible, then push messages will be sent to the page.
* 2.) If a notification needs to be shown, and the message contains no
* notification data this method will be called
* and the promise it returns will be passed to event.waitUntil.
* If you do not set this callback then all push messages will let and the
* developer can handle them in a their own 'push' event callback
* @export
* @param {function(Object)} callback The callback to be called when a push
* message is received and a notification must be shown. The callback will
* be given the data from the push message.
*/
setBackgroundMessageHandler(callback) {
if (callback && typeof callback !== 'function') {
throw this.errorFactory_.create(
Errors.codes.BG_HANDLER_FUNCTION_EXPECTED
);
}
this.bgMessageHandler_ = callback;
}
/**
* @private
* @param {string} url The URL to look for when focusing a client.
* @return {Object} Returns an existing window client or a newly opened
* WindowClient.
*/
getWindowClient_(url) {
// Use URL to normalize the URL when comparing to windowClients.
// This at least handles whether to include trailing slashes or not
const parsedURL = new URL(url).href;
return (self as any).clients
.matchAll({
type: 'window',
includeUncontrolled: true
})
.then(clientList => {
let suitableClient = null;
for (let i = 0; i < clientList.length; i++) {
const parsedClientUrl = new URL(clientList[i].url).href;
if (parsedClientUrl === parsedURL) {
suitableClient = clientList[i];
break;
}
}
if (suitableClient) {
suitableClient.focus();
return suitableClient;
}
});
}
/**
* This message will attempt to send the message to a window client.
* @private
* @param {Object} client The WindowClient to send the message to.
* @param {Object} message The message to send to the client.
* @returns {Promise} Returns a promise that resolves after sending the
* message. This does not guarantee that the message was successfully
* received.
*/
attemptToMessageClient_(client, message) {
return new Promise((resolve, reject) => {
if (!client) {
return reject(
this.errorFactory_.create(Errors.codes.NO_WINDOW_CLIENT_TO_MSG)
);
}
client.postMessage(message);
resolve();
});
}
/**
* @private
* @returns {Promise<boolean>} If there is currently a visible WindowClient,
* this method will resolve to true, otherwise false.
*/
hasVisibleClients_() {
return (self as any).clients
.matchAll({
type: 'window',
includeUncontrolled: true
})
.then(clientList => {
return clientList.some(client => client.visibilityState === 'visible');
});
}
/**
* @private
* @param {Object} msgPayload The data from the push event that should be sent
* to all available pages.
* @returns {Promise} Returns a promise that resolves once the message
* has been sent to all WindowClients.
*/
sendMessageToWindowClients_(msgPayload) {
return (self as any).clients
.matchAll({
type: 'window',
includeUncontrolled: true
})
.then(clientList => {
const internalMsg = WorkerPageMessage.createNewMsg(
WorkerPageMessage.TYPES_OF_MSG.PUSH_MSG_RECEIVED,
msgPayload
);
return Promise.all(
clientList.map(client => {
return this.attemptToMessageClient_(client, internalMsg);
})
);
});
}
/**
* This will register the default service worker and return the registration.
* @private
* @return {Promise<!ServiceWorkerRegistration>} The service worker
* registration to be used for the push service.
*/
getSWRegistration_() {
return Promise.resolve((self as any).registration);
}
}