Skip to content

Instantly share code, notes, and snippets.

@mrron313
Created November 22, 2019 09:01
Show Gist options
  • Save mrron313/53fc0d4c2183e0c183e96dccf4f008df to your computer and use it in GitHub Desktop.
Save mrron313/53fc0d4c2183e0c183e96dccf4f008df to your computer and use it in GitHub Desktop.
firebase-messaging-sw.js
/*
Give the service worker access to Firebase Messaging.
Note that you can only use Firebase Messaging here, other Firebase libraries are not available in the service worker.
*/
importScripts('https://www.gstatic.com/firebasejs/6.3.4/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/6.3.4/firebase-messaging.js');
/*
Initialize the Firebase app in the service worker by passing in the messagingSenderId.
* New configuration for app@pulseservice.com
*/
firebase.initializeApp({
apiKey: "<api-key>",
authDomain: "<xxxxx>.firebaseapp.com",
databaseURL: "https://<xxxxx>.firebaseio.com",
projectId: "<xxxxx>",
storageBucket: "<xxxxx>.appspot.com",
messagingSenderId: "<xxxxx>",
appId: "<xxxxx>"
});
/*
Retrieve an instance of Firebase Messaging so that it can handle background messages.
*/
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function(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'
};
return self.registration.showNotification(notificationTitle,
notificationOptions);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment