const firebaseConfig = {
  apiKey: "AIzaSyC1j8YxuvgQURoWOGwgh9iurY37gXBP328",
  authDomain: "push-notification-app-b8783.firebaseapp.com",
  projectId: "push-notification-app-b8783",
  storageBucket: "push-notification-app-b8783.appspot.com",
  messagingSenderId: "643408741244",
  appId: "1:643408741244:web:445239aa871fdc54425af9",
  measurementId: "G-PWETQ3J9CC"
};
      
  firebase.initializeApp(firebaseConfig);
  
  const messaging = firebase.messaging();
  messaging.onMessage((payload) => 
    {
        var obj = jQuery.parseJSON(payload.data.message);
        var notification = new Notification(obj.title,{
          icon:obj.icon,
          body:obj.body
        });
    });
  
  if(Notification.permission=="default") {
    messaging.getToken({vapidKey:'BCSWM0magnI_zBUFAUEd8nCMD93oJ3KsRhzUUZBfXpfMy1I8xqT8sacMVIFrh2cDyQZ58suK6gVaezN0WpMg_B4'}).then((token) => {
      if (token) 
      {
        let tokenData = new FormData();
        tokenData.append('token', token);
        fetch('index.php?route=marketing/push/store', {
          method: 'POST',
          body: tokenData,
        }).then((response) => {
          return response.json();
        });
      } 
    })
  }