Created
June 2, 2018 04:28
-
-
Save SoarLin/46fd3dfff6c0458f073c4aa03620740b to your computer and use it in GitHub Desktop.
Firebase Messaging Service Worker JS
This file contains hidden or 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
// [START initialize_firebase_in_sw] | |
// Import and configure the Firebase SDK | |
// These scripts are made available when the app is served or deployed on Firebase Hosting | |
// If you do not serve/host your project using Firebase Hosting see https://firebase.google.com/docs/web/setup | |
importScripts('https://www.gstatic.com/firebasejs/5.0.0/firebase-app.js'); | |
importScripts('https://www.gstatic.com/firebasejs/5.0.0/firebase-messaging.js'); | |
firebase.initializeApp({ | |
messagingSenderId: '<YOUR_SENDER_ID>' | |
}); | |
const messaging = firebase.messaging(); | |
// [END initialize_firebase_in_sw] | |
// If you would like to customize notifications that are received in the | |
// background (Web app is closed or not in browser focus) then you should | |
// implement this optional method. | |
// [START background_handler] | |
messaging.setBackgroundMessageHandler(function(payload) { | |
console.log('[firebase-messaging-sw.js] Received background message ', payload); | |
// Customize notification here | |
var notification = payload.notification | |
var notificationTitle = notification.title; | |
var notificationOptions = { | |
body: notification.body, | |
icon: '/static/images/logo.png' | |
}; | |
return self.registration.showNotification(notificationTitle, | |
notificationOptions); | |
}); | |
// [END background_handler] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank You, yes it is helpful