@depup/firebase__messaging 0.12.24-depup.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -0
- package/changes.json +14 -0
- package/dist/esm/index.esm.js +1270 -0
- package/dist/esm/index.esm.js.map +1 -0
- package/dist/esm/index.sw.esm.js +1278 -0
- package/dist/esm/index.sw.esm.js.map +1 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/src/api/deleteToken.d.ts +18 -0
- package/dist/esm/src/api/getToken.d.ts +19 -0
- package/dist/esm/src/api/isSupported.d.ts +30 -0
- package/dist/esm/src/api/onBackgroundMessage.d.ts +19 -0
- package/dist/esm/src/api/onMessage.d.ts +19 -0
- package/dist/esm/src/api/setDeliveryMetricsExportedToBigQueryEnabled.d.ts +18 -0
- package/dist/esm/src/api.d.ts +101 -0
- package/dist/esm/src/helpers/array-base64-translator.d.ts +18 -0
- package/dist/esm/src/helpers/externalizePayload.d.ts +19 -0
- package/dist/esm/src/helpers/extract-app-config.d.ts +19 -0
- package/dist/esm/src/helpers/is-console-message.d.ts +18 -0
- package/dist/esm/src/helpers/logToFirelog.d.ts +30 -0
- package/dist/esm/src/helpers/logToScion.d.ts +19 -0
- package/dist/esm/src/helpers/migrate-old-database.d.ts +51 -0
- package/dist/esm/src/helpers/register.d.ts +23 -0
- package/dist/esm/src/helpers/registerDefaultSw.d.ts +18 -0
- package/dist/esm/src/helpers/sleep.d.ts +18 -0
- package/dist/esm/src/helpers/updateSwReg.d.ts +18 -0
- package/dist/esm/src/helpers/updateVapidKey.d.ts +18 -0
- package/dist/esm/src/index.d.ts +32 -0
- package/dist/esm/src/index.sw.d.ts +26 -0
- package/dist/esm/src/interfaces/app-config.d.ts +24 -0
- package/dist/esm/src/interfaces/internal-dependencies.d.ts +27 -0
- package/dist/esm/src/interfaces/internal-message-payload.d.ts +65 -0
- package/dist/esm/src/interfaces/logging-types.d.ts +60 -0
- package/dist/esm/src/interfaces/public-types.d.ts +144 -0
- package/dist/esm/src/interfaces/token-details.d.ts +32 -0
- package/dist/esm/src/internals/idb-manager.d.ts +27 -0
- package/dist/esm/src/internals/requests.d.ts +35 -0
- package/dist/esm/src/internals/token-manager.d.ts +23 -0
- package/dist/esm/src/listeners/sw-listeners.d.ts +21 -0
- package/dist/esm/src/listeners/window-listener.d.ts +18 -0
- package/dist/esm/src/messaging-service.d.ts +36 -0
- package/dist/esm/src/testing/compare-headers.d.ts +22 -0
- package/dist/esm/src/testing/fakes/firebase-dependencies.d.ts +25 -0
- package/dist/esm/src/testing/fakes/logging-object.d.ts +18 -0
- package/dist/esm/src/testing/fakes/messaging-service.d.ts +18 -0
- package/dist/esm/src/testing/fakes/service-worker.d.ts +83 -0
- package/dist/esm/src/testing/fakes/token-details.d.ts +18 -0
- package/dist/esm/src/testing/setup.d.ts +17 -0
- package/dist/esm/src/testing/sinon-types.d.ts +19 -0
- package/dist/esm/src/util/constants.d.ts +40 -0
- package/dist/esm/src/util/errors.d.ts +57 -0
- package/dist/esm/src/util/sw-types.d.ts +90 -0
- package/dist/index-public.d.ts +204 -0
- package/dist/index.cjs.js +1278 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.sw.cjs +1285 -0
- package/dist/index.sw.cjs.map +1 -0
- package/dist/internal.d.ts +220 -0
- package/dist/private.d.ts +217 -0
- package/dist/src/api/deleteToken.d.ts +18 -0
- package/dist/src/api/getToken.d.ts +19 -0
- package/dist/src/api/isSupported.d.ts +30 -0
- package/dist/src/api/onBackgroundMessage.d.ts +19 -0
- package/dist/src/api/onMessage.d.ts +19 -0
- package/dist/src/api/setDeliveryMetricsExportedToBigQueryEnabled.d.ts +18 -0
- package/dist/src/api.d.ts +101 -0
- package/dist/src/helpers/array-base64-translator.d.ts +18 -0
- package/dist/src/helpers/externalizePayload.d.ts +19 -0
- package/dist/src/helpers/extract-app-config.d.ts +19 -0
- package/dist/src/helpers/is-console-message.d.ts +18 -0
- package/dist/src/helpers/logToFirelog.d.ts +30 -0
- package/dist/src/helpers/logToScion.d.ts +19 -0
- package/dist/src/helpers/migrate-old-database.d.ts +51 -0
- package/dist/src/helpers/register.d.ts +23 -0
- package/dist/src/helpers/registerDefaultSw.d.ts +18 -0
- package/dist/src/helpers/sleep.d.ts +18 -0
- package/dist/src/helpers/updateSwReg.d.ts +18 -0
- package/dist/src/helpers/updateVapidKey.d.ts +18 -0
- package/dist/src/index.d.ts +32 -0
- package/dist/src/index.sw.d.ts +26 -0
- package/dist/src/interfaces/app-config.d.ts +24 -0
- package/dist/src/interfaces/internal-dependencies.d.ts +27 -0
- package/dist/src/interfaces/internal-message-payload.d.ts +65 -0
- package/dist/src/interfaces/logging-types.d.ts +60 -0
- package/dist/src/interfaces/public-types.d.ts +144 -0
- package/dist/src/interfaces/token-details.d.ts +32 -0
- package/dist/src/internals/idb-manager.d.ts +27 -0
- package/dist/src/internals/requests.d.ts +35 -0
- package/dist/src/internals/token-manager.d.ts +23 -0
- package/dist/src/listeners/sw-listeners.d.ts +21 -0
- package/dist/src/listeners/window-listener.d.ts +18 -0
- package/dist/src/messaging-service.d.ts +36 -0
- package/dist/src/testing/compare-headers.d.ts +22 -0
- package/dist/src/testing/fakes/firebase-dependencies.d.ts +25 -0
- package/dist/src/testing/fakes/logging-object.d.ts +18 -0
- package/dist/src/testing/fakes/messaging-service.d.ts +18 -0
- package/dist/src/testing/fakes/service-worker.d.ts +83 -0
- package/dist/src/testing/fakes/token-details.d.ts +18 -0
- package/dist/src/testing/setup.d.ts +17 -0
- package/dist/src/testing/sinon-types.d.ts +19 -0
- package/dist/src/tsdoc-metadata.json +11 -0
- package/dist/src/util/constants.d.ts +40 -0
- package/dist/src/util/errors.d.ts +57 -0
- package/dist/src/util/sw-types.d.ts +90 -0
- package/dist/sw/index-public.d.ts +166 -0
- package/dist/sw/internal.d.ts +180 -0
- package/dist/sw/private.d.ts +177 -0
- package/package.json +103 -0
- package/sw/package.json +8 -0
|
@@ -0,0 +1,1285 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
require('@firebase/installations');
|
|
6
|
+
var component = require('@firebase/component');
|
|
7
|
+
var idb = require('idb');
|
|
8
|
+
var util = require('@firebase/util');
|
|
9
|
+
var app = require('@firebase/app');
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @license
|
|
13
|
+
* Copyright 2019 Google LLC
|
|
14
|
+
*
|
|
15
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
16
|
+
* you may not use this file except in compliance with the License.
|
|
17
|
+
* You may obtain a copy of the License at
|
|
18
|
+
*
|
|
19
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
20
|
+
*
|
|
21
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
22
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
23
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
24
|
+
* See the License for the specific language governing permissions and
|
|
25
|
+
* limitations under the License.
|
|
26
|
+
*/
|
|
27
|
+
const DEFAULT_VAPID_KEY = 'BDOU99-h67HcA6JeFXHbSNMu7e2yNNu3RzoMj8TM4W88jITfq7ZmPvIM1Iv-4_l2LxQcYwhqby2xGpWwzjfAnG4';
|
|
28
|
+
const ENDPOINT = 'https://fcmregistrations.googleapis.com/v1';
|
|
29
|
+
/** Key of FCM Payload in Notification's data field. */
|
|
30
|
+
const FCM_MSG = 'FCM_MSG';
|
|
31
|
+
const CONSOLE_CAMPAIGN_ID = 'google.c.a.c_id';
|
|
32
|
+
// Defined as in proto/messaging_event.proto. Neglecting fields that are supported.
|
|
33
|
+
const SDK_PLATFORM_WEB = 3;
|
|
34
|
+
const EVENT_MESSAGE_DELIVERED = 1;
|
|
35
|
+
var MessageType$1;
|
|
36
|
+
(function (MessageType) {
|
|
37
|
+
MessageType[MessageType["DATA_MESSAGE"] = 1] = "DATA_MESSAGE";
|
|
38
|
+
MessageType[MessageType["DISPLAY_NOTIFICATION"] = 3] = "DISPLAY_NOTIFICATION";
|
|
39
|
+
})(MessageType$1 || (MessageType$1 = {}));
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @license
|
|
43
|
+
* Copyright 2018 Google LLC
|
|
44
|
+
*
|
|
45
|
+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
|
46
|
+
* in compliance with the License. You may obtain a copy of the License at
|
|
47
|
+
*
|
|
48
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
49
|
+
*
|
|
50
|
+
* Unless required by applicable law or agreed to in writing, software distributed under the License
|
|
51
|
+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
|
52
|
+
* or implied. See the License for the specific language governing permissions and limitations under
|
|
53
|
+
* the License.
|
|
54
|
+
*/
|
|
55
|
+
var MessageType;
|
|
56
|
+
(function (MessageType) {
|
|
57
|
+
MessageType["PUSH_RECEIVED"] = "push-received";
|
|
58
|
+
MessageType["NOTIFICATION_CLICKED"] = "notification-clicked";
|
|
59
|
+
})(MessageType || (MessageType = {}));
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* @license
|
|
63
|
+
* Copyright 2017 Google LLC
|
|
64
|
+
*
|
|
65
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
66
|
+
* you may not use this file except in compliance with the License.
|
|
67
|
+
* You may obtain a copy of the License at
|
|
68
|
+
*
|
|
69
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
70
|
+
*
|
|
71
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
72
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
73
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
74
|
+
* See the License for the specific language governing permissions and
|
|
75
|
+
* limitations under the License.
|
|
76
|
+
*/
|
|
77
|
+
function arrayToBase64(array) {
|
|
78
|
+
const uint8Array = new Uint8Array(array);
|
|
79
|
+
const base64String = btoa(String.fromCharCode(...uint8Array));
|
|
80
|
+
return base64String.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
|
|
81
|
+
}
|
|
82
|
+
function base64ToArray(base64String) {
|
|
83
|
+
const padding = '='.repeat((4 - (base64String.length % 4)) % 4);
|
|
84
|
+
const base64 = (base64String + padding)
|
|
85
|
+
.replace(/\-/g, '+')
|
|
86
|
+
.replace(/_/g, '/');
|
|
87
|
+
const rawData = atob(base64);
|
|
88
|
+
const outputArray = new Uint8Array(rawData.length);
|
|
89
|
+
for (let i = 0; i < rawData.length; ++i) {
|
|
90
|
+
outputArray[i] = rawData.charCodeAt(i);
|
|
91
|
+
}
|
|
92
|
+
return outputArray;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* @license
|
|
97
|
+
* Copyright 2019 Google LLC
|
|
98
|
+
*
|
|
99
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
100
|
+
* you may not use this file except in compliance with the License.
|
|
101
|
+
* You may obtain a copy of the License at
|
|
102
|
+
*
|
|
103
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
104
|
+
*
|
|
105
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
106
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
107
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
108
|
+
* See the License for the specific language governing permissions and
|
|
109
|
+
* limitations under the License.
|
|
110
|
+
*/
|
|
111
|
+
const OLD_DB_NAME = 'fcm_token_details_db';
|
|
112
|
+
/**
|
|
113
|
+
* The last DB version of 'fcm_token_details_db' was 4. This is one higher, so that the upgrade
|
|
114
|
+
* callback is called for all versions of the old DB.
|
|
115
|
+
*/
|
|
116
|
+
const OLD_DB_VERSION = 5;
|
|
117
|
+
const OLD_OBJECT_STORE_NAME = 'fcm_token_object_Store';
|
|
118
|
+
async function migrateOldDatabase(senderId) {
|
|
119
|
+
if ('databases' in indexedDB) {
|
|
120
|
+
// indexedDb.databases() is an IndexedDB v3 API and does not exist in all browsers. TODO: Remove
|
|
121
|
+
// typecast when it lands in TS types.
|
|
122
|
+
const databases = await indexedDB.databases();
|
|
123
|
+
const dbNames = databases.map(db => db.name);
|
|
124
|
+
if (!dbNames.includes(OLD_DB_NAME)) {
|
|
125
|
+
// old DB didn't exist, no need to open.
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
let tokenDetails = null;
|
|
130
|
+
const db = await idb.openDB(OLD_DB_NAME, OLD_DB_VERSION, {
|
|
131
|
+
upgrade: async (db, oldVersion, newVersion, upgradeTransaction) => {
|
|
132
|
+
if (oldVersion < 2) {
|
|
133
|
+
// Database too old, skip migration.
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (!db.objectStoreNames.contains(OLD_OBJECT_STORE_NAME)) {
|
|
137
|
+
// Database did not exist. Nothing to do.
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const objectStore = upgradeTransaction.objectStore(OLD_OBJECT_STORE_NAME);
|
|
141
|
+
const value = await objectStore.index('fcmSenderId').get(senderId);
|
|
142
|
+
await objectStore.clear();
|
|
143
|
+
if (!value) {
|
|
144
|
+
// No entry in the database, nothing to migrate.
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
if (oldVersion === 2) {
|
|
148
|
+
const oldDetails = value;
|
|
149
|
+
if (!oldDetails.auth || !oldDetails.p256dh || !oldDetails.endpoint) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
tokenDetails = {
|
|
153
|
+
token: oldDetails.fcmToken,
|
|
154
|
+
createTime: oldDetails.createTime ?? Date.now(),
|
|
155
|
+
subscriptionOptions: {
|
|
156
|
+
auth: oldDetails.auth,
|
|
157
|
+
p256dh: oldDetails.p256dh,
|
|
158
|
+
endpoint: oldDetails.endpoint,
|
|
159
|
+
swScope: oldDetails.swScope,
|
|
160
|
+
vapidKey: typeof oldDetails.vapidKey === 'string'
|
|
161
|
+
? oldDetails.vapidKey
|
|
162
|
+
: arrayToBase64(oldDetails.vapidKey)
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
else if (oldVersion === 3) {
|
|
167
|
+
const oldDetails = value;
|
|
168
|
+
tokenDetails = {
|
|
169
|
+
token: oldDetails.fcmToken,
|
|
170
|
+
createTime: oldDetails.createTime,
|
|
171
|
+
subscriptionOptions: {
|
|
172
|
+
auth: arrayToBase64(oldDetails.auth),
|
|
173
|
+
p256dh: arrayToBase64(oldDetails.p256dh),
|
|
174
|
+
endpoint: oldDetails.endpoint,
|
|
175
|
+
swScope: oldDetails.swScope,
|
|
176
|
+
vapidKey: arrayToBase64(oldDetails.vapidKey)
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
else if (oldVersion === 4) {
|
|
181
|
+
const oldDetails = value;
|
|
182
|
+
tokenDetails = {
|
|
183
|
+
token: oldDetails.fcmToken,
|
|
184
|
+
createTime: oldDetails.createTime,
|
|
185
|
+
subscriptionOptions: {
|
|
186
|
+
auth: arrayToBase64(oldDetails.auth),
|
|
187
|
+
p256dh: arrayToBase64(oldDetails.p256dh),
|
|
188
|
+
endpoint: oldDetails.endpoint,
|
|
189
|
+
swScope: oldDetails.swScope,
|
|
190
|
+
vapidKey: arrayToBase64(oldDetails.vapidKey)
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
db.close();
|
|
197
|
+
// Delete all old databases.
|
|
198
|
+
await idb.deleteDB(OLD_DB_NAME);
|
|
199
|
+
await idb.deleteDB('fcm_vapid_details_db');
|
|
200
|
+
await idb.deleteDB('undefined');
|
|
201
|
+
return checkTokenDetails(tokenDetails) ? tokenDetails : null;
|
|
202
|
+
}
|
|
203
|
+
function checkTokenDetails(tokenDetails) {
|
|
204
|
+
if (!tokenDetails || !tokenDetails.subscriptionOptions) {
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
const { subscriptionOptions } = tokenDetails;
|
|
208
|
+
return (typeof tokenDetails.createTime === 'number' &&
|
|
209
|
+
tokenDetails.createTime > 0 &&
|
|
210
|
+
typeof tokenDetails.token === 'string' &&
|
|
211
|
+
tokenDetails.token.length > 0 &&
|
|
212
|
+
typeof subscriptionOptions.auth === 'string' &&
|
|
213
|
+
subscriptionOptions.auth.length > 0 &&
|
|
214
|
+
typeof subscriptionOptions.p256dh === 'string' &&
|
|
215
|
+
subscriptionOptions.p256dh.length > 0 &&
|
|
216
|
+
typeof subscriptionOptions.endpoint === 'string' &&
|
|
217
|
+
subscriptionOptions.endpoint.length > 0 &&
|
|
218
|
+
typeof subscriptionOptions.swScope === 'string' &&
|
|
219
|
+
subscriptionOptions.swScope.length > 0 &&
|
|
220
|
+
typeof subscriptionOptions.vapidKey === 'string' &&
|
|
221
|
+
subscriptionOptions.vapidKey.length > 0);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* @license
|
|
226
|
+
* Copyright 2019 Google LLC
|
|
227
|
+
*
|
|
228
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
229
|
+
* you may not use this file except in compliance with the License.
|
|
230
|
+
* You may obtain a copy of the License at
|
|
231
|
+
*
|
|
232
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
233
|
+
*
|
|
234
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
235
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
236
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
237
|
+
* See the License for the specific language governing permissions and
|
|
238
|
+
* limitations under the License.
|
|
239
|
+
*/
|
|
240
|
+
// Exported for tests.
|
|
241
|
+
const DATABASE_NAME = 'firebase-messaging-database';
|
|
242
|
+
const DATABASE_VERSION = 1;
|
|
243
|
+
const OBJECT_STORE_NAME = 'firebase-messaging-store';
|
|
244
|
+
let dbPromise = null;
|
|
245
|
+
function getDbPromise() {
|
|
246
|
+
if (!dbPromise) {
|
|
247
|
+
dbPromise = idb.openDB(DATABASE_NAME, DATABASE_VERSION, {
|
|
248
|
+
upgrade: (upgradeDb, oldVersion) => {
|
|
249
|
+
// We don't use 'break' in this switch statement, the fall-through behavior is what we want,
|
|
250
|
+
// because if there are multiple versions between the old version and the current version, we
|
|
251
|
+
// want ALL the migrations that correspond to those versions to run, not only the last one.
|
|
252
|
+
// eslint-disable-next-line default-case
|
|
253
|
+
switch (oldVersion) {
|
|
254
|
+
case 0:
|
|
255
|
+
upgradeDb.createObjectStore(OBJECT_STORE_NAME);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
return dbPromise;
|
|
261
|
+
}
|
|
262
|
+
/** Gets record(s) from the objectStore that match the given key. */
|
|
263
|
+
async function dbGet(firebaseDependencies) {
|
|
264
|
+
const key = getKey(firebaseDependencies);
|
|
265
|
+
const db = await getDbPromise();
|
|
266
|
+
const tokenDetails = (await db
|
|
267
|
+
.transaction(OBJECT_STORE_NAME)
|
|
268
|
+
.objectStore(OBJECT_STORE_NAME)
|
|
269
|
+
.get(key));
|
|
270
|
+
if (tokenDetails) {
|
|
271
|
+
return tokenDetails;
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
274
|
+
// Check if there is a tokenDetails object in the old DB.
|
|
275
|
+
const oldTokenDetails = await migrateOldDatabase(firebaseDependencies.appConfig.senderId);
|
|
276
|
+
if (oldTokenDetails) {
|
|
277
|
+
await dbSet(firebaseDependencies, oldTokenDetails);
|
|
278
|
+
return oldTokenDetails;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
/** Assigns or overwrites the record for the given key with the given value. */
|
|
283
|
+
async function dbSet(firebaseDependencies, tokenDetails) {
|
|
284
|
+
const key = getKey(firebaseDependencies);
|
|
285
|
+
const db = await getDbPromise();
|
|
286
|
+
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
|
|
287
|
+
await tx.objectStore(OBJECT_STORE_NAME).put(tokenDetails, key);
|
|
288
|
+
await tx.done;
|
|
289
|
+
return tokenDetails;
|
|
290
|
+
}
|
|
291
|
+
/** Removes record(s) from the objectStore that match the given key. */
|
|
292
|
+
async function dbRemove(firebaseDependencies) {
|
|
293
|
+
const key = getKey(firebaseDependencies);
|
|
294
|
+
const db = await getDbPromise();
|
|
295
|
+
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
|
|
296
|
+
await tx.objectStore(OBJECT_STORE_NAME).delete(key);
|
|
297
|
+
await tx.done;
|
|
298
|
+
}
|
|
299
|
+
function getKey({ appConfig }) {
|
|
300
|
+
return appConfig.appId;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* @license
|
|
305
|
+
* Copyright 2017 Google LLC
|
|
306
|
+
*
|
|
307
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
308
|
+
* you may not use this file except in compliance with the License.
|
|
309
|
+
* You may obtain a copy of the License at
|
|
310
|
+
*
|
|
311
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
312
|
+
*
|
|
313
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
314
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
315
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
316
|
+
* See the License for the specific language governing permissions and
|
|
317
|
+
* limitations under the License.
|
|
318
|
+
*/
|
|
319
|
+
const ERROR_MAP = {
|
|
320
|
+
["missing-app-config-values" /* ErrorCode.MISSING_APP_CONFIG_VALUES */]: 'Missing App configuration value: "{$valueName}"',
|
|
321
|
+
["only-available-in-window" /* ErrorCode.AVAILABLE_IN_WINDOW */]: 'This method is available in a Window context.',
|
|
322
|
+
["only-available-in-sw" /* ErrorCode.AVAILABLE_IN_SW */]: 'This method is available in a service worker context.',
|
|
323
|
+
["permission-default" /* ErrorCode.PERMISSION_DEFAULT */]: 'The notification permission was not granted and dismissed instead.',
|
|
324
|
+
["permission-blocked" /* ErrorCode.PERMISSION_BLOCKED */]: 'The notification permission was not granted and blocked instead.',
|
|
325
|
+
["unsupported-browser" /* ErrorCode.UNSUPPORTED_BROWSER */]: "This browser doesn't support the API's required to use the Firebase SDK.",
|
|
326
|
+
["indexed-db-unsupported" /* ErrorCode.INDEXED_DB_UNSUPPORTED */]: "This browser doesn't support indexedDb.open() (ex. Safari iFrame, Firefox Private Browsing, etc)",
|
|
327
|
+
["failed-service-worker-registration" /* ErrorCode.FAILED_DEFAULT_REGISTRATION */]: 'We are unable to register the default service worker. {$browserErrorMessage}',
|
|
328
|
+
["token-subscribe-failed" /* ErrorCode.TOKEN_SUBSCRIBE_FAILED */]: 'A problem occurred while subscribing the user to FCM: {$errorInfo}',
|
|
329
|
+
["token-subscribe-no-token" /* ErrorCode.TOKEN_SUBSCRIBE_NO_TOKEN */]: 'FCM returned no token when subscribing the user to push.',
|
|
330
|
+
["token-unsubscribe-failed" /* ErrorCode.TOKEN_UNSUBSCRIBE_FAILED */]: 'A problem occurred while unsubscribing the ' +
|
|
331
|
+
'user from FCM: {$errorInfo}',
|
|
332
|
+
["token-update-failed" /* ErrorCode.TOKEN_UPDATE_FAILED */]: 'A problem occurred while updating the user from FCM: {$errorInfo}',
|
|
333
|
+
["token-update-no-token" /* ErrorCode.TOKEN_UPDATE_NO_TOKEN */]: 'FCM returned no token when updating the user to push.',
|
|
334
|
+
["use-sw-after-get-token" /* ErrorCode.USE_SW_AFTER_GET_TOKEN */]: 'The useServiceWorker() method may only be called once and must be ' +
|
|
335
|
+
'called before calling getToken() to ensure your service worker is used.',
|
|
336
|
+
["invalid-sw-registration" /* ErrorCode.INVALID_SW_REGISTRATION */]: 'The input to useServiceWorker() must be a ServiceWorkerRegistration.',
|
|
337
|
+
["invalid-bg-handler" /* ErrorCode.INVALID_BG_HANDLER */]: 'The input to setBackgroundMessageHandler() must be a function.',
|
|
338
|
+
["invalid-vapid-key" /* ErrorCode.INVALID_VAPID_KEY */]: 'The public VAPID key must be a string.',
|
|
339
|
+
["use-vapid-key-after-get-token" /* ErrorCode.USE_VAPID_KEY_AFTER_GET_TOKEN */]: 'The usePublicVapidKey() method may only be called once and must be ' +
|
|
340
|
+
'called before calling getToken() to ensure your VAPID key is used.'
|
|
341
|
+
};
|
|
342
|
+
const ERROR_FACTORY = new util.ErrorFactory('messaging', 'Messaging', ERROR_MAP);
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* @license
|
|
346
|
+
* Copyright 2019 Google LLC
|
|
347
|
+
*
|
|
348
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
349
|
+
* you may not use this file except in compliance with the License.
|
|
350
|
+
* You may obtain a copy of the License at
|
|
351
|
+
*
|
|
352
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
353
|
+
*
|
|
354
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
355
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
356
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
357
|
+
* See the License for the specific language governing permissions and
|
|
358
|
+
* limitations under the License.
|
|
359
|
+
*/
|
|
360
|
+
async function requestGetToken(firebaseDependencies, subscriptionOptions) {
|
|
361
|
+
const headers = await getHeaders(firebaseDependencies);
|
|
362
|
+
const body = getBody(subscriptionOptions);
|
|
363
|
+
const subscribeOptions = {
|
|
364
|
+
method: 'POST',
|
|
365
|
+
headers,
|
|
366
|
+
body: JSON.stringify(body)
|
|
367
|
+
};
|
|
368
|
+
let responseData;
|
|
369
|
+
try {
|
|
370
|
+
const response = await fetch(getEndpoint(firebaseDependencies.appConfig), subscribeOptions);
|
|
371
|
+
responseData = await response.json();
|
|
372
|
+
}
|
|
373
|
+
catch (err) {
|
|
374
|
+
throw ERROR_FACTORY.create("token-subscribe-failed" /* ErrorCode.TOKEN_SUBSCRIBE_FAILED */, {
|
|
375
|
+
errorInfo: err?.toString()
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
if (responseData.error) {
|
|
379
|
+
const message = responseData.error.message;
|
|
380
|
+
throw ERROR_FACTORY.create("token-subscribe-failed" /* ErrorCode.TOKEN_SUBSCRIBE_FAILED */, {
|
|
381
|
+
errorInfo: message
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
if (!responseData.token) {
|
|
385
|
+
throw ERROR_FACTORY.create("token-subscribe-no-token" /* ErrorCode.TOKEN_SUBSCRIBE_NO_TOKEN */);
|
|
386
|
+
}
|
|
387
|
+
return responseData.token;
|
|
388
|
+
}
|
|
389
|
+
async function requestUpdateToken(firebaseDependencies, tokenDetails) {
|
|
390
|
+
const headers = await getHeaders(firebaseDependencies);
|
|
391
|
+
const body = getBody(tokenDetails.subscriptionOptions);
|
|
392
|
+
const updateOptions = {
|
|
393
|
+
method: 'PATCH',
|
|
394
|
+
headers,
|
|
395
|
+
body: JSON.stringify(body)
|
|
396
|
+
};
|
|
397
|
+
let responseData;
|
|
398
|
+
try {
|
|
399
|
+
const response = await fetch(`${getEndpoint(firebaseDependencies.appConfig)}/${tokenDetails.token}`, updateOptions);
|
|
400
|
+
responseData = await response.json();
|
|
401
|
+
}
|
|
402
|
+
catch (err) {
|
|
403
|
+
throw ERROR_FACTORY.create("token-update-failed" /* ErrorCode.TOKEN_UPDATE_FAILED */, {
|
|
404
|
+
errorInfo: err?.toString()
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
if (responseData.error) {
|
|
408
|
+
const message = responseData.error.message;
|
|
409
|
+
throw ERROR_FACTORY.create("token-update-failed" /* ErrorCode.TOKEN_UPDATE_FAILED */, {
|
|
410
|
+
errorInfo: message
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
if (!responseData.token) {
|
|
414
|
+
throw ERROR_FACTORY.create("token-update-no-token" /* ErrorCode.TOKEN_UPDATE_NO_TOKEN */);
|
|
415
|
+
}
|
|
416
|
+
return responseData.token;
|
|
417
|
+
}
|
|
418
|
+
async function requestDeleteToken(firebaseDependencies, token) {
|
|
419
|
+
const headers = await getHeaders(firebaseDependencies);
|
|
420
|
+
const unsubscribeOptions = {
|
|
421
|
+
method: 'DELETE',
|
|
422
|
+
headers
|
|
423
|
+
};
|
|
424
|
+
try {
|
|
425
|
+
const response = await fetch(`${getEndpoint(firebaseDependencies.appConfig)}/${token}`, unsubscribeOptions);
|
|
426
|
+
const responseData = await response.json();
|
|
427
|
+
if (responseData.error) {
|
|
428
|
+
const message = responseData.error.message;
|
|
429
|
+
throw ERROR_FACTORY.create("token-unsubscribe-failed" /* ErrorCode.TOKEN_UNSUBSCRIBE_FAILED */, {
|
|
430
|
+
errorInfo: message
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
catch (err) {
|
|
435
|
+
throw ERROR_FACTORY.create("token-unsubscribe-failed" /* ErrorCode.TOKEN_UNSUBSCRIBE_FAILED */, {
|
|
436
|
+
errorInfo: err?.toString()
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
function getEndpoint({ projectId }) {
|
|
441
|
+
return `${ENDPOINT}/projects/${projectId}/registrations`;
|
|
442
|
+
}
|
|
443
|
+
async function getHeaders({ appConfig, installations }) {
|
|
444
|
+
const authToken = await installations.getToken();
|
|
445
|
+
return new Headers({
|
|
446
|
+
'Content-Type': 'application/json',
|
|
447
|
+
Accept: 'application/json',
|
|
448
|
+
'x-goog-api-key': appConfig.apiKey,
|
|
449
|
+
'x-goog-firebase-installations-auth': `FIS ${authToken}`
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
function getBody({ p256dh, auth, endpoint, vapidKey }) {
|
|
453
|
+
const body = {
|
|
454
|
+
web: {
|
|
455
|
+
endpoint,
|
|
456
|
+
auth,
|
|
457
|
+
p256dh
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
if (vapidKey !== DEFAULT_VAPID_KEY) {
|
|
461
|
+
body.web.applicationPubKey = vapidKey;
|
|
462
|
+
}
|
|
463
|
+
return body;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* @license
|
|
468
|
+
* Copyright 2019 Google LLC
|
|
469
|
+
*
|
|
470
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
471
|
+
* you may not use this file except in compliance with the License.
|
|
472
|
+
* You may obtain a copy of the License at
|
|
473
|
+
*
|
|
474
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
475
|
+
*
|
|
476
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
477
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
478
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
479
|
+
* See the License for the specific language governing permissions and
|
|
480
|
+
* limitations under the License.
|
|
481
|
+
*/
|
|
482
|
+
// UpdateRegistration will be called once every week.
|
|
483
|
+
const TOKEN_EXPIRATION_MS = 7 * 24 * 60 * 60 * 1000; // 7 days
|
|
484
|
+
async function getTokenInternal(messaging) {
|
|
485
|
+
const pushSubscription = await getPushSubscription(messaging.swRegistration, messaging.vapidKey);
|
|
486
|
+
const subscriptionOptions = {
|
|
487
|
+
vapidKey: messaging.vapidKey,
|
|
488
|
+
swScope: messaging.swRegistration.scope,
|
|
489
|
+
endpoint: pushSubscription.endpoint,
|
|
490
|
+
auth: arrayToBase64(pushSubscription.getKey('auth')),
|
|
491
|
+
p256dh: arrayToBase64(pushSubscription.getKey('p256dh'))
|
|
492
|
+
};
|
|
493
|
+
const tokenDetails = await dbGet(messaging.firebaseDependencies);
|
|
494
|
+
if (!tokenDetails) {
|
|
495
|
+
// No token, get a new one.
|
|
496
|
+
return getNewToken(messaging.firebaseDependencies, subscriptionOptions);
|
|
497
|
+
}
|
|
498
|
+
else if (!isTokenValid(tokenDetails.subscriptionOptions, subscriptionOptions)) {
|
|
499
|
+
// Invalid token, get a new one.
|
|
500
|
+
try {
|
|
501
|
+
await requestDeleteToken(messaging.firebaseDependencies, tokenDetails.token);
|
|
502
|
+
}
|
|
503
|
+
catch (e) {
|
|
504
|
+
// Suppress errors because of #2364
|
|
505
|
+
console.warn(e);
|
|
506
|
+
}
|
|
507
|
+
return getNewToken(messaging.firebaseDependencies, subscriptionOptions);
|
|
508
|
+
}
|
|
509
|
+
else if (Date.now() >= tokenDetails.createTime + TOKEN_EXPIRATION_MS) {
|
|
510
|
+
// Weekly token refresh
|
|
511
|
+
return updateToken(messaging, {
|
|
512
|
+
token: tokenDetails.token,
|
|
513
|
+
createTime: Date.now(),
|
|
514
|
+
subscriptionOptions
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
else {
|
|
518
|
+
// Valid token, nothing to do.
|
|
519
|
+
return tokenDetails.token;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* This method deletes the token from the database, unsubscribes the token from FCM, and unregisters
|
|
524
|
+
* the push subscription if it exists.
|
|
525
|
+
*/
|
|
526
|
+
async function deleteTokenInternal(messaging) {
|
|
527
|
+
const tokenDetails = await dbGet(messaging.firebaseDependencies);
|
|
528
|
+
if (tokenDetails) {
|
|
529
|
+
await requestDeleteToken(messaging.firebaseDependencies, tokenDetails.token);
|
|
530
|
+
await dbRemove(messaging.firebaseDependencies);
|
|
531
|
+
}
|
|
532
|
+
// Unsubscribe from the push subscription.
|
|
533
|
+
const pushSubscription = await messaging.swRegistration.pushManager.getSubscription();
|
|
534
|
+
if (pushSubscription) {
|
|
535
|
+
return pushSubscription.unsubscribe();
|
|
536
|
+
}
|
|
537
|
+
// If there's no SW, consider it a success.
|
|
538
|
+
return true;
|
|
539
|
+
}
|
|
540
|
+
async function updateToken(messaging, tokenDetails) {
|
|
541
|
+
try {
|
|
542
|
+
const updatedToken = await requestUpdateToken(messaging.firebaseDependencies, tokenDetails);
|
|
543
|
+
const updatedTokenDetails = {
|
|
544
|
+
...tokenDetails,
|
|
545
|
+
token: updatedToken,
|
|
546
|
+
createTime: Date.now()
|
|
547
|
+
};
|
|
548
|
+
await dbSet(messaging.firebaseDependencies, updatedTokenDetails);
|
|
549
|
+
return updatedToken;
|
|
550
|
+
}
|
|
551
|
+
catch (e) {
|
|
552
|
+
throw e;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
async function getNewToken(firebaseDependencies, subscriptionOptions) {
|
|
556
|
+
const token = await requestGetToken(firebaseDependencies, subscriptionOptions);
|
|
557
|
+
const tokenDetails = {
|
|
558
|
+
token,
|
|
559
|
+
createTime: Date.now(),
|
|
560
|
+
subscriptionOptions
|
|
561
|
+
};
|
|
562
|
+
await dbSet(firebaseDependencies, tokenDetails);
|
|
563
|
+
return tokenDetails.token;
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* Gets a PushSubscription for the current user.
|
|
567
|
+
*/
|
|
568
|
+
async function getPushSubscription(swRegistration, vapidKey) {
|
|
569
|
+
const subscription = await swRegistration.pushManager.getSubscription();
|
|
570
|
+
if (subscription) {
|
|
571
|
+
return subscription;
|
|
572
|
+
}
|
|
573
|
+
return swRegistration.pushManager.subscribe({
|
|
574
|
+
userVisibleOnly: true,
|
|
575
|
+
// Chrome <= 75 doesn't support base64-encoded VAPID key. For backward compatibility, VAPID key
|
|
576
|
+
// submitted to pushManager#subscribe must be of type Uint8Array.
|
|
577
|
+
applicationServerKey: base64ToArray(vapidKey)
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* Checks if the saved tokenDetails object matches the configuration provided.
|
|
582
|
+
*/
|
|
583
|
+
function isTokenValid(dbOptions, currentOptions) {
|
|
584
|
+
const isVapidKeyEqual = currentOptions.vapidKey === dbOptions.vapidKey;
|
|
585
|
+
const isEndpointEqual = currentOptions.endpoint === dbOptions.endpoint;
|
|
586
|
+
const isAuthEqual = currentOptions.auth === dbOptions.auth;
|
|
587
|
+
const isP256dhEqual = currentOptions.p256dh === dbOptions.p256dh;
|
|
588
|
+
return isVapidKeyEqual && isEndpointEqual && isAuthEqual && isP256dhEqual;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/**
|
|
592
|
+
* @license
|
|
593
|
+
* Copyright 2020 Google LLC
|
|
594
|
+
*
|
|
595
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
596
|
+
* you may not use this file except in compliance with the License.
|
|
597
|
+
* You may obtain a copy of the License at
|
|
598
|
+
*
|
|
599
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
600
|
+
*
|
|
601
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
602
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
603
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
604
|
+
* See the License for the specific language governing permissions and
|
|
605
|
+
* limitations under the License.
|
|
606
|
+
*/
|
|
607
|
+
function externalizePayload(internalPayload) {
|
|
608
|
+
const payload = {
|
|
609
|
+
from: internalPayload.from,
|
|
610
|
+
// eslint-disable-next-line camelcase
|
|
611
|
+
collapseKey: internalPayload.collapse_key,
|
|
612
|
+
// eslint-disable-next-line camelcase
|
|
613
|
+
messageId: internalPayload.fcmMessageId
|
|
614
|
+
};
|
|
615
|
+
propagateNotificationPayload(payload, internalPayload);
|
|
616
|
+
propagateDataPayload(payload, internalPayload);
|
|
617
|
+
propagateFcmOptions(payload, internalPayload);
|
|
618
|
+
return payload;
|
|
619
|
+
}
|
|
620
|
+
function propagateNotificationPayload(payload, messagePayloadInternal) {
|
|
621
|
+
if (!messagePayloadInternal.notification) {
|
|
622
|
+
return;
|
|
623
|
+
}
|
|
624
|
+
payload.notification = {};
|
|
625
|
+
const title = messagePayloadInternal.notification.title;
|
|
626
|
+
if (!!title) {
|
|
627
|
+
payload.notification.title = title;
|
|
628
|
+
}
|
|
629
|
+
const body = messagePayloadInternal.notification.body;
|
|
630
|
+
if (!!body) {
|
|
631
|
+
payload.notification.body = body;
|
|
632
|
+
}
|
|
633
|
+
const image = messagePayloadInternal.notification.image;
|
|
634
|
+
if (!!image) {
|
|
635
|
+
payload.notification.image = image;
|
|
636
|
+
}
|
|
637
|
+
const icon = messagePayloadInternal.notification.icon;
|
|
638
|
+
if (!!icon) {
|
|
639
|
+
payload.notification.icon = icon;
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
function propagateDataPayload(payload, messagePayloadInternal) {
|
|
643
|
+
if (!messagePayloadInternal.data) {
|
|
644
|
+
return;
|
|
645
|
+
}
|
|
646
|
+
payload.data = messagePayloadInternal.data;
|
|
647
|
+
}
|
|
648
|
+
function propagateFcmOptions(payload, messagePayloadInternal) {
|
|
649
|
+
// fcmOptions.link value is written into notification.click_action. see more in b/232072111
|
|
650
|
+
if (!messagePayloadInternal.fcmOptions &&
|
|
651
|
+
!messagePayloadInternal.notification?.click_action) {
|
|
652
|
+
return;
|
|
653
|
+
}
|
|
654
|
+
payload.fcmOptions = {};
|
|
655
|
+
const link = messagePayloadInternal.fcmOptions?.link ??
|
|
656
|
+
messagePayloadInternal.notification?.click_action;
|
|
657
|
+
if (!!link) {
|
|
658
|
+
payload.fcmOptions.link = link;
|
|
659
|
+
}
|
|
660
|
+
// eslint-disable-next-line camelcase
|
|
661
|
+
const analyticsLabel = messagePayloadInternal.fcmOptions?.analytics_label;
|
|
662
|
+
if (!!analyticsLabel) {
|
|
663
|
+
payload.fcmOptions.analyticsLabel = analyticsLabel;
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
/**
|
|
668
|
+
* @license
|
|
669
|
+
* Copyright 2019 Google LLC
|
|
670
|
+
*
|
|
671
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
672
|
+
* you may not use this file except in compliance with the License.
|
|
673
|
+
* You may obtain a copy of the License at
|
|
674
|
+
*
|
|
675
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
676
|
+
*
|
|
677
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
678
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
679
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
680
|
+
* See the License for the specific language governing permissions and
|
|
681
|
+
* limitations under the License.
|
|
682
|
+
*/
|
|
683
|
+
function isConsoleMessage(data) {
|
|
684
|
+
// This message has a campaign ID, meaning it was sent using the Firebase Console.
|
|
685
|
+
return typeof data === 'object' && !!data && CONSOLE_CAMPAIGN_ID in data;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
/**
|
|
689
|
+
* @license
|
|
690
|
+
* Copyright 2019 Google LLC
|
|
691
|
+
*
|
|
692
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
693
|
+
* you may not use this file except in compliance with the License.
|
|
694
|
+
* You may obtain a copy of the License at
|
|
695
|
+
*
|
|
696
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
697
|
+
*
|
|
698
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
699
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
700
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
701
|
+
* See the License for the specific language governing permissions and
|
|
702
|
+
* limitations under the License.
|
|
703
|
+
*/
|
|
704
|
+
/** Returns a promise that resolves after given time passes. */
|
|
705
|
+
function sleep(ms) {
|
|
706
|
+
return new Promise(resolve => {
|
|
707
|
+
setTimeout(resolve, ms);
|
|
708
|
+
});
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
/**
|
|
712
|
+
* @license
|
|
713
|
+
* Copyright 2019 Google LLC
|
|
714
|
+
*
|
|
715
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
716
|
+
* you may not use this file except in compliance with the License.
|
|
717
|
+
* You may obtain a copy of the License at
|
|
718
|
+
*
|
|
719
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
720
|
+
*
|
|
721
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
722
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
723
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
724
|
+
* See the License for the specific language governing permissions and
|
|
725
|
+
* limitations under the License.
|
|
726
|
+
*/
|
|
727
|
+
_mergeStrings('AzSCbw63g1R0nCw85jG8', 'Iaya3yLKwmgvh7cF0q4');
|
|
728
|
+
async function stageLog(messaging, internalPayload) {
|
|
729
|
+
const fcmEvent = createFcmEvent(internalPayload, await messaging.firebaseDependencies.installations.getId());
|
|
730
|
+
createAndEnqueueLogEvent(messaging, fcmEvent, internalPayload.productId);
|
|
731
|
+
}
|
|
732
|
+
function createFcmEvent(internalPayload, fid) {
|
|
733
|
+
const fcmEvent = {};
|
|
734
|
+
/* eslint-disable camelcase */
|
|
735
|
+
// some fields should always be non-null. Still check to ensure.
|
|
736
|
+
if (!!internalPayload.from) {
|
|
737
|
+
fcmEvent.project_number = internalPayload.from;
|
|
738
|
+
}
|
|
739
|
+
if (!!internalPayload.fcmMessageId) {
|
|
740
|
+
fcmEvent.message_id = internalPayload.fcmMessageId;
|
|
741
|
+
}
|
|
742
|
+
fcmEvent.instance_id = fid;
|
|
743
|
+
if (!!internalPayload.notification) {
|
|
744
|
+
fcmEvent.message_type = MessageType$1.DISPLAY_NOTIFICATION.toString();
|
|
745
|
+
}
|
|
746
|
+
else {
|
|
747
|
+
fcmEvent.message_type = MessageType$1.DATA_MESSAGE.toString();
|
|
748
|
+
}
|
|
749
|
+
fcmEvent.sdk_platform = SDK_PLATFORM_WEB.toString();
|
|
750
|
+
fcmEvent.package_name = self.origin.replace(/(^\w+:|^)\/\//, '');
|
|
751
|
+
if (!!internalPayload.collapse_key) {
|
|
752
|
+
fcmEvent.collapse_key = internalPayload.collapse_key;
|
|
753
|
+
}
|
|
754
|
+
fcmEvent.event = EVENT_MESSAGE_DELIVERED.toString();
|
|
755
|
+
if (!!internalPayload.fcmOptions?.analytics_label) {
|
|
756
|
+
fcmEvent.analytics_label = internalPayload.fcmOptions?.analytics_label;
|
|
757
|
+
}
|
|
758
|
+
/* eslint-enable camelcase */
|
|
759
|
+
return fcmEvent;
|
|
760
|
+
}
|
|
761
|
+
function createAndEnqueueLogEvent(messaging, fcmEvent, productId) {
|
|
762
|
+
const logEvent = {};
|
|
763
|
+
/* eslint-disable camelcase */
|
|
764
|
+
logEvent.event_time_ms = Math.floor(Date.now()).toString();
|
|
765
|
+
logEvent.source_extension_json_proto3 = JSON.stringify({
|
|
766
|
+
messaging_client_event: fcmEvent
|
|
767
|
+
});
|
|
768
|
+
if (!!productId) {
|
|
769
|
+
logEvent.compliance_data = buildComplianceData(productId);
|
|
770
|
+
}
|
|
771
|
+
// eslint-disable-next-line camelcase
|
|
772
|
+
messaging.logEvents.push(logEvent);
|
|
773
|
+
}
|
|
774
|
+
function buildComplianceData(productId) {
|
|
775
|
+
const complianceData = {
|
|
776
|
+
privacy_context: {
|
|
777
|
+
prequest: {
|
|
778
|
+
origin_associated_product_id: productId
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
};
|
|
782
|
+
return complianceData;
|
|
783
|
+
}
|
|
784
|
+
function _mergeStrings(s1, s2) {
|
|
785
|
+
const resultArray = [];
|
|
786
|
+
for (let i = 0; i < s1.length; i++) {
|
|
787
|
+
resultArray.push(s1.charAt(i));
|
|
788
|
+
if (i < s2.length) {
|
|
789
|
+
resultArray.push(s2.charAt(i));
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
return resultArray.join('');
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
/**
|
|
796
|
+
* @license
|
|
797
|
+
* Copyright 2017 Google LLC
|
|
798
|
+
*
|
|
799
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
800
|
+
* you may not use this file except in compliance with the License.
|
|
801
|
+
* You may obtain a copy of the License at
|
|
802
|
+
*
|
|
803
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
804
|
+
*
|
|
805
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
806
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
807
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
808
|
+
* See the License for the specific language governing permissions and
|
|
809
|
+
* limitations under the License.
|
|
810
|
+
*/
|
|
811
|
+
async function onSubChange(event, messaging) {
|
|
812
|
+
const { newSubscription } = event;
|
|
813
|
+
if (!newSubscription) {
|
|
814
|
+
// Subscription revoked, delete token
|
|
815
|
+
await deleteTokenInternal(messaging);
|
|
816
|
+
return;
|
|
817
|
+
}
|
|
818
|
+
const tokenDetails = await dbGet(messaging.firebaseDependencies);
|
|
819
|
+
await deleteTokenInternal(messaging);
|
|
820
|
+
messaging.vapidKey =
|
|
821
|
+
tokenDetails?.subscriptionOptions?.vapidKey ?? DEFAULT_VAPID_KEY;
|
|
822
|
+
await getTokenInternal(messaging);
|
|
823
|
+
}
|
|
824
|
+
async function onPush(event, messaging) {
|
|
825
|
+
const internalPayload = getMessagePayloadInternal(event);
|
|
826
|
+
if (!internalPayload) {
|
|
827
|
+
// Failed to get parsed MessagePayload from the PushEvent. Skip handling the push.
|
|
828
|
+
return;
|
|
829
|
+
}
|
|
830
|
+
// log to Firelog with user consent
|
|
831
|
+
if (messaging.deliveryMetricsExportedToBigQueryEnabled) {
|
|
832
|
+
await stageLog(messaging, internalPayload);
|
|
833
|
+
}
|
|
834
|
+
// foreground handling: eventually passed to onMessage hook
|
|
835
|
+
const clientList = await getClientList();
|
|
836
|
+
if (hasVisibleClients(clientList)) {
|
|
837
|
+
return sendMessagePayloadInternalToWindows(clientList, internalPayload);
|
|
838
|
+
}
|
|
839
|
+
// background handling: display if possible and pass to onBackgroundMessage hook
|
|
840
|
+
if (!!internalPayload.notification) {
|
|
841
|
+
await showNotification(wrapInternalPayload(internalPayload));
|
|
842
|
+
}
|
|
843
|
+
if (!messaging) {
|
|
844
|
+
return;
|
|
845
|
+
}
|
|
846
|
+
if (!!messaging.onBackgroundMessageHandler) {
|
|
847
|
+
const payload = externalizePayload(internalPayload);
|
|
848
|
+
if (typeof messaging.onBackgroundMessageHandler === 'function') {
|
|
849
|
+
await messaging.onBackgroundMessageHandler(payload);
|
|
850
|
+
}
|
|
851
|
+
else {
|
|
852
|
+
messaging.onBackgroundMessageHandler.next(payload);
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
async function onNotificationClick(event) {
|
|
857
|
+
const internalPayload = event.notification?.data?.[FCM_MSG];
|
|
858
|
+
if (!internalPayload) {
|
|
859
|
+
return;
|
|
860
|
+
}
|
|
861
|
+
else if (event.action) {
|
|
862
|
+
// User clicked on an action button. This will allow developers to act on action button clicks
|
|
863
|
+
// by using a custom onNotificationClick listener that they define.
|
|
864
|
+
return;
|
|
865
|
+
}
|
|
866
|
+
// Prevent other listeners from receiving the event
|
|
867
|
+
event.stopImmediatePropagation();
|
|
868
|
+
event.notification.close();
|
|
869
|
+
// Note clicking on a notification with no link set will focus the Chrome's current tab.
|
|
870
|
+
const link = getLink(internalPayload);
|
|
871
|
+
if (!link) {
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
// FM should only open/focus links from app's origin.
|
|
875
|
+
const url = new URL(link, self.location.href);
|
|
876
|
+
const originUrl = new URL(self.location.origin);
|
|
877
|
+
if (url.host !== originUrl.host) {
|
|
878
|
+
return;
|
|
879
|
+
}
|
|
880
|
+
let client = await getWindowClient(url);
|
|
881
|
+
if (!client) {
|
|
882
|
+
client = await self.clients.openWindow(link);
|
|
883
|
+
// Wait three seconds for the client to initialize and set up the message handler so that it
|
|
884
|
+
// can receive the message.
|
|
885
|
+
await sleep(3000);
|
|
886
|
+
}
|
|
887
|
+
else {
|
|
888
|
+
client = await client.focus();
|
|
889
|
+
}
|
|
890
|
+
if (!client) {
|
|
891
|
+
// Window Client will not be returned if it's for a third party origin.
|
|
892
|
+
return;
|
|
893
|
+
}
|
|
894
|
+
internalPayload.messageType = MessageType.NOTIFICATION_CLICKED;
|
|
895
|
+
internalPayload.isFirebaseMessaging = true;
|
|
896
|
+
return client.postMessage(internalPayload);
|
|
897
|
+
}
|
|
898
|
+
function wrapInternalPayload(internalPayload) {
|
|
899
|
+
const wrappedInternalPayload = {
|
|
900
|
+
...internalPayload.notification
|
|
901
|
+
};
|
|
902
|
+
// Put the message payload under FCM_MSG name so we can identify the notification as being an FCM
|
|
903
|
+
// notification vs a notification from somewhere else (i.e. normal web push or developer generated
|
|
904
|
+
// notification).
|
|
905
|
+
wrappedInternalPayload.data = {
|
|
906
|
+
[FCM_MSG]: internalPayload
|
|
907
|
+
};
|
|
908
|
+
return wrappedInternalPayload;
|
|
909
|
+
}
|
|
910
|
+
function getMessagePayloadInternal({ data }) {
|
|
911
|
+
if (!data) {
|
|
912
|
+
return null;
|
|
913
|
+
}
|
|
914
|
+
try {
|
|
915
|
+
return data.json();
|
|
916
|
+
}
|
|
917
|
+
catch (err) {
|
|
918
|
+
// Not JSON so not an FCM message.
|
|
919
|
+
return null;
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
/**
|
|
923
|
+
* @param url The URL to look for when focusing a client.
|
|
924
|
+
* @return Returns an existing window client or a newly opened WindowClient.
|
|
925
|
+
*/
|
|
926
|
+
async function getWindowClient(url) {
|
|
927
|
+
const clientList = await getClientList();
|
|
928
|
+
for (const client of clientList) {
|
|
929
|
+
const clientUrl = new URL(client.url, self.location.href);
|
|
930
|
+
if (url.host === clientUrl.host) {
|
|
931
|
+
return client;
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
return null;
|
|
935
|
+
}
|
|
936
|
+
/**
|
|
937
|
+
* @returns If there is currently a visible WindowClient, this method will resolve to true,
|
|
938
|
+
* otherwise false.
|
|
939
|
+
*/
|
|
940
|
+
function hasVisibleClients(clientList) {
|
|
941
|
+
return clientList.some(client => client.visibilityState === 'visible' &&
|
|
942
|
+
// Ignore chrome-extension clients as that matches the background pages of extensions, which
|
|
943
|
+
// are always considered visible for some reason.
|
|
944
|
+
!client.url.startsWith('chrome-extension://'));
|
|
945
|
+
}
|
|
946
|
+
function sendMessagePayloadInternalToWindows(clientList, internalPayload) {
|
|
947
|
+
internalPayload.isFirebaseMessaging = true;
|
|
948
|
+
internalPayload.messageType = MessageType.PUSH_RECEIVED;
|
|
949
|
+
for (const client of clientList) {
|
|
950
|
+
client.postMessage(internalPayload);
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
function getClientList() {
|
|
954
|
+
return self.clients.matchAll({
|
|
955
|
+
type: 'window',
|
|
956
|
+
includeUncontrolled: true
|
|
957
|
+
// TS doesn't know that "type: 'window'" means it'll return WindowClient[]
|
|
958
|
+
});
|
|
959
|
+
}
|
|
960
|
+
function showNotification(notificationPayloadInternal) {
|
|
961
|
+
// Note: Firefox does not support the maxActions property.
|
|
962
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/notification/maxActions
|
|
963
|
+
const { actions } = notificationPayloadInternal;
|
|
964
|
+
const { maxActions } = Notification;
|
|
965
|
+
if (actions && maxActions && actions.length > maxActions) {
|
|
966
|
+
console.warn(`This browser only supports ${maxActions} actions. The remaining actions will not be displayed.`);
|
|
967
|
+
}
|
|
968
|
+
return self.registration.showNotification(
|
|
969
|
+
/* title= */ notificationPayloadInternal.title ?? '', notificationPayloadInternal);
|
|
970
|
+
}
|
|
971
|
+
function getLink(payload) {
|
|
972
|
+
// eslint-disable-next-line camelcase
|
|
973
|
+
const link = payload.fcmOptions?.link ?? payload.notification?.click_action;
|
|
974
|
+
if (link) {
|
|
975
|
+
return link;
|
|
976
|
+
}
|
|
977
|
+
if (isConsoleMessage(payload.data)) {
|
|
978
|
+
// Notification created in the Firebase Console. Redirect to origin.
|
|
979
|
+
return self.location.origin;
|
|
980
|
+
}
|
|
981
|
+
else {
|
|
982
|
+
return null;
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
/**
|
|
987
|
+
* @license
|
|
988
|
+
* Copyright 2019 Google LLC
|
|
989
|
+
*
|
|
990
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
991
|
+
* you may not use this file except in compliance with the License.
|
|
992
|
+
* You may obtain a copy of the License at
|
|
993
|
+
*
|
|
994
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
995
|
+
*
|
|
996
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
997
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
998
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
999
|
+
* See the License for the specific language governing permissions and
|
|
1000
|
+
* limitations under the License.
|
|
1001
|
+
*/
|
|
1002
|
+
function extractAppConfig(app) {
|
|
1003
|
+
if (!app || !app.options) {
|
|
1004
|
+
throw getMissingValueError('App Configuration Object');
|
|
1005
|
+
}
|
|
1006
|
+
if (!app.name) {
|
|
1007
|
+
throw getMissingValueError('App Name');
|
|
1008
|
+
}
|
|
1009
|
+
// Required app config keys
|
|
1010
|
+
const configKeys = [
|
|
1011
|
+
'projectId',
|
|
1012
|
+
'apiKey',
|
|
1013
|
+
'appId',
|
|
1014
|
+
'messagingSenderId'
|
|
1015
|
+
];
|
|
1016
|
+
const { options } = app;
|
|
1017
|
+
for (const keyName of configKeys) {
|
|
1018
|
+
if (!options[keyName]) {
|
|
1019
|
+
throw getMissingValueError(keyName);
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
return {
|
|
1023
|
+
appName: app.name,
|
|
1024
|
+
projectId: options.projectId,
|
|
1025
|
+
apiKey: options.apiKey,
|
|
1026
|
+
appId: options.appId,
|
|
1027
|
+
senderId: options.messagingSenderId
|
|
1028
|
+
};
|
|
1029
|
+
}
|
|
1030
|
+
function getMissingValueError(valueName) {
|
|
1031
|
+
return ERROR_FACTORY.create("missing-app-config-values" /* ErrorCode.MISSING_APP_CONFIG_VALUES */, {
|
|
1032
|
+
valueName
|
|
1033
|
+
});
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
/**
|
|
1037
|
+
* @license
|
|
1038
|
+
* Copyright 2020 Google LLC
|
|
1039
|
+
*
|
|
1040
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1041
|
+
* you may not use this file except in compliance with the License.
|
|
1042
|
+
* You may obtain a copy of the License at
|
|
1043
|
+
*
|
|
1044
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1045
|
+
*
|
|
1046
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
1047
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1048
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1049
|
+
* See the License for the specific language governing permissions and
|
|
1050
|
+
* limitations under the License.
|
|
1051
|
+
*/
|
|
1052
|
+
class MessagingService {
|
|
1053
|
+
constructor(app, installations, analyticsProvider) {
|
|
1054
|
+
// logging is only done with end user consent. Default to false.
|
|
1055
|
+
this.deliveryMetricsExportedToBigQueryEnabled = false;
|
|
1056
|
+
this.onBackgroundMessageHandler = null;
|
|
1057
|
+
this.onMessageHandler = null;
|
|
1058
|
+
this.logEvents = [];
|
|
1059
|
+
this.isLogServiceStarted = false;
|
|
1060
|
+
const appConfig = extractAppConfig(app);
|
|
1061
|
+
this.firebaseDependencies = {
|
|
1062
|
+
app,
|
|
1063
|
+
appConfig,
|
|
1064
|
+
installations,
|
|
1065
|
+
analyticsProvider
|
|
1066
|
+
};
|
|
1067
|
+
}
|
|
1068
|
+
_delete() {
|
|
1069
|
+
return Promise.resolve();
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
/**
|
|
1074
|
+
* @license
|
|
1075
|
+
* Copyright 2020 Google LLC
|
|
1076
|
+
*
|
|
1077
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1078
|
+
* you may not use this file except in compliance with the License.
|
|
1079
|
+
* You may obtain a copy of the License at
|
|
1080
|
+
*
|
|
1081
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1082
|
+
*
|
|
1083
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
1084
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1085
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1086
|
+
* See the License for the specific language governing permissions and
|
|
1087
|
+
* limitations under the License.
|
|
1088
|
+
*/
|
|
1089
|
+
const SwMessagingFactory = (container) => {
|
|
1090
|
+
const messaging = new MessagingService(container.getProvider('app').getImmediate(), container.getProvider('installations-internal').getImmediate(), container.getProvider('analytics-internal'));
|
|
1091
|
+
self.addEventListener('push', e => {
|
|
1092
|
+
e.waitUntil(onPush(e, messaging));
|
|
1093
|
+
});
|
|
1094
|
+
self.addEventListener('pushsubscriptionchange', e => {
|
|
1095
|
+
e.waitUntil(onSubChange(e, messaging));
|
|
1096
|
+
});
|
|
1097
|
+
self.addEventListener('notificationclick', e => {
|
|
1098
|
+
e.waitUntil(onNotificationClick(e));
|
|
1099
|
+
});
|
|
1100
|
+
return messaging;
|
|
1101
|
+
};
|
|
1102
|
+
/**
|
|
1103
|
+
* The messaging instance registered in sw is named differently than that of in client. This is
|
|
1104
|
+
* because both `registerMessagingInWindow` and `registerMessagingInSw` would be called in
|
|
1105
|
+
* `messaging-compat` and component with the same name can only be registered once.
|
|
1106
|
+
*/
|
|
1107
|
+
function registerMessagingInSw() {
|
|
1108
|
+
app._registerComponent(new component.Component('messaging-sw', SwMessagingFactory, "PUBLIC" /* ComponentType.PUBLIC */));
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
/**
|
|
1112
|
+
* @license
|
|
1113
|
+
* Copyright 2020 Google LLC
|
|
1114
|
+
*
|
|
1115
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1116
|
+
* you may not use this file except in compliance with the License.
|
|
1117
|
+
* You may obtain a copy of the License at
|
|
1118
|
+
*
|
|
1119
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1120
|
+
*
|
|
1121
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
1122
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1123
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1124
|
+
* See the License for the specific language governing permissions and
|
|
1125
|
+
* limitations under the License.
|
|
1126
|
+
*/
|
|
1127
|
+
/**
|
|
1128
|
+
* Checks whether all required APIs exist within SW Context
|
|
1129
|
+
* @returns a Promise that resolves to a boolean.
|
|
1130
|
+
*
|
|
1131
|
+
* @public
|
|
1132
|
+
*/
|
|
1133
|
+
async function isSwSupported() {
|
|
1134
|
+
// firebase-js-sdk/issues/2393 reveals that idb#open in Safari iframe and Firefox private browsing
|
|
1135
|
+
// might be prohibited to run. In these contexts, an error would be thrown during the messaging
|
|
1136
|
+
// instantiating phase, informing the developers to import/call isSupported for special handling.
|
|
1137
|
+
return (util.isIndexedDBAvailable() &&
|
|
1138
|
+
(await util.validateIndexedDBOpenable()) &&
|
|
1139
|
+
'PushManager' in self &&
|
|
1140
|
+
'Notification' in self &&
|
|
1141
|
+
ServiceWorkerRegistration.prototype.hasOwnProperty('showNotification') &&
|
|
1142
|
+
PushSubscription.prototype.hasOwnProperty('getKey'));
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
/**
|
|
1146
|
+
* @license
|
|
1147
|
+
* Copyright 2020 Google LLC
|
|
1148
|
+
*
|
|
1149
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1150
|
+
* you may not use this file except in compliance with the License.
|
|
1151
|
+
* You may obtain a copy of the License at
|
|
1152
|
+
*
|
|
1153
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1154
|
+
*
|
|
1155
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
1156
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1157
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1158
|
+
* See the License for the specific language governing permissions and
|
|
1159
|
+
* limitations under the License.
|
|
1160
|
+
*/
|
|
1161
|
+
function onBackgroundMessage$1(messaging, nextOrObserver) {
|
|
1162
|
+
if (self.document !== undefined) {
|
|
1163
|
+
throw ERROR_FACTORY.create("only-available-in-sw" /* ErrorCode.AVAILABLE_IN_SW */);
|
|
1164
|
+
}
|
|
1165
|
+
messaging.onBackgroundMessageHandler = nextOrObserver;
|
|
1166
|
+
return () => {
|
|
1167
|
+
messaging.onBackgroundMessageHandler = null;
|
|
1168
|
+
};
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
/**
|
|
1172
|
+
* @license
|
|
1173
|
+
* Copyright 2020 Google LLC
|
|
1174
|
+
*
|
|
1175
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1176
|
+
* you may not use this file except in compliance with the License.
|
|
1177
|
+
* You may obtain a copy of the License at
|
|
1178
|
+
*
|
|
1179
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1180
|
+
*
|
|
1181
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
1182
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1183
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1184
|
+
* See the License for the specific language governing permissions and
|
|
1185
|
+
* limitations under the License.
|
|
1186
|
+
*/
|
|
1187
|
+
function _setDeliveryMetricsExportedToBigQueryEnabled(messaging, enable) {
|
|
1188
|
+
messaging.deliveryMetricsExportedToBigQueryEnabled =
|
|
1189
|
+
enable;
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
/**
|
|
1193
|
+
* @license
|
|
1194
|
+
* Copyright 2017 Google LLC
|
|
1195
|
+
*
|
|
1196
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1197
|
+
* you may not use this file except in compliance with the License.
|
|
1198
|
+
* You may obtain a copy of the License at
|
|
1199
|
+
*
|
|
1200
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1201
|
+
*
|
|
1202
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
1203
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1204
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1205
|
+
* See the License for the specific language governing permissions and
|
|
1206
|
+
* limitations under the License.
|
|
1207
|
+
*/
|
|
1208
|
+
/**
|
|
1209
|
+
* Retrieves a Firebase Cloud Messaging instance.
|
|
1210
|
+
*
|
|
1211
|
+
* @returns The Firebase Cloud Messaging instance associated with the provided firebase app.
|
|
1212
|
+
*
|
|
1213
|
+
* @public
|
|
1214
|
+
*/
|
|
1215
|
+
function getMessagingInSw(app$1 = app.getApp()) {
|
|
1216
|
+
// Conscious decision to make this async check non-blocking during the messaging instance
|
|
1217
|
+
// initialization phase for performance consideration. An error would be thrown latter for
|
|
1218
|
+
// developer's information. Developers can then choose to import and call `isSupported` for
|
|
1219
|
+
// special handling.
|
|
1220
|
+
isSwSupported().then(isSupported => {
|
|
1221
|
+
// If `isSwSupported()` resolved, but returned false.
|
|
1222
|
+
if (!isSupported) {
|
|
1223
|
+
throw ERROR_FACTORY.create("unsupported-browser" /* ErrorCode.UNSUPPORTED_BROWSER */);
|
|
1224
|
+
}
|
|
1225
|
+
}, _ => {
|
|
1226
|
+
// If `isSwSupported()` rejected.
|
|
1227
|
+
throw ERROR_FACTORY.create("indexed-db-unsupported" /* ErrorCode.INDEXED_DB_UNSUPPORTED */);
|
|
1228
|
+
});
|
|
1229
|
+
return app._getProvider(util.getModularInstance(app$1), 'messaging-sw').getImmediate();
|
|
1230
|
+
}
|
|
1231
|
+
/**
|
|
1232
|
+
* Called when a message is received while the app is in the background. An app is considered to be
|
|
1233
|
+
* in the background if no active window is displayed.
|
|
1234
|
+
*
|
|
1235
|
+
* @param messaging - The {@link Messaging} instance.
|
|
1236
|
+
* @param nextOrObserver - This function, or observer object with `next` defined, is called when a
|
|
1237
|
+
* message is received and the app is currently in the background.
|
|
1238
|
+
*
|
|
1239
|
+
* @returns To stop listening for messages execute this returned function
|
|
1240
|
+
*
|
|
1241
|
+
* @public
|
|
1242
|
+
*/
|
|
1243
|
+
function onBackgroundMessage(messaging, nextOrObserver) {
|
|
1244
|
+
messaging = util.getModularInstance(messaging);
|
|
1245
|
+
return onBackgroundMessage$1(messaging, nextOrObserver);
|
|
1246
|
+
}
|
|
1247
|
+
/**
|
|
1248
|
+
* Enables or disables Firebase Cloud Messaging message delivery metrics export to BigQuery. By
|
|
1249
|
+
* default, message delivery metrics are not exported to BigQuery. Use this method to enable or
|
|
1250
|
+
* disable the export at runtime.
|
|
1251
|
+
*
|
|
1252
|
+
* @param messaging - The `FirebaseMessaging` instance.
|
|
1253
|
+
* @param enable - Whether Firebase Cloud Messaging should export message delivery metrics to
|
|
1254
|
+
* BigQuery.
|
|
1255
|
+
*
|
|
1256
|
+
* @public
|
|
1257
|
+
*/
|
|
1258
|
+
function experimentalSetDeliveryMetricsExportedToBigQueryEnabled(messaging, enable) {
|
|
1259
|
+
messaging = util.getModularInstance(messaging);
|
|
1260
|
+
return _setDeliveryMetricsExportedToBigQueryEnabled(messaging, enable);
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
/**
|
|
1264
|
+
* @license
|
|
1265
|
+
* Copyright 2017 Google LLC
|
|
1266
|
+
*
|
|
1267
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1268
|
+
* you may not use this file except in compliance with the License.
|
|
1269
|
+
* You may obtain a copy of the License at
|
|
1270
|
+
*
|
|
1271
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1272
|
+
*
|
|
1273
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
1274
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1275
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1276
|
+
* See the License for the specific language governing permissions and
|
|
1277
|
+
* limitations under the License.
|
|
1278
|
+
*/
|
|
1279
|
+
registerMessagingInSw();
|
|
1280
|
+
|
|
1281
|
+
exports.experimentalSetDeliveryMetricsExportedToBigQueryEnabled = experimentalSetDeliveryMetricsExportedToBigQueryEnabled;
|
|
1282
|
+
exports.getMessaging = getMessagingInSw;
|
|
1283
|
+
exports.isSupported = isSwSupported;
|
|
1284
|
+
exports.onBackgroundMessage = onBackgroundMessage;
|
|
1285
|
+
//# sourceMappingURL=index.sw.cjs.map
|