0

Trying to configure FCM SDK on Shopify store using below script.

<script src="https://www.gstatic.com/firebasejs/8.7.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.7.0/firebase-messaging.js"></script>
<script>
  // Initialize Firebase
  var firebaseConfig = {
    // my goes my FCM details 
  };
  firebase.initializeApp(firebaseConfig);

  // Retrieve Firebase Messaging object.
  const messaging = firebase.messaging();



  messaging.getToken({ vapidKey: 'MY-TOKEN' }).then((currentToken) => {
    if (currentToken) {
      console.log(currentToken);
      // Send the current token to your server to enable sending notifications to this device
    } else {
      console.log('No registration token available. Request permission to generate one.');
    }
  }).catch((err) => {
    console.log('An error occurred while retrieving token. ', err);
  });
</script>

But i am getting below error 404, which mean resource not found.

An error occurred while retrieving token.  FirebaseError: Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker for scope ('https://bolanclinic.com/firebase-cloud-messaging-push-scope') with script ('https://bolanclinic.com/firebase-messaging-sw.js'): A bad HTTP response code (404) was received when fetching the script. (messaging/failed-service-worker-registration).
    at nt.<anonymous> (window-controller.ts:166:27)
    at tslib.es6.js:102:23
    at Object.throw (tslib.es6.js:83:53)
    at i (tslib.es6.js:74:64)

tried below code after placing file in Assets folder, but not worked

 navigator.serviceWorker.register({{ 'firebase-messaging-sw.js' | asset_url }});
 messaging.useServiceWorker(navigator.serviceWorker.register());

I suspect i need to put file at same location or root Directory but i am unable to do so via Shopify store. please guide where and how to put SW file so it will accessible at 'https://bolanclinic.com/firebase-messaging-sw.js' ?

Muhammad Suleman
  • 2,892
  • 2
  • 25
  • 33

0 Answers0