-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
firebase-messaging-sw.ts
45 lines (37 loc) 路 1.22 KB
/
firebase-messaging-sw.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { initializeApp } from 'firebase/app';
import {
experimentalSetDeliveryMetricsExportedToBigQueryEnabled,
getMessaging,
isSupported,
onBackgroundMessage
} from 'firebase/messaging/sw';
declare var self: ServiceWorkerGlobalScope;
self.addEventListener('install', (event) => {
console.log(self);
console.log(event);
});
const app = initializeApp({
apiKey: 'AIzaSyAgUhHU8wSJgO5MVNy95tMT07NEjzMOfz0',
authDomain: 'react-native-firebase-testing.firebaseapp.com',
databaseURL: 'https://react-native-firebase-testing.firebaseio.com',
projectId: 'react-native-firebase-testing',
storageBucket: 'react-native-firebase-testing.appspot.com',
messagingSenderId: '448618578101',
appId: '1:448618578101:web:ecaffe2bc4511738',
});
isSupported().then((isSupported) => {
if (isSupported) {
const messaging = getMessaging(app);
experimentalSetDeliveryMetricsExportedToBigQueryEnabled(messaging, true);
onBackgroundMessage(messaging, ({ notification: notification }) => {
const { title, body, image } = notification ?? {};
if (!title) {
return;
}
self.registration.showNotification(title, {
body,
icon: image || '/assets/icons/icon-72x72.png',
});
});
}
});