-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathservice-worker.js
60 lines (51 loc) · 1.92 KB
/
service-worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// [SNIPPET_REGISTRY disabled]
// [SNIPPETS_SEPARATION enabled]
import { initializeApp } from "firebase/app";
const firebaseApp = initializeApp({
apiKey: '### FIREBASE API KEY ###',
appId: '### FIREBASE APP ID ###',
projectId: '### FIREBASE PROJECT ID ###'
});
// See: https://github.com/microsoft/TypeScript/issues/14877
/** @type {ServiceWorkerGlobalScope} */
let self;
function initInSw() {
// [START messaging_init_in_sw]
const { initializeApp } = require("firebase/app");
const { getMessaging } = require("firebase/messaging/sw");
// Initialize the Firebase app in the service worker by passing in
// your app's Firebase config object.
// https://firebase.google.com/docs/web/setup#config-object
const firebaseApp = initializeApp({
apiKey: 'api-key',
authDomain: 'project-id.firebaseapp.com',
databaseURL: 'https://project-id.firebaseio.com',
projectId: 'project-id',
storageBucket: 'project-id.appspot.com',
messagingSenderId: 'sender-id',
appId: 'app-id',
measurementId: 'G-measurement-id',
});
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = getMessaging(firebaseApp);
// [END messaging_init_in_sw]
}
function onBackgroundMessage() {
// [START messaging_on_background_message]
const { getMessaging } = require("firebase/messaging/sw");
const { onBackgroundMessage } = require("firebase/messaging/sw");
const messaging = getMessaging();
onBackgroundMessage(messaging, (payload) => {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
const notificationTitle = 'Background Message Title';
const notificationOptions = {
body: 'Background Message body.',
icon: '/firebase-logo.png'
};
self.registration.showNotification(notificationTitle,
notificationOptions);
});
// [END messaging_on_background_message]
}