@firebase/messaging 0.12.12 → 0.12.13
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/dist/esm/index.esm2017.js +2 -2
- package/dist/esm/index.esm2017.js.map +1 -1
- package/dist/esm/index.sw.esm2017.js.map +1 -1
- package/dist/index.cjs.js +482 -779
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.sw.cjs +488 -804
- package/dist/index.sw.cjs.map +1 -1
- package/package.json +5 -7
- package/dist/esm/index.esm.js +0 -1530
- package/dist/esm/index.esm.js.map +0 -1
package/dist/index.cjs.js
CHANGED
|
@@ -4,7 +4,6 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
require('@firebase/installations');
|
|
6
6
|
var component = require('@firebase/component');
|
|
7
|
-
var tslib = require('tslib');
|
|
8
7
|
var idb = require('idb');
|
|
9
8
|
var util = require('@firebase/util');
|
|
10
9
|
var app = require('@firebase/app');
|
|
@@ -25,15 +24,15 @@ var app = require('@firebase/app');
|
|
|
25
24
|
* See the License for the specific language governing permissions and
|
|
26
25
|
* limitations under the License.
|
|
27
26
|
*/
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
27
|
+
const DEFAULT_SW_PATH = '/firebase-messaging-sw.js';
|
|
28
|
+
const DEFAULT_SW_SCOPE = '/firebase-cloud-messaging-push-scope';
|
|
29
|
+
const DEFAULT_VAPID_KEY = 'BDOU99-h67HcA6JeFXHbSNMu7e2yNNu3RzoMj8TM4W88jITfq7ZmPvIM1Iv-4_l2LxQcYwhqby2xGpWwzjfAnG4';
|
|
30
|
+
const ENDPOINT = 'https://fcmregistrations.googleapis.com/v1';
|
|
31
|
+
const CONSOLE_CAMPAIGN_ID = 'google.c.a.c_id';
|
|
32
|
+
const CONSOLE_CAMPAIGN_NAME = 'google.c.a.c_l';
|
|
33
|
+
const CONSOLE_CAMPAIGN_TIME = 'google.c.a.ts';
|
|
35
34
|
/** Set to '1' if Analytics is enabled for the campaign */
|
|
36
|
-
|
|
35
|
+
const CONSOLE_CAMPAIGN_ANALYTICS_ENABLED = 'google.c.a.e';
|
|
37
36
|
var MessageType$1;
|
|
38
37
|
(function (MessageType) {
|
|
39
38
|
MessageType[MessageType["DATA_MESSAGE"] = 1] = "DATA_MESSAGE";
|
|
@@ -77,18 +76,18 @@ var MessageType;
|
|
|
77
76
|
* limitations under the License.
|
|
78
77
|
*/
|
|
79
78
|
function arrayToBase64(array) {
|
|
80
|
-
|
|
81
|
-
|
|
79
|
+
const uint8Array = new Uint8Array(array);
|
|
80
|
+
const base64String = btoa(String.fromCharCode(...uint8Array));
|
|
82
81
|
return base64String.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
|
|
83
82
|
}
|
|
84
83
|
function base64ToArray(base64String) {
|
|
85
|
-
|
|
86
|
-
|
|
84
|
+
const padding = '='.repeat((4 - (base64String.length % 4)) % 4);
|
|
85
|
+
const base64 = (base64String + padding)
|
|
87
86
|
.replace(/\-/g, '+')
|
|
88
87
|
.replace(/_/g, '/');
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
for (
|
|
88
|
+
const rawData = atob(base64);
|
|
89
|
+
const outputArray = new Uint8Array(rawData.length);
|
|
90
|
+
for (let i = 0; i < rawData.length; ++i) {
|
|
92
91
|
outputArray[i] = rawData.charCodeAt(i);
|
|
93
92
|
}
|
|
94
93
|
return outputArray;
|
|
@@ -110,134 +109,104 @@ function base64ToArray(base64String) {
|
|
|
110
109
|
* See the License for the specific language governing permissions and
|
|
111
110
|
* limitations under the License.
|
|
112
111
|
*/
|
|
113
|
-
|
|
112
|
+
const OLD_DB_NAME = 'fcm_token_details_db';
|
|
114
113
|
/**
|
|
115
114
|
* The last DB version of 'fcm_token_details_db' was 4. This is one higher, so that the upgrade
|
|
116
115
|
* callback is called for all versions of the old DB.
|
|
117
116
|
*/
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
function migrateOldDatabase(senderId) {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
117
|
+
const OLD_DB_VERSION = 5;
|
|
118
|
+
const OLD_OBJECT_STORE_NAME = 'fcm_token_object_Store';
|
|
119
|
+
async function migrateOldDatabase(senderId) {
|
|
120
|
+
if ('databases' in indexedDB) {
|
|
121
|
+
// indexedDb.databases() is an IndexedDB v3 API and does not exist in all browsers. TODO: Remove
|
|
122
|
+
// typecast when it lands in TS types.
|
|
123
|
+
const databases = await indexedDB.databases();
|
|
124
|
+
const dbNames = databases.map(db => db.name);
|
|
125
|
+
if (!dbNames.includes(OLD_DB_NAME)) {
|
|
126
|
+
// old DB didn't exist, no need to open.
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
let tokenDetails = null;
|
|
131
|
+
const db = await idb.openDB(OLD_DB_NAME, OLD_DB_VERSION, {
|
|
132
|
+
upgrade: async (db, oldVersion, newVersion, upgradeTransaction) => {
|
|
133
|
+
var _a;
|
|
134
|
+
if (oldVersion < 2) {
|
|
135
|
+
// Database too old, skip migration.
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (!db.objectStoreNames.contains(OLD_OBJECT_STORE_NAME)) {
|
|
139
|
+
// Database did not exist. Nothing to do.
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
const objectStore = upgradeTransaction.objectStore(OLD_OBJECT_STORE_NAME);
|
|
143
|
+
const value = await objectStore.index('fcmSenderId').get(senderId);
|
|
144
|
+
await objectStore.clear();
|
|
145
|
+
if (!value) {
|
|
146
|
+
// No entry in the database, nothing to migrate.
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
if (oldVersion === 2) {
|
|
150
|
+
const oldDetails = value;
|
|
151
|
+
if (!oldDetails.auth || !oldDetails.p256dh || !oldDetails.endpoint) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
tokenDetails = {
|
|
155
|
+
token: oldDetails.fcmToken,
|
|
156
|
+
createTime: (_a = oldDetails.createTime) !== null && _a !== void 0 ? _a : Date.now(),
|
|
157
|
+
subscriptionOptions: {
|
|
158
|
+
auth: oldDetails.auth,
|
|
159
|
+
p256dh: oldDetails.p256dh,
|
|
160
|
+
endpoint: oldDetails.endpoint,
|
|
161
|
+
swScope: oldDetails.swScope,
|
|
162
|
+
vapidKey: typeof oldDetails.vapidKey === 'string'
|
|
163
|
+
? oldDetails.vapidKey
|
|
164
|
+
: arrayToBase64(oldDetails.vapidKey)
|
|
135
165
|
}
|
|
136
|
-
|
|
137
|
-
case 2:
|
|
138
|
-
tokenDetails = null;
|
|
139
|
-
return [4 /*yield*/, idb.openDB(OLD_DB_NAME, OLD_DB_VERSION, {
|
|
140
|
-
upgrade: function (db, oldVersion, newVersion, upgradeTransaction) { return tslib.__awaiter(_this, void 0, void 0, function () {
|
|
141
|
-
var objectStore, value, oldDetails, oldDetails, oldDetails;
|
|
142
|
-
var _a;
|
|
143
|
-
return tslib.__generator(this, function (_b) {
|
|
144
|
-
switch (_b.label) {
|
|
145
|
-
case 0:
|
|
146
|
-
if (oldVersion < 2) {
|
|
147
|
-
// Database too old, skip migration.
|
|
148
|
-
return [2 /*return*/];
|
|
149
|
-
}
|
|
150
|
-
if (!db.objectStoreNames.contains(OLD_OBJECT_STORE_NAME)) {
|
|
151
|
-
// Database did not exist. Nothing to do.
|
|
152
|
-
return [2 /*return*/];
|
|
153
|
-
}
|
|
154
|
-
objectStore = upgradeTransaction.objectStore(OLD_OBJECT_STORE_NAME);
|
|
155
|
-
return [4 /*yield*/, objectStore.index('fcmSenderId').get(senderId)];
|
|
156
|
-
case 1:
|
|
157
|
-
value = _b.sent();
|
|
158
|
-
return [4 /*yield*/, objectStore.clear()];
|
|
159
|
-
case 2:
|
|
160
|
-
_b.sent();
|
|
161
|
-
if (!value) {
|
|
162
|
-
// No entry in the database, nothing to migrate.
|
|
163
|
-
return [2 /*return*/];
|
|
164
|
-
}
|
|
165
|
-
if (oldVersion === 2) {
|
|
166
|
-
oldDetails = value;
|
|
167
|
-
if (!oldDetails.auth || !oldDetails.p256dh || !oldDetails.endpoint) {
|
|
168
|
-
return [2 /*return*/];
|
|
169
|
-
}
|
|
170
|
-
tokenDetails = {
|
|
171
|
-
token: oldDetails.fcmToken,
|
|
172
|
-
createTime: (_a = oldDetails.createTime) !== null && _a !== void 0 ? _a : Date.now(),
|
|
173
|
-
subscriptionOptions: {
|
|
174
|
-
auth: oldDetails.auth,
|
|
175
|
-
p256dh: oldDetails.p256dh,
|
|
176
|
-
endpoint: oldDetails.endpoint,
|
|
177
|
-
swScope: oldDetails.swScope,
|
|
178
|
-
vapidKey: typeof oldDetails.vapidKey === 'string'
|
|
179
|
-
? oldDetails.vapidKey
|
|
180
|
-
: arrayToBase64(oldDetails.vapidKey)
|
|
181
|
-
}
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
else if (oldVersion === 3) {
|
|
185
|
-
oldDetails = value;
|
|
186
|
-
tokenDetails = {
|
|
187
|
-
token: oldDetails.fcmToken,
|
|
188
|
-
createTime: oldDetails.createTime,
|
|
189
|
-
subscriptionOptions: {
|
|
190
|
-
auth: arrayToBase64(oldDetails.auth),
|
|
191
|
-
p256dh: arrayToBase64(oldDetails.p256dh),
|
|
192
|
-
endpoint: oldDetails.endpoint,
|
|
193
|
-
swScope: oldDetails.swScope,
|
|
194
|
-
vapidKey: arrayToBase64(oldDetails.vapidKey)
|
|
195
|
-
}
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
else if (oldVersion === 4) {
|
|
199
|
-
oldDetails = value;
|
|
200
|
-
tokenDetails = {
|
|
201
|
-
token: oldDetails.fcmToken,
|
|
202
|
-
createTime: oldDetails.createTime,
|
|
203
|
-
subscriptionOptions: {
|
|
204
|
-
auth: arrayToBase64(oldDetails.auth),
|
|
205
|
-
p256dh: arrayToBase64(oldDetails.p256dh),
|
|
206
|
-
endpoint: oldDetails.endpoint,
|
|
207
|
-
swScope: oldDetails.swScope,
|
|
208
|
-
vapidKey: arrayToBase64(oldDetails.vapidKey)
|
|
209
|
-
}
|
|
210
|
-
};
|
|
211
|
-
}
|
|
212
|
-
return [2 /*return*/];
|
|
213
|
-
}
|
|
214
|
-
});
|
|
215
|
-
}); }
|
|
216
|
-
})];
|
|
217
|
-
case 3:
|
|
218
|
-
db = _a.sent();
|
|
219
|
-
db.close();
|
|
220
|
-
// Delete all old databases.
|
|
221
|
-
return [4 /*yield*/, idb.deleteDB(OLD_DB_NAME)];
|
|
222
|
-
case 4:
|
|
223
|
-
// Delete all old databases.
|
|
224
|
-
_a.sent();
|
|
225
|
-
return [4 /*yield*/, idb.deleteDB('fcm_vapid_details_db')];
|
|
226
|
-
case 5:
|
|
227
|
-
_a.sent();
|
|
228
|
-
return [4 /*yield*/, idb.deleteDB('undefined')];
|
|
229
|
-
case 6:
|
|
230
|
-
_a.sent();
|
|
231
|
-
return [2 /*return*/, checkTokenDetails(tokenDetails) ? tokenDetails : null];
|
|
166
|
+
};
|
|
232
167
|
}
|
|
233
|
-
|
|
168
|
+
else if (oldVersion === 3) {
|
|
169
|
+
const oldDetails = value;
|
|
170
|
+
tokenDetails = {
|
|
171
|
+
token: oldDetails.fcmToken,
|
|
172
|
+
createTime: oldDetails.createTime,
|
|
173
|
+
subscriptionOptions: {
|
|
174
|
+
auth: arrayToBase64(oldDetails.auth),
|
|
175
|
+
p256dh: arrayToBase64(oldDetails.p256dh),
|
|
176
|
+
endpoint: oldDetails.endpoint,
|
|
177
|
+
swScope: oldDetails.swScope,
|
|
178
|
+
vapidKey: arrayToBase64(oldDetails.vapidKey)
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
else if (oldVersion === 4) {
|
|
183
|
+
const oldDetails = value;
|
|
184
|
+
tokenDetails = {
|
|
185
|
+
token: oldDetails.fcmToken,
|
|
186
|
+
createTime: oldDetails.createTime,
|
|
187
|
+
subscriptionOptions: {
|
|
188
|
+
auth: arrayToBase64(oldDetails.auth),
|
|
189
|
+
p256dh: arrayToBase64(oldDetails.p256dh),
|
|
190
|
+
endpoint: oldDetails.endpoint,
|
|
191
|
+
swScope: oldDetails.swScope,
|
|
192
|
+
vapidKey: arrayToBase64(oldDetails.vapidKey)
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
}
|
|
234
197
|
});
|
|
198
|
+
db.close();
|
|
199
|
+
// Delete all old databases.
|
|
200
|
+
await idb.deleteDB(OLD_DB_NAME);
|
|
201
|
+
await idb.deleteDB('fcm_vapid_details_db');
|
|
202
|
+
await idb.deleteDB('undefined');
|
|
203
|
+
return checkTokenDetails(tokenDetails) ? tokenDetails : null;
|
|
235
204
|
}
|
|
236
205
|
function checkTokenDetails(tokenDetails) {
|
|
237
206
|
if (!tokenDetails || !tokenDetails.subscriptionOptions) {
|
|
238
207
|
return false;
|
|
239
208
|
}
|
|
240
|
-
|
|
209
|
+
const { subscriptionOptions } = tokenDetails;
|
|
241
210
|
return (typeof tokenDetails.createTime === 'number' &&
|
|
242
211
|
tokenDetails.createTime > 0 &&
|
|
243
212
|
typeof tokenDetails.token === 'string' &&
|
|
@@ -271,14 +240,14 @@ function checkTokenDetails(tokenDetails) {
|
|
|
271
240
|
* limitations under the License.
|
|
272
241
|
*/
|
|
273
242
|
// Exported for tests.
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
243
|
+
const DATABASE_NAME = 'firebase-messaging-database';
|
|
244
|
+
const DATABASE_VERSION = 1;
|
|
245
|
+
const OBJECT_STORE_NAME = 'firebase-messaging-store';
|
|
246
|
+
let dbPromise = null;
|
|
278
247
|
function getDbPromise() {
|
|
279
248
|
if (!dbPromise) {
|
|
280
249
|
dbPromise = idb.openDB(DATABASE_NAME, DATABASE_VERSION, {
|
|
281
|
-
upgrade:
|
|
250
|
+
upgrade: (upgradeDb, oldVersion) => {
|
|
282
251
|
// We don't use 'break' in this switch statement, the fall-through behavior is what we want,
|
|
283
252
|
// because if there are multiple versions between the old version and the current version, we
|
|
284
253
|
// want ALL the migrations that correspond to those versions to run, not only the last one.
|
|
@@ -293,85 +262,43 @@ function getDbPromise() {
|
|
|
293
262
|
return dbPromise;
|
|
294
263
|
}
|
|
295
264
|
/** Gets record(s) from the objectStore that match the given key. */
|
|
296
|
-
function dbGet(firebaseDependencies) {
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
case 3: return [4 /*yield*/, migrateOldDatabase(firebaseDependencies.appConfig.senderId)];
|
|
315
|
-
case 4:
|
|
316
|
-
oldTokenDetails = _a.sent();
|
|
317
|
-
if (!oldTokenDetails) return [3 /*break*/, 6];
|
|
318
|
-
return [4 /*yield*/, dbSet(firebaseDependencies, oldTokenDetails)];
|
|
319
|
-
case 5:
|
|
320
|
-
_a.sent();
|
|
321
|
-
return [2 /*return*/, oldTokenDetails];
|
|
322
|
-
case 6: return [2 /*return*/];
|
|
323
|
-
}
|
|
324
|
-
});
|
|
325
|
-
});
|
|
265
|
+
async function dbGet(firebaseDependencies) {
|
|
266
|
+
const key = getKey(firebaseDependencies);
|
|
267
|
+
const db = await getDbPromise();
|
|
268
|
+
const tokenDetails = (await db
|
|
269
|
+
.transaction(OBJECT_STORE_NAME)
|
|
270
|
+
.objectStore(OBJECT_STORE_NAME)
|
|
271
|
+
.get(key));
|
|
272
|
+
if (tokenDetails) {
|
|
273
|
+
return tokenDetails;
|
|
274
|
+
}
|
|
275
|
+
else {
|
|
276
|
+
// Check if there is a tokenDetails object in the old DB.
|
|
277
|
+
const oldTokenDetails = await migrateOldDatabase(firebaseDependencies.appConfig.senderId);
|
|
278
|
+
if (oldTokenDetails) {
|
|
279
|
+
await dbSet(firebaseDependencies, oldTokenDetails);
|
|
280
|
+
return oldTokenDetails;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
326
283
|
}
|
|
327
284
|
/** Assigns or overwrites the record for the given key with the given value. */
|
|
328
|
-
function dbSet(firebaseDependencies, tokenDetails) {
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
return [4 /*yield*/, getDbPromise()];
|
|
336
|
-
case 1:
|
|
337
|
-
db = _a.sent();
|
|
338
|
-
tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
|
|
339
|
-
return [4 /*yield*/, tx.objectStore(OBJECT_STORE_NAME).put(tokenDetails, key)];
|
|
340
|
-
case 2:
|
|
341
|
-
_a.sent();
|
|
342
|
-
return [4 /*yield*/, tx.done];
|
|
343
|
-
case 3:
|
|
344
|
-
_a.sent();
|
|
345
|
-
return [2 /*return*/, tokenDetails];
|
|
346
|
-
}
|
|
347
|
-
});
|
|
348
|
-
});
|
|
285
|
+
async function dbSet(firebaseDependencies, tokenDetails) {
|
|
286
|
+
const key = getKey(firebaseDependencies);
|
|
287
|
+
const db = await getDbPromise();
|
|
288
|
+
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
|
|
289
|
+
await tx.objectStore(OBJECT_STORE_NAME).put(tokenDetails, key);
|
|
290
|
+
await tx.done;
|
|
291
|
+
return tokenDetails;
|
|
349
292
|
}
|
|
350
293
|
/** Removes record(s) from the objectStore that match the given key. */
|
|
351
|
-
function dbRemove(firebaseDependencies) {
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
key = getKey(firebaseDependencies);
|
|
358
|
-
return [4 /*yield*/, getDbPromise()];
|
|
359
|
-
case 1:
|
|
360
|
-
db = _a.sent();
|
|
361
|
-
tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
|
|
362
|
-
return [4 /*yield*/, tx.objectStore(OBJECT_STORE_NAME).delete(key)];
|
|
363
|
-
case 2:
|
|
364
|
-
_a.sent();
|
|
365
|
-
return [4 /*yield*/, tx.done];
|
|
366
|
-
case 3:
|
|
367
|
-
_a.sent();
|
|
368
|
-
return [2 /*return*/];
|
|
369
|
-
}
|
|
370
|
-
});
|
|
371
|
-
});
|
|
294
|
+
async function dbRemove(firebaseDependencies) {
|
|
295
|
+
const key = getKey(firebaseDependencies);
|
|
296
|
+
const db = await getDbPromise();
|
|
297
|
+
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
|
|
298
|
+
await tx.objectStore(OBJECT_STORE_NAME).delete(key);
|
|
299
|
+
await tx.done;
|
|
372
300
|
}
|
|
373
|
-
function getKey(
|
|
374
|
-
var appConfig = _a.appConfig;
|
|
301
|
+
function getKey({ appConfig }) {
|
|
375
302
|
return appConfig.appId;
|
|
376
303
|
}
|
|
377
304
|
|
|
@@ -391,31 +318,30 @@ function getKey(_a) {
|
|
|
391
318
|
* See the License for the specific language governing permissions and
|
|
392
319
|
* limitations under the License.
|
|
393
320
|
*/
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
_a["token-unsubscribe-failed" /* ErrorCode.TOKEN_UNSUBSCRIBE_FAILED */] = 'A problem occurred while unsubscribing the ' +
|
|
321
|
+
const ERROR_MAP = {
|
|
322
|
+
["missing-app-config-values" /* ErrorCode.MISSING_APP_CONFIG_VALUES */]: 'Missing App configuration value: "{$valueName}"',
|
|
323
|
+
["only-available-in-window" /* ErrorCode.AVAILABLE_IN_WINDOW */]: 'This method is available in a Window context.',
|
|
324
|
+
["only-available-in-sw" /* ErrorCode.AVAILABLE_IN_SW */]: 'This method is available in a service worker context.',
|
|
325
|
+
["permission-default" /* ErrorCode.PERMISSION_DEFAULT */]: 'The notification permission was not granted and dismissed instead.',
|
|
326
|
+
["permission-blocked" /* ErrorCode.PERMISSION_BLOCKED */]: 'The notification permission was not granted and blocked instead.',
|
|
327
|
+
["unsupported-browser" /* ErrorCode.UNSUPPORTED_BROWSER */]: "This browser doesn't support the API's required to use the Firebase SDK.",
|
|
328
|
+
["indexed-db-unsupported" /* ErrorCode.INDEXED_DB_UNSUPPORTED */]: "This browser doesn't support indexedDb.open() (ex. Safari iFrame, Firefox Private Browsing, etc)",
|
|
329
|
+
["failed-service-worker-registration" /* ErrorCode.FAILED_DEFAULT_REGISTRATION */]: 'We are unable to register the default service worker. {$browserErrorMessage}',
|
|
330
|
+
["token-subscribe-failed" /* ErrorCode.TOKEN_SUBSCRIBE_FAILED */]: 'A problem occurred while subscribing the user to FCM: {$errorInfo}',
|
|
331
|
+
["token-subscribe-no-token" /* ErrorCode.TOKEN_SUBSCRIBE_NO_TOKEN */]: 'FCM returned no token when subscribing the user to push.',
|
|
332
|
+
["token-unsubscribe-failed" /* ErrorCode.TOKEN_UNSUBSCRIBE_FAILED */]: 'A problem occurred while unsubscribing the ' +
|
|
407
333
|
'user from FCM: {$errorInfo}',
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
334
|
+
["token-update-failed" /* ErrorCode.TOKEN_UPDATE_FAILED */]: 'A problem occurred while updating the user from FCM: {$errorInfo}',
|
|
335
|
+
["token-update-no-token" /* ErrorCode.TOKEN_UPDATE_NO_TOKEN */]: 'FCM returned no token when updating the user to push.',
|
|
336
|
+
["use-sw-after-get-token" /* ErrorCode.USE_SW_AFTER_GET_TOKEN */]: 'The useServiceWorker() method may only be called once and must be ' +
|
|
411
337
|
'called before calling getToken() to ensure your service worker is used.',
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
'called before calling getToken() to ensure your VAPID key is used.'
|
|
417
|
-
|
|
418
|
-
|
|
338
|
+
["invalid-sw-registration" /* ErrorCode.INVALID_SW_REGISTRATION */]: 'The input to useServiceWorker() must be a ServiceWorkerRegistration.',
|
|
339
|
+
["invalid-bg-handler" /* ErrorCode.INVALID_BG_HANDLER */]: 'The input to setBackgroundMessageHandler() must be a function.',
|
|
340
|
+
["invalid-vapid-key" /* ErrorCode.INVALID_VAPID_KEY */]: 'The public VAPID key must be a string.',
|
|
341
|
+
["use-vapid-key-after-get-token" /* ErrorCode.USE_VAPID_KEY_AFTER_GET_TOKEN */]: 'The usePublicVapidKey() method may only be called once and must be ' +
|
|
342
|
+
'called before calling getToken() to ensure your VAPID key is used.'
|
|
343
|
+
};
|
|
344
|
+
const ERROR_FACTORY = new util.ErrorFactory('messaging', 'Messaging', ERROR_MAP);
|
|
419
345
|
|
|
420
346
|
/**
|
|
421
347
|
* @license
|
|
@@ -433,162 +359,104 @@ var ERROR_FACTORY = new util.ErrorFactory('messaging', 'Messaging', ERROR_MAP);
|
|
|
433
359
|
* See the License for the specific language governing permissions and
|
|
434
360
|
* limitations under the License.
|
|
435
361
|
*/
|
|
436
|
-
function requestGetToken(firebaseDependencies, subscriptionOptions) {
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
_a.trys.push([2, 5, , 6]);
|
|
453
|
-
return [4 /*yield*/, fetch(getEndpoint(firebaseDependencies.appConfig), subscribeOptions)];
|
|
454
|
-
case 3:
|
|
455
|
-
response = _a.sent();
|
|
456
|
-
return [4 /*yield*/, response.json()];
|
|
457
|
-
case 4:
|
|
458
|
-
responseData = _a.sent();
|
|
459
|
-
return [3 /*break*/, 6];
|
|
460
|
-
case 5:
|
|
461
|
-
err_1 = _a.sent();
|
|
462
|
-
throw ERROR_FACTORY.create("token-subscribe-failed" /* ErrorCode.TOKEN_SUBSCRIBE_FAILED */, {
|
|
463
|
-
errorInfo: err_1 === null || err_1 === void 0 ? void 0 : err_1.toString()
|
|
464
|
-
});
|
|
465
|
-
case 6:
|
|
466
|
-
if (responseData.error) {
|
|
467
|
-
message = responseData.error.message;
|
|
468
|
-
throw ERROR_FACTORY.create("token-subscribe-failed" /* ErrorCode.TOKEN_SUBSCRIBE_FAILED */, {
|
|
469
|
-
errorInfo: message
|
|
470
|
-
});
|
|
471
|
-
}
|
|
472
|
-
if (!responseData.token) {
|
|
473
|
-
throw ERROR_FACTORY.create("token-subscribe-no-token" /* ErrorCode.TOKEN_SUBSCRIBE_NO_TOKEN */);
|
|
474
|
-
}
|
|
475
|
-
return [2 /*return*/, responseData.token];
|
|
476
|
-
}
|
|
362
|
+
async function requestGetToken(firebaseDependencies, subscriptionOptions) {
|
|
363
|
+
const headers = await getHeaders(firebaseDependencies);
|
|
364
|
+
const body = getBody(subscriptionOptions);
|
|
365
|
+
const subscribeOptions = {
|
|
366
|
+
method: 'POST',
|
|
367
|
+
headers,
|
|
368
|
+
body: JSON.stringify(body)
|
|
369
|
+
};
|
|
370
|
+
let responseData;
|
|
371
|
+
try {
|
|
372
|
+
const response = await fetch(getEndpoint(firebaseDependencies.appConfig), subscribeOptions);
|
|
373
|
+
responseData = await response.json();
|
|
374
|
+
}
|
|
375
|
+
catch (err) {
|
|
376
|
+
throw ERROR_FACTORY.create("token-subscribe-failed" /* ErrorCode.TOKEN_SUBSCRIBE_FAILED */, {
|
|
377
|
+
errorInfo: err === null || err === void 0 ? void 0 : err.toString()
|
|
477
378
|
});
|
|
478
|
-
}
|
|
379
|
+
}
|
|
380
|
+
if (responseData.error) {
|
|
381
|
+
const message = responseData.error.message;
|
|
382
|
+
throw ERROR_FACTORY.create("token-subscribe-failed" /* ErrorCode.TOKEN_SUBSCRIBE_FAILED */, {
|
|
383
|
+
errorInfo: message
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
if (!responseData.token) {
|
|
387
|
+
throw ERROR_FACTORY.create("token-subscribe-no-token" /* ErrorCode.TOKEN_SUBSCRIBE_NO_TOKEN */);
|
|
388
|
+
}
|
|
389
|
+
return responseData.token;
|
|
479
390
|
}
|
|
480
|
-
function requestUpdateToken(firebaseDependencies, tokenDetails) {
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
_a.trys.push([2, 5, , 6]);
|
|
497
|
-
return [4 /*yield*/, fetch("".concat(getEndpoint(firebaseDependencies.appConfig), "/").concat(tokenDetails.token), updateOptions)];
|
|
498
|
-
case 3:
|
|
499
|
-
response = _a.sent();
|
|
500
|
-
return [4 /*yield*/, response.json()];
|
|
501
|
-
case 4:
|
|
502
|
-
responseData = _a.sent();
|
|
503
|
-
return [3 /*break*/, 6];
|
|
504
|
-
case 5:
|
|
505
|
-
err_2 = _a.sent();
|
|
506
|
-
throw ERROR_FACTORY.create("token-update-failed" /* ErrorCode.TOKEN_UPDATE_FAILED */, {
|
|
507
|
-
errorInfo: err_2 === null || err_2 === void 0 ? void 0 : err_2.toString()
|
|
508
|
-
});
|
|
509
|
-
case 6:
|
|
510
|
-
if (responseData.error) {
|
|
511
|
-
message = responseData.error.message;
|
|
512
|
-
throw ERROR_FACTORY.create("token-update-failed" /* ErrorCode.TOKEN_UPDATE_FAILED */, {
|
|
513
|
-
errorInfo: message
|
|
514
|
-
});
|
|
515
|
-
}
|
|
516
|
-
if (!responseData.token) {
|
|
517
|
-
throw ERROR_FACTORY.create("token-update-no-token" /* ErrorCode.TOKEN_UPDATE_NO_TOKEN */);
|
|
518
|
-
}
|
|
519
|
-
return [2 /*return*/, responseData.token];
|
|
520
|
-
}
|
|
391
|
+
async function requestUpdateToken(firebaseDependencies, tokenDetails) {
|
|
392
|
+
const headers = await getHeaders(firebaseDependencies);
|
|
393
|
+
const body = getBody(tokenDetails.subscriptionOptions);
|
|
394
|
+
const updateOptions = {
|
|
395
|
+
method: 'PATCH',
|
|
396
|
+
headers,
|
|
397
|
+
body: JSON.stringify(body)
|
|
398
|
+
};
|
|
399
|
+
let responseData;
|
|
400
|
+
try {
|
|
401
|
+
const response = await fetch(`${getEndpoint(firebaseDependencies.appConfig)}/${tokenDetails.token}`, updateOptions);
|
|
402
|
+
responseData = await response.json();
|
|
403
|
+
}
|
|
404
|
+
catch (err) {
|
|
405
|
+
throw ERROR_FACTORY.create("token-update-failed" /* ErrorCode.TOKEN_UPDATE_FAILED */, {
|
|
406
|
+
errorInfo: err === null || err === void 0 ? void 0 : err.toString()
|
|
521
407
|
});
|
|
522
|
-
}
|
|
408
|
+
}
|
|
409
|
+
if (responseData.error) {
|
|
410
|
+
const message = responseData.error.message;
|
|
411
|
+
throw ERROR_FACTORY.create("token-update-failed" /* ErrorCode.TOKEN_UPDATE_FAILED */, {
|
|
412
|
+
errorInfo: message
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
if (!responseData.token) {
|
|
416
|
+
throw ERROR_FACTORY.create("token-update-no-token" /* ErrorCode.TOKEN_UPDATE_NO_TOKEN */);
|
|
417
|
+
}
|
|
418
|
+
return responseData.token;
|
|
523
419
|
}
|
|
524
|
-
function requestDeleteToken(firebaseDependencies, token) {
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
case 4:
|
|
544
|
-
responseData = _a.sent();
|
|
545
|
-
if (responseData.error) {
|
|
546
|
-
message = responseData.error.message;
|
|
547
|
-
throw ERROR_FACTORY.create("token-unsubscribe-failed" /* ErrorCode.TOKEN_UNSUBSCRIBE_FAILED */, {
|
|
548
|
-
errorInfo: message
|
|
549
|
-
});
|
|
550
|
-
}
|
|
551
|
-
return [3 /*break*/, 6];
|
|
552
|
-
case 5:
|
|
553
|
-
err_3 = _a.sent();
|
|
554
|
-
throw ERROR_FACTORY.create("token-unsubscribe-failed" /* ErrorCode.TOKEN_UNSUBSCRIBE_FAILED */, {
|
|
555
|
-
errorInfo: err_3 === null || err_3 === void 0 ? void 0 : err_3.toString()
|
|
556
|
-
});
|
|
557
|
-
case 6: return [2 /*return*/];
|
|
558
|
-
}
|
|
420
|
+
async function requestDeleteToken(firebaseDependencies, token) {
|
|
421
|
+
const headers = await getHeaders(firebaseDependencies);
|
|
422
|
+
const unsubscribeOptions = {
|
|
423
|
+
method: 'DELETE',
|
|
424
|
+
headers
|
|
425
|
+
};
|
|
426
|
+
try {
|
|
427
|
+
const response = await fetch(`${getEndpoint(firebaseDependencies.appConfig)}/${token}`, unsubscribeOptions);
|
|
428
|
+
const responseData = await response.json();
|
|
429
|
+
if (responseData.error) {
|
|
430
|
+
const message = responseData.error.message;
|
|
431
|
+
throw ERROR_FACTORY.create("token-unsubscribe-failed" /* ErrorCode.TOKEN_UNSUBSCRIBE_FAILED */, {
|
|
432
|
+
errorInfo: message
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
catch (err) {
|
|
437
|
+
throw ERROR_FACTORY.create("token-unsubscribe-failed" /* ErrorCode.TOKEN_UNSUBSCRIBE_FAILED */, {
|
|
438
|
+
errorInfo: err === null || err === void 0 ? void 0 : err.toString()
|
|
559
439
|
});
|
|
560
|
-
}
|
|
440
|
+
}
|
|
561
441
|
}
|
|
562
|
-
function getEndpoint(
|
|
563
|
-
|
|
564
|
-
return "".concat(ENDPOINT, "/projects/").concat(projectId, "/registrations");
|
|
442
|
+
function getEndpoint({ projectId }) {
|
|
443
|
+
return `${ENDPOINT}/projects/${projectId}/registrations`;
|
|
565
444
|
}
|
|
566
|
-
function getHeaders(
|
|
567
|
-
|
|
568
|
-
return
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
case 1:
|
|
574
|
-
authToken = _b.sent();
|
|
575
|
-
return [2 /*return*/, new Headers({
|
|
576
|
-
'Content-Type': 'application/json',
|
|
577
|
-
Accept: 'application/json',
|
|
578
|
-
'x-goog-api-key': appConfig.apiKey,
|
|
579
|
-
'x-goog-firebase-installations-auth': "FIS ".concat(authToken)
|
|
580
|
-
})];
|
|
581
|
-
}
|
|
582
|
-
});
|
|
445
|
+
async function getHeaders({ appConfig, installations }) {
|
|
446
|
+
const authToken = await installations.getToken();
|
|
447
|
+
return new Headers({
|
|
448
|
+
'Content-Type': 'application/json',
|
|
449
|
+
Accept: 'application/json',
|
|
450
|
+
'x-goog-api-key': appConfig.apiKey,
|
|
451
|
+
'x-goog-firebase-installations-auth': `FIS ${authToken}`
|
|
583
452
|
});
|
|
584
453
|
}
|
|
585
|
-
function getBody(
|
|
586
|
-
|
|
587
|
-
var body = {
|
|
454
|
+
function getBody({ p256dh, auth, endpoint, vapidKey }) {
|
|
455
|
+
const body = {
|
|
588
456
|
web: {
|
|
589
|
-
endpoint
|
|
590
|
-
auth
|
|
591
|
-
p256dh
|
|
457
|
+
endpoint,
|
|
458
|
+
auth,
|
|
459
|
+
p256dh
|
|
592
460
|
}
|
|
593
461
|
};
|
|
594
462
|
if (vapidKey !== DEFAULT_VAPID_KEY) {
|
|
@@ -614,169 +482,107 @@ function getBody(_a) {
|
|
|
614
482
|
* limitations under the License.
|
|
615
483
|
*/
|
|
616
484
|
// UpdateRegistration will be called once every week.
|
|
617
|
-
|
|
618
|
-
function getTokenInternal(messaging) {
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
e_1 = _a.sent();
|
|
650
|
-
// Suppress errors because of #2364
|
|
651
|
-
console.warn(e_1);
|
|
652
|
-
return [3 /*break*/, 7];
|
|
653
|
-
case 7: return [2 /*return*/, getNewToken(messaging.firebaseDependencies, subscriptionOptions)];
|
|
654
|
-
case 8:
|
|
655
|
-
if (Date.now() >= tokenDetails.createTime + TOKEN_EXPIRATION_MS) {
|
|
656
|
-
// Weekly token refresh
|
|
657
|
-
return [2 /*return*/, updateToken(messaging, {
|
|
658
|
-
token: tokenDetails.token,
|
|
659
|
-
createTime: Date.now(),
|
|
660
|
-
subscriptionOptions: subscriptionOptions
|
|
661
|
-
})];
|
|
662
|
-
}
|
|
663
|
-
else {
|
|
664
|
-
// Valid token, nothing to do.
|
|
665
|
-
return [2 /*return*/, tokenDetails.token];
|
|
666
|
-
}
|
|
667
|
-
case 9: return [2 /*return*/];
|
|
668
|
-
}
|
|
485
|
+
const TOKEN_EXPIRATION_MS = 7 * 24 * 60 * 60 * 1000; // 7 days
|
|
486
|
+
async function getTokenInternal(messaging) {
|
|
487
|
+
const pushSubscription = await getPushSubscription(messaging.swRegistration, messaging.vapidKey);
|
|
488
|
+
const subscriptionOptions = {
|
|
489
|
+
vapidKey: messaging.vapidKey,
|
|
490
|
+
swScope: messaging.swRegistration.scope,
|
|
491
|
+
endpoint: pushSubscription.endpoint,
|
|
492
|
+
auth: arrayToBase64(pushSubscription.getKey('auth')),
|
|
493
|
+
p256dh: arrayToBase64(pushSubscription.getKey('p256dh'))
|
|
494
|
+
};
|
|
495
|
+
const tokenDetails = await dbGet(messaging.firebaseDependencies);
|
|
496
|
+
if (!tokenDetails) {
|
|
497
|
+
// No token, get a new one.
|
|
498
|
+
return getNewToken(messaging.firebaseDependencies, subscriptionOptions);
|
|
499
|
+
}
|
|
500
|
+
else if (!isTokenValid(tokenDetails.subscriptionOptions, subscriptionOptions)) {
|
|
501
|
+
// Invalid token, get a new one.
|
|
502
|
+
try {
|
|
503
|
+
await requestDeleteToken(messaging.firebaseDependencies, tokenDetails.token);
|
|
504
|
+
}
|
|
505
|
+
catch (e) {
|
|
506
|
+
// Suppress errors because of #2364
|
|
507
|
+
console.warn(e);
|
|
508
|
+
}
|
|
509
|
+
return getNewToken(messaging.firebaseDependencies, subscriptionOptions);
|
|
510
|
+
}
|
|
511
|
+
else if (Date.now() >= tokenDetails.createTime + TOKEN_EXPIRATION_MS) {
|
|
512
|
+
// Weekly token refresh
|
|
513
|
+
return updateToken(messaging, {
|
|
514
|
+
token: tokenDetails.token,
|
|
515
|
+
createTime: Date.now(),
|
|
516
|
+
subscriptionOptions
|
|
669
517
|
});
|
|
670
|
-
}
|
|
518
|
+
}
|
|
519
|
+
else {
|
|
520
|
+
// Valid token, nothing to do.
|
|
521
|
+
return tokenDetails.token;
|
|
522
|
+
}
|
|
671
523
|
}
|
|
672
524
|
/**
|
|
673
525
|
* This method deletes the token from the database, unsubscribes the token from FCM, and unregisters
|
|
674
526
|
* the push subscription if it exists.
|
|
675
527
|
*/
|
|
676
|
-
function deleteTokenInternal(messaging) {
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
case 3:
|
|
690
|
-
_a.sent();
|
|
691
|
-
_a.label = 4;
|
|
692
|
-
case 4: return [4 /*yield*/, messaging.swRegistration.pushManager.getSubscription()];
|
|
693
|
-
case 5:
|
|
694
|
-
pushSubscription = _a.sent();
|
|
695
|
-
if (pushSubscription) {
|
|
696
|
-
return [2 /*return*/, pushSubscription.unsubscribe()];
|
|
697
|
-
}
|
|
698
|
-
// If there's no SW, consider it a success.
|
|
699
|
-
return [2 /*return*/, true];
|
|
700
|
-
}
|
|
701
|
-
});
|
|
702
|
-
});
|
|
528
|
+
async function deleteTokenInternal(messaging) {
|
|
529
|
+
const tokenDetails = await dbGet(messaging.firebaseDependencies);
|
|
530
|
+
if (tokenDetails) {
|
|
531
|
+
await requestDeleteToken(messaging.firebaseDependencies, tokenDetails.token);
|
|
532
|
+
await dbRemove(messaging.firebaseDependencies);
|
|
533
|
+
}
|
|
534
|
+
// Unsubscribe from the push subscription.
|
|
535
|
+
const pushSubscription = await messaging.swRegistration.pushManager.getSubscription();
|
|
536
|
+
if (pushSubscription) {
|
|
537
|
+
return pushSubscription.unsubscribe();
|
|
538
|
+
}
|
|
539
|
+
// If there's no SW, consider it a success.
|
|
540
|
+
return true;
|
|
703
541
|
}
|
|
704
|
-
function updateToken(messaging, tokenDetails) {
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
updatedTokenDetails = tslib.__assign(tslib.__assign({}, tokenDetails), { token: updatedToken, createTime: Date.now() });
|
|
715
|
-
return [4 /*yield*/, dbSet(messaging.firebaseDependencies, updatedTokenDetails)];
|
|
716
|
-
case 2:
|
|
717
|
-
_a.sent();
|
|
718
|
-
return [2 /*return*/, updatedToken];
|
|
719
|
-
case 3:
|
|
720
|
-
e_2 = _a.sent();
|
|
721
|
-
throw e_2;
|
|
722
|
-
case 4: return [2 /*return*/];
|
|
723
|
-
}
|
|
724
|
-
});
|
|
725
|
-
});
|
|
542
|
+
async function updateToken(messaging, tokenDetails) {
|
|
543
|
+
try {
|
|
544
|
+
const updatedToken = await requestUpdateToken(messaging.firebaseDependencies, tokenDetails);
|
|
545
|
+
const updatedTokenDetails = Object.assign(Object.assign({}, tokenDetails), { token: updatedToken, createTime: Date.now() });
|
|
546
|
+
await dbSet(messaging.firebaseDependencies, updatedTokenDetails);
|
|
547
|
+
return updatedToken;
|
|
548
|
+
}
|
|
549
|
+
catch (e) {
|
|
550
|
+
throw e;
|
|
551
|
+
}
|
|
726
552
|
}
|
|
727
|
-
function getNewToken(firebaseDependencies, subscriptionOptions) {
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
token: token,
|
|
737
|
-
createTime: Date.now(),
|
|
738
|
-
subscriptionOptions: subscriptionOptions
|
|
739
|
-
};
|
|
740
|
-
return [4 /*yield*/, dbSet(firebaseDependencies, tokenDetails)];
|
|
741
|
-
case 2:
|
|
742
|
-
_a.sent();
|
|
743
|
-
return [2 /*return*/, tokenDetails.token];
|
|
744
|
-
}
|
|
745
|
-
});
|
|
746
|
-
});
|
|
553
|
+
async function getNewToken(firebaseDependencies, subscriptionOptions) {
|
|
554
|
+
const token = await requestGetToken(firebaseDependencies, subscriptionOptions);
|
|
555
|
+
const tokenDetails = {
|
|
556
|
+
token,
|
|
557
|
+
createTime: Date.now(),
|
|
558
|
+
subscriptionOptions
|
|
559
|
+
};
|
|
560
|
+
await dbSet(firebaseDependencies, tokenDetails);
|
|
561
|
+
return tokenDetails.token;
|
|
747
562
|
}
|
|
748
563
|
/**
|
|
749
564
|
* Gets a PushSubscription for the current user.
|
|
750
565
|
*/
|
|
751
|
-
function getPushSubscription(swRegistration, vapidKey) {
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
return
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
}
|
|
762
|
-
return [2 /*return*/, swRegistration.pushManager.subscribe({
|
|
763
|
-
userVisibleOnly: true,
|
|
764
|
-
// Chrome <= 75 doesn't support base64-encoded VAPID key. For backward compatibility, VAPID key
|
|
765
|
-
// submitted to pushManager#subscribe must be of type Uint8Array.
|
|
766
|
-
applicationServerKey: base64ToArray(vapidKey)
|
|
767
|
-
})];
|
|
768
|
-
}
|
|
769
|
-
});
|
|
566
|
+
async function getPushSubscription(swRegistration, vapidKey) {
|
|
567
|
+
const subscription = await swRegistration.pushManager.getSubscription();
|
|
568
|
+
if (subscription) {
|
|
569
|
+
return subscription;
|
|
570
|
+
}
|
|
571
|
+
return swRegistration.pushManager.subscribe({
|
|
572
|
+
userVisibleOnly: true,
|
|
573
|
+
// Chrome <= 75 doesn't support base64-encoded VAPID key. For backward compatibility, VAPID key
|
|
574
|
+
// submitted to pushManager#subscribe must be of type Uint8Array.
|
|
575
|
+
applicationServerKey: base64ToArray(vapidKey)
|
|
770
576
|
});
|
|
771
577
|
}
|
|
772
578
|
/**
|
|
773
579
|
* Checks if the saved tokenDetails object matches the configuration provided.
|
|
774
580
|
*/
|
|
775
581
|
function isTokenValid(dbOptions, currentOptions) {
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
582
|
+
const isVapidKeyEqual = currentOptions.vapidKey === dbOptions.vapidKey;
|
|
583
|
+
const isEndpointEqual = currentOptions.endpoint === dbOptions.endpoint;
|
|
584
|
+
const isAuthEqual = currentOptions.auth === dbOptions.auth;
|
|
585
|
+
const isP256dhEqual = currentOptions.p256dh === dbOptions.p256dh;
|
|
780
586
|
return isVapidKeyEqual && isEndpointEqual && isAuthEqual && isP256dhEqual;
|
|
781
587
|
}
|
|
782
588
|
|
|
@@ -797,7 +603,7 @@ function isTokenValid(dbOptions, currentOptions) {
|
|
|
797
603
|
* limitations under the License.
|
|
798
604
|
*/
|
|
799
605
|
function externalizePayload(internalPayload) {
|
|
800
|
-
|
|
606
|
+
const payload = {
|
|
801
607
|
from: internalPayload.from,
|
|
802
608
|
// eslint-disable-next-line camelcase
|
|
803
609
|
collapseKey: internalPayload.collapse_key,
|
|
@@ -814,19 +620,19 @@ function propagateNotificationPayload(payload, messagePayloadInternal) {
|
|
|
814
620
|
return;
|
|
815
621
|
}
|
|
816
622
|
payload.notification = {};
|
|
817
|
-
|
|
623
|
+
const title = messagePayloadInternal.notification.title;
|
|
818
624
|
if (!!title) {
|
|
819
625
|
payload.notification.title = title;
|
|
820
626
|
}
|
|
821
|
-
|
|
627
|
+
const body = messagePayloadInternal.notification.body;
|
|
822
628
|
if (!!body) {
|
|
823
629
|
payload.notification.body = body;
|
|
824
630
|
}
|
|
825
|
-
|
|
631
|
+
const image = messagePayloadInternal.notification.image;
|
|
826
632
|
if (!!image) {
|
|
827
633
|
payload.notification.image = image;
|
|
828
634
|
}
|
|
829
|
-
|
|
635
|
+
const icon = messagePayloadInternal.notification.icon;
|
|
830
636
|
if (!!icon) {
|
|
831
637
|
payload.notification.icon = icon;
|
|
832
638
|
}
|
|
@@ -845,12 +651,12 @@ function propagateFcmOptions(payload, messagePayloadInternal) {
|
|
|
845
651
|
return;
|
|
846
652
|
}
|
|
847
653
|
payload.fcmOptions = {};
|
|
848
|
-
|
|
654
|
+
const link = (_c = (_b = messagePayloadInternal.fcmOptions) === null || _b === void 0 ? void 0 : _b.link) !== null && _c !== void 0 ? _c : (_d = messagePayloadInternal.notification) === null || _d === void 0 ? void 0 : _d.click_action;
|
|
849
655
|
if (!!link) {
|
|
850
656
|
payload.fcmOptions.link = link;
|
|
851
657
|
}
|
|
852
658
|
// eslint-disable-next-line camelcase
|
|
853
|
-
|
|
659
|
+
const analyticsLabel = (_e = messagePayloadInternal.fcmOptions) === null || _e === void 0 ? void 0 : _e.analytics_label;
|
|
854
660
|
if (!!analyticsLabel) {
|
|
855
661
|
payload.fcmOptions.analyticsLabel = analyticsLabel;
|
|
856
662
|
}
|
|
@@ -895,8 +701,8 @@ function isConsoleMessage(data) {
|
|
|
895
701
|
*/
|
|
896
702
|
_mergeStrings('AzSCbw63g1R0nCw85jG8', 'Iaya3yLKwmgvh7cF0q4');
|
|
897
703
|
function _mergeStrings(s1, s2) {
|
|
898
|
-
|
|
899
|
-
for (
|
|
704
|
+
const resultArray = [];
|
|
705
|
+
for (let i = 0; i < s1.length; i++) {
|
|
900
706
|
resultArray.push(s1.charAt(i));
|
|
901
707
|
if (i < s2.length) {
|
|
902
708
|
resultArray.push(s2.charAt(i));
|
|
@@ -922,7 +728,6 @@ function _mergeStrings(s1, s2) {
|
|
|
922
728
|
* limitations under the License.
|
|
923
729
|
*/
|
|
924
730
|
function extractAppConfig(app) {
|
|
925
|
-
var e_1, _a;
|
|
926
731
|
if (!app || !app.options) {
|
|
927
732
|
throw getMissingValueError('App Configuration Object');
|
|
928
733
|
}
|
|
@@ -930,28 +735,18 @@ function extractAppConfig(app) {
|
|
|
930
735
|
throw getMissingValueError('App Name');
|
|
931
736
|
}
|
|
932
737
|
// Required app config keys
|
|
933
|
-
|
|
738
|
+
const configKeys = [
|
|
934
739
|
'projectId',
|
|
935
740
|
'apiKey',
|
|
936
741
|
'appId',
|
|
937
742
|
'messagingSenderId'
|
|
938
743
|
];
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
if (!options[keyName]) {
|
|
944
|
-
throw getMissingValueError(keyName);
|
|
945
|
-
}
|
|
744
|
+
const { options } = app;
|
|
745
|
+
for (const keyName of configKeys) {
|
|
746
|
+
if (!options[keyName]) {
|
|
747
|
+
throw getMissingValueError(keyName);
|
|
946
748
|
}
|
|
947
749
|
}
|
|
948
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
949
|
-
finally {
|
|
950
|
-
try {
|
|
951
|
-
if (configKeys_1_1 && !configKeys_1_1.done && (_a = configKeys_1.return)) _a.call(configKeys_1);
|
|
952
|
-
}
|
|
953
|
-
finally { if (e_1) throw e_1.error; }
|
|
954
|
-
}
|
|
955
750
|
return {
|
|
956
751
|
appName: app.name,
|
|
957
752
|
projectId: options.projectId,
|
|
@@ -962,7 +757,7 @@ function extractAppConfig(app) {
|
|
|
962
757
|
}
|
|
963
758
|
function getMissingValueError(valueName) {
|
|
964
759
|
return ERROR_FACTORY.create("missing-app-config-values" /* ErrorCode.MISSING_APP_CONFIG_VALUES */, {
|
|
965
|
-
valueName
|
|
760
|
+
valueName
|
|
966
761
|
});
|
|
967
762
|
}
|
|
968
763
|
|
|
@@ -982,27 +777,26 @@ function getMissingValueError(valueName) {
|
|
|
982
777
|
* See the License for the specific language governing permissions and
|
|
983
778
|
* limitations under the License.
|
|
984
779
|
*/
|
|
985
|
-
|
|
986
|
-
|
|
780
|
+
class MessagingService {
|
|
781
|
+
constructor(app, installations, analyticsProvider) {
|
|
987
782
|
// logging is only done with end user consent. Default to false.
|
|
988
783
|
this.deliveryMetricsExportedToBigQueryEnabled = false;
|
|
989
784
|
this.onBackgroundMessageHandler = null;
|
|
990
785
|
this.onMessageHandler = null;
|
|
991
786
|
this.logEvents = [];
|
|
992
787
|
this.isLogServiceStarted = false;
|
|
993
|
-
|
|
788
|
+
const appConfig = extractAppConfig(app);
|
|
994
789
|
this.firebaseDependencies = {
|
|
995
|
-
app
|
|
996
|
-
appConfig
|
|
997
|
-
installations
|
|
998
|
-
analyticsProvider
|
|
790
|
+
app,
|
|
791
|
+
appConfig,
|
|
792
|
+
installations,
|
|
793
|
+
analyticsProvider
|
|
999
794
|
};
|
|
1000
795
|
}
|
|
1001
|
-
|
|
796
|
+
_delete() {
|
|
1002
797
|
return Promise.resolve();
|
|
1003
|
-
}
|
|
1004
|
-
|
|
1005
|
-
}());
|
|
798
|
+
}
|
|
799
|
+
}
|
|
1006
800
|
|
|
1007
801
|
/**
|
|
1008
802
|
* @license
|
|
@@ -1020,37 +814,25 @@ var MessagingService = /** @class */ (function () {
|
|
|
1020
814
|
* See the License for the specific language governing permissions and
|
|
1021
815
|
* limitations under the License.
|
|
1022
816
|
*/
|
|
1023
|
-
function registerDefaultSw(messaging) {
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
switch (_b.label) {
|
|
1028
|
-
case 0:
|
|
1029
|
-
_b.trys.push([0, 2, , 3]);
|
|
1030
|
-
_a = messaging;
|
|
1031
|
-
return [4 /*yield*/, navigator.serviceWorker.register(DEFAULT_SW_PATH, {
|
|
1032
|
-
scope: DEFAULT_SW_SCOPE
|
|
1033
|
-
})];
|
|
1034
|
-
case 1:
|
|
1035
|
-
_a.swRegistration = _b.sent();
|
|
1036
|
-
// The timing when browser updates sw when sw has an update is unreliable from experiment. It
|
|
1037
|
-
// leads to version conflict when the SDK upgrades to a newer version in the main page, but sw
|
|
1038
|
-
// is stuck with the old version. For example,
|
|
1039
|
-
// https://github.com/firebase/firebase-js-sdk/issues/2590 The following line reliably updates
|
|
1040
|
-
// sw if there was an update.
|
|
1041
|
-
messaging.swRegistration.update().catch(function () {
|
|
1042
|
-
/* it is non blocking and we don't care if it failed */
|
|
1043
|
-
});
|
|
1044
|
-
return [3 /*break*/, 3];
|
|
1045
|
-
case 2:
|
|
1046
|
-
e_1 = _b.sent();
|
|
1047
|
-
throw ERROR_FACTORY.create("failed-service-worker-registration" /* ErrorCode.FAILED_DEFAULT_REGISTRATION */, {
|
|
1048
|
-
browserErrorMessage: e_1 === null || e_1 === void 0 ? void 0 : e_1.message
|
|
1049
|
-
});
|
|
1050
|
-
case 3: return [2 /*return*/];
|
|
1051
|
-
}
|
|
817
|
+
async function registerDefaultSw(messaging) {
|
|
818
|
+
try {
|
|
819
|
+
messaging.swRegistration = await navigator.serviceWorker.register(DEFAULT_SW_PATH, {
|
|
820
|
+
scope: DEFAULT_SW_SCOPE
|
|
1052
821
|
});
|
|
1053
|
-
|
|
822
|
+
// The timing when browser updates sw when sw has an update is unreliable from experiment. It
|
|
823
|
+
// leads to version conflict when the SDK upgrades to a newer version in the main page, but sw
|
|
824
|
+
// is stuck with the old version. For example,
|
|
825
|
+
// https://github.com/firebase/firebase-js-sdk/issues/2590 The following line reliably updates
|
|
826
|
+
// sw if there was an update.
|
|
827
|
+
messaging.swRegistration.update().catch(() => {
|
|
828
|
+
/* it is non blocking and we don't care if it failed */
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
catch (e) {
|
|
832
|
+
throw ERROR_FACTORY.create("failed-service-worker-registration" /* ErrorCode.FAILED_DEFAULT_REGISTRATION */, {
|
|
833
|
+
browserErrorMessage: e === null || e === void 0 ? void 0 : e.message
|
|
834
|
+
});
|
|
835
|
+
}
|
|
1054
836
|
}
|
|
1055
837
|
|
|
1056
838
|
/**
|
|
@@ -1069,28 +851,17 @@ function registerDefaultSw(messaging) {
|
|
|
1069
851
|
* See the License for the specific language governing permissions and
|
|
1070
852
|
* limitations under the License.
|
|
1071
853
|
*/
|
|
1072
|
-
function updateSwReg(messaging, swRegistration) {
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
if (!swRegistration && !!messaging.swRegistration) {
|
|
1084
|
-
return [2 /*return*/];
|
|
1085
|
-
}
|
|
1086
|
-
if (!(swRegistration instanceof ServiceWorkerRegistration)) {
|
|
1087
|
-
throw ERROR_FACTORY.create("invalid-sw-registration" /* ErrorCode.INVALID_SW_REGISTRATION */);
|
|
1088
|
-
}
|
|
1089
|
-
messaging.swRegistration = swRegistration;
|
|
1090
|
-
return [2 /*return*/];
|
|
1091
|
-
}
|
|
1092
|
-
});
|
|
1093
|
-
});
|
|
854
|
+
async function updateSwReg(messaging, swRegistration) {
|
|
855
|
+
if (!swRegistration && !messaging.swRegistration) {
|
|
856
|
+
await registerDefaultSw(messaging);
|
|
857
|
+
}
|
|
858
|
+
if (!swRegistration && !!messaging.swRegistration) {
|
|
859
|
+
return;
|
|
860
|
+
}
|
|
861
|
+
if (!(swRegistration instanceof ServiceWorkerRegistration)) {
|
|
862
|
+
throw ERROR_FACTORY.create("invalid-sw-registration" /* ErrorCode.INVALID_SW_REGISTRATION */);
|
|
863
|
+
}
|
|
864
|
+
messaging.swRegistration = swRegistration;
|
|
1094
865
|
}
|
|
1095
866
|
|
|
1096
867
|
/**
|
|
@@ -1109,18 +880,13 @@ function updateSwReg(messaging, swRegistration) {
|
|
|
1109
880
|
* See the License for the specific language governing permissions and
|
|
1110
881
|
* limitations under the License.
|
|
1111
882
|
*/
|
|
1112
|
-
function updateVapidKey(messaging, vapidKey) {
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
messaging.vapidKey = DEFAULT_VAPID_KEY;
|
|
1120
|
-
}
|
|
1121
|
-
return [2 /*return*/];
|
|
1122
|
-
});
|
|
1123
|
-
});
|
|
883
|
+
async function updateVapidKey(messaging, vapidKey) {
|
|
884
|
+
if (!!vapidKey) {
|
|
885
|
+
messaging.vapidKey = vapidKey;
|
|
886
|
+
}
|
|
887
|
+
else if (!messaging.vapidKey) {
|
|
888
|
+
messaging.vapidKey = DEFAULT_VAPID_KEY;
|
|
889
|
+
}
|
|
1124
890
|
}
|
|
1125
891
|
|
|
1126
892
|
/**
|
|
@@ -1139,33 +905,19 @@ function updateVapidKey(messaging, vapidKey) {
|
|
|
1139
905
|
* See the License for the specific language governing permissions and
|
|
1140
906
|
* limitations under the License.
|
|
1141
907
|
*/
|
|
1142
|
-
function getToken$1(messaging, options) {
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
case 2:
|
|
1156
|
-
if (Notification.permission !== 'granted') {
|
|
1157
|
-
throw ERROR_FACTORY.create("permission-blocked" /* ErrorCode.PERMISSION_BLOCKED */);
|
|
1158
|
-
}
|
|
1159
|
-
return [4 /*yield*/, updateVapidKey(messaging, options === null || options === void 0 ? void 0 : options.vapidKey)];
|
|
1160
|
-
case 3:
|
|
1161
|
-
_a.sent();
|
|
1162
|
-
return [4 /*yield*/, updateSwReg(messaging, options === null || options === void 0 ? void 0 : options.serviceWorkerRegistration)];
|
|
1163
|
-
case 4:
|
|
1164
|
-
_a.sent();
|
|
1165
|
-
return [2 /*return*/, getTokenInternal(messaging)];
|
|
1166
|
-
}
|
|
1167
|
-
});
|
|
1168
|
-
});
|
|
908
|
+
async function getToken$1(messaging, options) {
|
|
909
|
+
if (!navigator) {
|
|
910
|
+
throw ERROR_FACTORY.create("only-available-in-window" /* ErrorCode.AVAILABLE_IN_WINDOW */);
|
|
911
|
+
}
|
|
912
|
+
if (Notification.permission === 'default') {
|
|
913
|
+
await Notification.requestPermission();
|
|
914
|
+
}
|
|
915
|
+
if (Notification.permission !== 'granted') {
|
|
916
|
+
throw ERROR_FACTORY.create("permission-blocked" /* ErrorCode.PERMISSION_BLOCKED */);
|
|
917
|
+
}
|
|
918
|
+
await updateVapidKey(messaging, options === null || options === void 0 ? void 0 : options.vapidKey);
|
|
919
|
+
await updateSwReg(messaging, options === null || options === void 0 ? void 0 : options.serviceWorkerRegistration);
|
|
920
|
+
return getTokenInternal(messaging);
|
|
1169
921
|
}
|
|
1170
922
|
|
|
1171
923
|
/**
|
|
@@ -1184,27 +936,16 @@ function getToken$1(messaging, options) {
|
|
|
1184
936
|
* See the License for the specific language governing permissions and
|
|
1185
937
|
* limitations under the License.
|
|
1186
938
|
*/
|
|
1187
|
-
function logToScion(messaging, messageType, data) {
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
analytics.logEvent(eventType, {
|
|
1198
|
-
/* eslint-disable camelcase */
|
|
1199
|
-
message_id: data[CONSOLE_CAMPAIGN_ID],
|
|
1200
|
-
message_name: data[CONSOLE_CAMPAIGN_NAME],
|
|
1201
|
-
message_time: data[CONSOLE_CAMPAIGN_TIME],
|
|
1202
|
-
message_device_time: Math.floor(Date.now() / 1000)
|
|
1203
|
-
/* eslint-enable camelcase */
|
|
1204
|
-
});
|
|
1205
|
-
return [2 /*return*/];
|
|
1206
|
-
}
|
|
1207
|
-
});
|
|
939
|
+
async function logToScion(messaging, messageType, data) {
|
|
940
|
+
const eventType = getEventType(messageType);
|
|
941
|
+
const analytics = await messaging.firebaseDependencies.analyticsProvider.get();
|
|
942
|
+
analytics.logEvent(eventType, {
|
|
943
|
+
/* eslint-disable camelcase */
|
|
944
|
+
message_id: data[CONSOLE_CAMPAIGN_ID],
|
|
945
|
+
message_name: data[CONSOLE_CAMPAIGN_NAME],
|
|
946
|
+
message_time: data[CONSOLE_CAMPAIGN_TIME],
|
|
947
|
+
message_device_time: Math.floor(Date.now() / 1000)
|
|
948
|
+
/* eslint-enable camelcase */
|
|
1208
949
|
});
|
|
1209
950
|
}
|
|
1210
951
|
function getEventType(messageType) {
|
|
@@ -1234,40 +975,30 @@ function getEventType(messageType) {
|
|
|
1234
975
|
* See the License for the specific language governing permissions and
|
|
1235
976
|
* limitations under the License.
|
|
1236
977
|
*/
|
|
1237
|
-
function messageEventListener(messaging, event) {
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
return
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
if (!(isConsoleMessage(dataPayload) &&
|
|
1258
|
-
dataPayload[CONSOLE_CAMPAIGN_ANALYTICS_ENABLED] === '1')) return [3 /*break*/, 2];
|
|
1259
|
-
return [4 /*yield*/, logToScion(messaging, internalPayload.messageType, dataPayload)];
|
|
1260
|
-
case 1:
|
|
1261
|
-
_a.sent();
|
|
1262
|
-
_a.label = 2;
|
|
1263
|
-
case 2: return [2 /*return*/];
|
|
1264
|
-
}
|
|
1265
|
-
});
|
|
1266
|
-
});
|
|
978
|
+
async function messageEventListener(messaging, event) {
|
|
979
|
+
const internalPayload = event.data;
|
|
980
|
+
if (!internalPayload.isFirebaseMessaging) {
|
|
981
|
+
return;
|
|
982
|
+
}
|
|
983
|
+
if (messaging.onMessageHandler &&
|
|
984
|
+
internalPayload.messageType === MessageType.PUSH_RECEIVED) {
|
|
985
|
+
if (typeof messaging.onMessageHandler === 'function') {
|
|
986
|
+
messaging.onMessageHandler(externalizePayload(internalPayload));
|
|
987
|
+
}
|
|
988
|
+
else {
|
|
989
|
+
messaging.onMessageHandler.next(externalizePayload(internalPayload));
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
// Log to Scion if applicable
|
|
993
|
+
const dataPayload = internalPayload.data;
|
|
994
|
+
if (isConsoleMessage(dataPayload) &&
|
|
995
|
+
dataPayload[CONSOLE_CAMPAIGN_ANALYTICS_ENABLED] === '1') {
|
|
996
|
+
await logToScion(messaging, internalPayload.messageType, dataPayload);
|
|
997
|
+
}
|
|
1267
998
|
}
|
|
1268
999
|
|
|
1269
|
-
|
|
1270
|
-
|
|
1000
|
+
const name = "@firebase/messaging";
|
|
1001
|
+
const version = "0.12.13";
|
|
1271
1002
|
|
|
1272
1003
|
/**
|
|
1273
1004
|
* @license
|
|
@@ -1285,19 +1016,17 @@ var version = "0.12.12";
|
|
|
1285
1016
|
* See the License for the specific language governing permissions and
|
|
1286
1017
|
* limitations under the License.
|
|
1287
1018
|
*/
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
navigator.serviceWorker.addEventListener('message',
|
|
1291
|
-
return messageEventListener(messaging, e);
|
|
1292
|
-
});
|
|
1019
|
+
const WindowMessagingFactory = (container) => {
|
|
1020
|
+
const messaging = new MessagingService(container.getProvider('app').getImmediate(), container.getProvider('installations-internal').getImmediate(), container.getProvider('analytics-internal'));
|
|
1021
|
+
navigator.serviceWorker.addEventListener('message', e => messageEventListener(messaging, e));
|
|
1293
1022
|
return messaging;
|
|
1294
1023
|
};
|
|
1295
|
-
|
|
1296
|
-
|
|
1024
|
+
const WindowMessagingInternalFactory = (container) => {
|
|
1025
|
+
const messaging = container
|
|
1297
1026
|
.getProvider('messaging')
|
|
1298
1027
|
.getImmediate();
|
|
1299
|
-
|
|
1300
|
-
getToken:
|
|
1028
|
+
const messagingInternal = {
|
|
1029
|
+
getToken: (options) => getToken$1(messaging, options)
|
|
1301
1030
|
};
|
|
1302
1031
|
return messagingInternal;
|
|
1303
1032
|
};
|
|
@@ -1305,8 +1034,8 @@ function registerMessagingInWindow() {
|
|
|
1305
1034
|
app._registerComponent(new component.Component('messaging', WindowMessagingFactory, "PUBLIC" /* ComponentType.PUBLIC */));
|
|
1306
1035
|
app._registerComponent(new component.Component('messaging-internal', WindowMessagingInternalFactory, "PRIVATE" /* ComponentType.PRIVATE */));
|
|
1307
1036
|
app.registerVersion(name, version);
|
|
1308
|
-
// BUILD_TARGET will be replaced by values like
|
|
1309
|
-
app.registerVersion(name, version, '
|
|
1037
|
+
// BUILD_TARGET will be replaced by values like esm2017, cjs2017, etc during the compilation
|
|
1038
|
+
app.registerVersion(name, version, 'cjs2017');
|
|
1310
1039
|
}
|
|
1311
1040
|
|
|
1312
1041
|
/**
|
|
@@ -1331,39 +1060,27 @@ function registerMessagingInWindow() {
|
|
|
1331
1060
|
*
|
|
1332
1061
|
* @public
|
|
1333
1062
|
*/
|
|
1334
|
-
function isWindowSupported() {
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
return [2 /*return*/, (typeof window !== 'undefined' &&
|
|
1356
|
-
util.isIndexedDBAvailable() &&
|
|
1357
|
-
util.areCookiesEnabled() &&
|
|
1358
|
-
'serviceWorker' in navigator &&
|
|
1359
|
-
'PushManager' in window &&
|
|
1360
|
-
'Notification' in window &&
|
|
1361
|
-
'fetch' in window &&
|
|
1362
|
-
ServiceWorkerRegistration.prototype.hasOwnProperty('showNotification') &&
|
|
1363
|
-
PushSubscription.prototype.hasOwnProperty('getKey'))];
|
|
1364
|
-
}
|
|
1365
|
-
});
|
|
1366
|
-
});
|
|
1063
|
+
async function isWindowSupported() {
|
|
1064
|
+
try {
|
|
1065
|
+
// This throws if open() is unsupported, so adding it to the conditional
|
|
1066
|
+
// statement below can cause an uncaught error.
|
|
1067
|
+
await util.validateIndexedDBOpenable();
|
|
1068
|
+
}
|
|
1069
|
+
catch (e) {
|
|
1070
|
+
return false;
|
|
1071
|
+
}
|
|
1072
|
+
// firebase-js-sdk/issues/2393 reveals that idb#open in Safari iframe and Firefox private browsing
|
|
1073
|
+
// might be prohibited to run. In these contexts, an error would be thrown during the messaging
|
|
1074
|
+
// instantiating phase, informing the developers to import/call isSupported for special handling.
|
|
1075
|
+
return (typeof window !== 'undefined' &&
|
|
1076
|
+
util.isIndexedDBAvailable() &&
|
|
1077
|
+
util.areCookiesEnabled() &&
|
|
1078
|
+
'serviceWorker' in navigator &&
|
|
1079
|
+
'PushManager' in window &&
|
|
1080
|
+
'Notification' in window &&
|
|
1081
|
+
'fetch' in window &&
|
|
1082
|
+
ServiceWorkerRegistration.prototype.hasOwnProperty('showNotification') &&
|
|
1083
|
+
PushSubscription.prototype.hasOwnProperty('getKey'));
|
|
1367
1084
|
}
|
|
1368
1085
|
|
|
1369
1086
|
/**
|
|
@@ -1382,23 +1099,14 @@ function isWindowSupported() {
|
|
|
1382
1099
|
* See the License for the specific language governing permissions and
|
|
1383
1100
|
* limitations under the License.
|
|
1384
1101
|
*/
|
|
1385
|
-
function deleteToken$1(messaging) {
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
if (!!messaging.swRegistration) return [3 /*break*/, 2];
|
|
1394
|
-
return [4 /*yield*/, registerDefaultSw(messaging)];
|
|
1395
|
-
case 1:
|
|
1396
|
-
_a.sent();
|
|
1397
|
-
_a.label = 2;
|
|
1398
|
-
case 2: return [2 /*return*/, deleteTokenInternal(messaging)];
|
|
1399
|
-
}
|
|
1400
|
-
});
|
|
1401
|
-
});
|
|
1102
|
+
async function deleteToken$1(messaging) {
|
|
1103
|
+
if (!navigator) {
|
|
1104
|
+
throw ERROR_FACTORY.create("only-available-in-window" /* ErrorCode.AVAILABLE_IN_WINDOW */);
|
|
1105
|
+
}
|
|
1106
|
+
if (!messaging.swRegistration) {
|
|
1107
|
+
await registerDefaultSw(messaging);
|
|
1108
|
+
}
|
|
1109
|
+
return deleteTokenInternal(messaging);
|
|
1402
1110
|
}
|
|
1403
1111
|
|
|
1404
1112
|
/**
|
|
@@ -1422,7 +1130,7 @@ function onMessage$1(messaging, nextOrObserver) {
|
|
|
1422
1130
|
throw ERROR_FACTORY.create("only-available-in-window" /* ErrorCode.AVAILABLE_IN_WINDOW */);
|
|
1423
1131
|
}
|
|
1424
1132
|
messaging.onMessageHandler = nextOrObserver;
|
|
1425
|
-
return
|
|
1133
|
+
return () => {
|
|
1426
1134
|
messaging.onMessageHandler = null;
|
|
1427
1135
|
};
|
|
1428
1136
|
}
|
|
@@ -1450,18 +1158,17 @@ function onMessage$1(messaging, nextOrObserver) {
|
|
|
1450
1158
|
*
|
|
1451
1159
|
* @public
|
|
1452
1160
|
*/
|
|
1453
|
-
function getMessagingInWindow(app$1) {
|
|
1454
|
-
if (app$1 === void 0) { app$1 = app.getApp(); }
|
|
1161
|
+
function getMessagingInWindow(app$1 = app.getApp()) {
|
|
1455
1162
|
// Conscious decision to make this async check non-blocking during the messaging instance
|
|
1456
1163
|
// initialization phase for performance consideration. An error would be thrown latter for
|
|
1457
1164
|
// developer's information. Developers can then choose to import and call `isSupported` for
|
|
1458
1165
|
// special handling.
|
|
1459
|
-
isWindowSupported().then(
|
|
1166
|
+
isWindowSupported().then(isSupported => {
|
|
1460
1167
|
// If `isWindowSupported()` resolved, but returned false.
|
|
1461
1168
|
if (!isSupported) {
|
|
1462
1169
|
throw ERROR_FACTORY.create("unsupported-browser" /* ErrorCode.UNSUPPORTED_BROWSER */);
|
|
1463
1170
|
}
|
|
1464
|
-
},
|
|
1171
|
+
}, _ => {
|
|
1465
1172
|
// If `isWindowSupported()` rejected.
|
|
1466
1173
|
throw ERROR_FACTORY.create("indexed-db-unsupported" /* ErrorCode.INDEXED_DB_UNSUPPORTED */);
|
|
1467
1174
|
});
|
|
@@ -1482,13 +1189,9 @@ function getMessagingInWindow(app$1) {
|
|
|
1482
1189
|
*
|
|
1483
1190
|
* @public
|
|
1484
1191
|
*/
|
|
1485
|
-
function getToken(messaging, options) {
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
messaging = util.getModularInstance(messaging);
|
|
1489
|
-
return [2 /*return*/, getToken$1(messaging, options)];
|
|
1490
|
-
});
|
|
1491
|
-
});
|
|
1192
|
+
async function getToken(messaging, options) {
|
|
1193
|
+
messaging = util.getModularInstance(messaging);
|
|
1194
|
+
return getToken$1(messaging, options);
|
|
1492
1195
|
}
|
|
1493
1196
|
/**
|
|
1494
1197
|
* Deletes the registration token associated with this {@link Messaging} instance and unsubscribes
|