title: What is the use of firebase-messaging-sw.js in firebase web notifications?
body: <p>I have configured Javascript API for firebase push notifications on my website. Everything is working fine but I'm seeing many 'Background messages'. I've just put the <strong>firebase-messaging-sw.js</strong> file in my website's directory.</p>
  
  <p>I don't know the use of it. I was just trying to know what it does. If you see following code, you'll see I haven't provided the <code>messagingSenderId</code> still this file gets executed.</p>
  
  <p>So what I want to know is:</p>
  
  <p>How it works without ID?</p>
  
  <p>What is the use of it? Is it a mandatory file to receive push notifications and handle them in <code>onMessage</code> handler?</p>
  
  <p>Where should I actually put this file (my current directory is: <strong>MY-SITE-DOMAIN/firebase-messaging-sw.js</strong> but the firebase notifications are configured in different directory)?</p>
  
  <p>Here's the code:</p>
  
  <pre><code>importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-app.js');
  importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-messaging.js');
  
  firebase.initializeApp({
    'messagingSenderId': 'YOUR-SENDER-ID'
  });
  
  const messaging = firebase.messaging();
  messaging.setBackgroundMessageHandler(function(payload) {
   console.log('[firebase-messaging-sw.js] Received background message ', payload);
   const notificationTitle = 'Background Message from html';
    const notificationOptions = {
     body: 'Background Message body.',
     icon: '/firebase-logo.png'
   };
  
    return self.registration.showNotification(notificationTitle,
       notificationOptions);
  });
  </code></pre>
  
  <p>Original file is <a href="https://github.com/firebase/quickstart-js/blob/master/messaging/firebase-messaging-sw.js" rel="noreferrer">here</a></p>