Skip to content

Instantly share code, notes, and snippets.

@Mohammad-Faisal
Last active November 2, 2020 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mohammad-Faisal/1ba8eaf12e69bc259711b3227bb701e6 to your computer and use it in GitHub Desktop.
Save Mohammad-Faisal/1ba8eaf12e69bc259711b3227bb701e6 to your computer and use it in GitHub Desktop.
importScripts('https://www.gstatic.com/firebasejs/7.14.2/firebase-app.js')
importScripts('https://www.gstatic.com/firebasejs/7.14.2/firebase-messaging.js')
const config = {
apiKey: "API_KEY",
authDomain: "PROJECT_ID.firebaseapp.com",
databaseURL: "https://PROJECT_ID.firebaseio.com",
projectId: "PROJECT_ID",
storageBucket: "PROJECT_ID.appspot.com",
messagingSenderId: "SENDER_ID",
appId: "APP_ID",
measurementId: "G-MEASUREMENT_ID"
}
firebase.initializeApp(config)
const messaging = firebase.messaging()
// IF WE WANT TO HANDLE BACKGROUND NOTIFICATION WE HAVE TO ADD THE FOLLOWING BLOCK OF CODE AS WELL
messaging.setBackgroundMessageHandler(function (payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload)
const notificationTitle = payload.data.title
const notificationOptions = {
body: payload.data.body,
icon: '/firebase-logo.png'
}
return self.registration.showNotification(notificationTitle, notificationOptions)
})
self.addEventListener('notificationclick', (event) => {
console.log(event)
return event
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment