Created
November 22, 2019 09:01
-
-
Save mrron313/53fc0d4c2183e0c183e96dccf4f008df to your computer and use it in GitHub Desktop.
firebase-messaging-sw.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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