Skip to content

Commit f063360

Browse files
authored
Add messaging sw snippet (#199)
1 parent e48a146 commit f063360

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

messaging-next/service-worker.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@ function initInSw() {
4040

4141
function onBackgroundMessage() {
4242
// [START messaging_on_background_message]
43-
// TODO(snippet): This snippet is not yet translated to the @exp SDK
43+
const { getMessaging } = require("firebase/messaging");
44+
const { onBackgroundMessage } = require("firebase/messaging/sw");
45+
46+
const messaging = getMessaging();
47+
onBackgroundMessage(messaging, (payload) => {
48+
console.log('[firebase-messaging-sw.js] Received background message ', payload);
49+
// Customize notification here
50+
const notificationTitle = 'Background Message Title';
51+
const notificationOptions = {
52+
body: 'Background Message body.',
53+
icon: '/firebase-logo.png'
54+
};
55+
56+
self.registration.showNotification(notificationTitle,
57+
notificationOptions);
58+
});
4459
// [END messaging_on_background_message]
4560
}

snippets/messaging-next/service-worker/messaging_on_background_message.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,20 @@
44
// To make edits to the snippets in this file, please edit the source
55

66
// [START messaging_on_background_message_modular]
7-
// TODO(snippet): This snippet is not yet translated to the @exp SDK
7+
import { getMessaging } from "firebase/messaging";
8+
import { onBackgroundMessage } from "firebase/messaging/sw";
9+
10+
const messaging = getMessaging();
11+
onBackgroundMessage(messaging, (payload) => {
12+
console.log('[firebase-messaging-sw.js] Received background message ', payload);
13+
// Customize notification here
14+
const notificationTitle = 'Background Message Title';
15+
const notificationOptions = {
16+
body: 'Background Message body.',
17+
icon: '/firebase-logo.png'
18+
};
19+
20+
self.registration.showNotification(notificationTitle,
21+
notificationOptions);
22+
});
823
// [END messaging_on_background_message_modular]

0 commit comments

Comments
 (0)