1

As per this video instruction https://www.youtube.com/watch?v=BsCBCudx58g, I have integrated the code it looks below.

<script src="https://www.gstatic.com/firebasejs/3.6.5/firebase.js"></script>
<script>

  // Initialize Firebase
  var config = {
    apiKey: "IIzaSyA3MZtKiK6E6y7q9w26J-YMi9Leeao5Zs0",
    authDomain: "a...99.firebaseapp.com",
    databaseURL: "https://a...99.firebaseio.com",
    storageBucket: "a...99.appspot.com",
    messagingSenderId: "915965251026"
  };
  firebase.initializeApp(config);

    const messaging = firebase.messaging();
    messaging.requestPermission()
    .then(function() {
    alert('Notification permission granted.');

    return messaging.getToken();

    }).then(function(token){
        alert(token);
    })
    .catch(function(err) {
        alert('Unable to get permission to notify.', err);
    });

</script>

manifest.json file

{
  "short_name": "AirHorner",
  "name": "Kinlan's AirHorner of Infamy",

  "start_url": "index.html?launcher=true"
}

Also I have created a dummy "firebase-messaging-sw.js" file


What error I faced
When I execute it, Notification permission granted alert was executed, and suddenly display Unable to get permission to notify message and in the err argument I got the following error

browserErrorMessage : "Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script." code : "messaging/failed-serviceworker-registration" message : "Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script. (messaging/failed-serviceworker-registration)." stack : "FirebaseError: Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script. (messaging/failed-serviceworker-registration).↵ at https://www.gstatic.com/firebasejs/3.6.5/firebase.js:552:225"

In message.getToken() return the following value

_proto__ : Promise [[PromiseStatus]] : "pending" [[PromiseValue]] : undefined

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Merbin Jo
  • 1,189
  • 1
  • 11
  • 19
  • 1
    It lookks like it can't find the service worker script. Do you have a `firebase-messaging-sw.js`? – Frank van Puffelen Jan 11 '17 at 14:21
  • @FrankvanPuffelen Thank you very much, I created but it is not in home directory. So it is not worked. Now my problem is solved. – Merbin Jo Jan 12 '17 at 04:32
  • 1
    Good to hear. It'd be good to post a self-answer explaining that your had to put `firebase-messaging-sw.js` in the root directory. – Frank van Puffelen Jan 12 '17 at 05:26
  • @FrankvanPuffelen Do you know how to send push notification from http://onlinecurl.com/ here I tried but it show "HTTP/1.1 401 The request was missing an Authentification Key (FCM Token)" error – Merbin Jo Jan 12 '17 at 05:41
  • 1
    Get SSL and run in secure transfer protocol – Rijo Oct 05 '17 at 09:47
  • I am using apache, where should I keep firebase-messaging-sw.js. I have tried to store it in my project directory and also in webapps folder but it's not working – Manish Jul 20 '18 at 10:19
  • For Eg: in Apache tomcat do we have to set the js file in root ? or In project directory root ? – Ajay Takur Jul 17 '20 at 15:06

0 Answers0