templates/bo/include/notification-modal.html.twig line 1

Open in your IDE?
  1. <div class="modal fade" id="notificationModal" data-backdrop="static" data-keyboard="false" tabindex="-1" aria-labelledby="notificationModalLabel" aria-hidden="true">
  2.     <div class="modal-dialog">
  3.         <div class="modal-content">
  4.             <div class="modal-body">
  5.                 <h5 class="modal-title" id="notificationModalLabel">
  6.                     <i class="fa flaticon2-notification fa-1x"></i>
  7.                     Veuillez activer les notifications
  8.                 </h5>
  9.             </div>
  10.             <div class="modal-footer">
  11.                 <button type="button" class="btn btn-primary" id="enableNotificationBtn">activer</button>
  12.             </div>
  13.         </div>
  14.     </div>
  15. </div>
  16. <script type="module">
  17.     import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.23.0/firebase-app.js';
  18.     import { getMessaging, getToken, onMessage, deleteToken } from 'https://www.gstatic.com/firebasejs/9.23.0/firebase-messaging.js';
  19.     const firebaseConfig = {
  20.         apiKey: "AIzaSyCRHeWKNN7QGvSK_aaOPUQZlWKdBfjoQwY",
  21.         authDomain: "zoe-business-plan.firebaseapp.com",
  22.         projectId: "zoe-business-plan",
  23.         storageBucket: "zoe-business-plan.appspot.com",
  24.         messagingSenderId: "581857449358",
  25.         appId: "1:581857449358:web:1a7faa7f7427f587b540df",
  26.         measurementId: "G-C227R0F3H2"
  27.     };
  28.     // Initialize Firebase
  29.     const app = initializeApp(firebaseConfig);
  30.     const vapidKey = "BPuVr_gdlBS5JDnCLO4pVzQimBee32x2hfFbH77nuCSGrpceyR5-8Ei-VTLNXZp9FNAg4G7bx167itbVp3cuIXI"
  31.     let messaging = getMessaging(app);
  32.     function deleteFirebaseToken() {
  33.         deleteToken(messaging, { vapidKey: vapidKey }).then(() => {
  34.             console.log('Token deleted.');
  35.         }).catch((err) => {
  36.             console.log('Unable to delete token. ', err);
  37.         });
  38.     }
  39.     onMessage(messaging, (payload) => {
  40.         console.log('Message received. ', payload);
  41.         const notificationTitle = payload.notification.title;
  42.         const notificationOptions = { body: payload.notification.body };
  43.         const notification = new Notification(notificationTitle,  notificationOptions);
  44.         console.log(notification)
  45.         if (location.pathname.includes("/bo/affichage-business-plan") || location.pathname.includes("/bo/models-business-plan") || location.pathname.includes('/mes-business-plan')){
  46.             location.reload()
  47.         }
  48.     });
  49.     if (window.Notification && Notification.permission === 'granted') {
  50.         console.log('Les notifications sont activées.');
  51.     } else if (window.Notification && Notification.permission !== 'denied') {
  52.         $('#notificationModal').modal('show')
  53.     } else {
  54.         console.log('Les notifications sont désactivées ou l\'accès aux notifications est refusé.');
  55.     }
  56.     async function getOrRegisterServiceWorker(){
  57.         if ('serviceWorker' in navigator) {
  58.             const serviceWorker = await window.navigator.serviceWorker
  59.                 .getRegistration('/firebase-zoe-business-notification-scope');
  60.             if (serviceWorker) return serviceWorker;
  61.             return window.navigator.serviceWorker.register('/firebase-messaging-sw.js', {
  62.                 scope: '/firebase-zoe-business-notification-scope',
  63.             });
  64.         }
  65.         throw new Error('The browser doesn`t support service worker.');
  66.     }
  67.     function registerFirebaseToken(token){
  68.         const firebaseToken = localStorage.getItem("firebaseToken")
  69.         const firebaseTokenIsUsed = localStorage.getItem("firebaseTokenIsUsed")
  70.         if(firebaseTokenIsUsed !== undefined && firebaseTokenIsUsed && firebaseToken !== undefined && firebaseToken === token){
  71.             console.log("Zoe Business Notifications")
  72.         }else{
  73.             localStorage.setItem("firebaseToken", token)
  74.             localStorage.setItem("firebaseTokenIsUsed", false)
  75.         }
  76.     }
  77.     function enableNotification(){
  78.         $('#notificationModal').modal('hide')
  79.         Notification.requestPermission().then(function(permission) {
  80.             if (permission === 'granted') {
  81.                 console.log('Les notifications sont maintenant activées.');
  82.                 localStorage.setItem("isGranted", true)
  83.                 getOrRegisterServiceWorker().then(serviceWorkerRegistration => {
  84.                     getToken(messaging, { vapidKey: vapidKey, serviceWorkerRegistration }).then((currentToken) => {
  85.                         if (currentToken) {
  86.                             registerFirebaseToken(currentToken)
  87.                         } else {
  88.                             console.log('No registration token available. Request permission to generate one.');
  89.                         }
  90.                     }).catch((err) => {
  91.                         location.reload()
  92.                         console.log('An error occurred while retrieving token. ', err);
  93.                     });
  94.                 })
  95.             }
  96.         });
  97.     }
  98.     document.addEventListener('DOMContentLoaded', function() {
  99.         document.querySelector('#enableNotificationBtn').addEventListener('click', enableNotification);
  100.     });
  101.     if (localStorage.getItem('isGranted')){
  102.         getOrRegisterServiceWorker().then(serviceWorkerRegistration => {
  103.             getToken(messaging, { vapidKey: vapidKey, serviceWorkerRegistration }).then((currentToken) => {
  104.                 if (currentToken) {
  105.                     registerFirebaseToken(currentToken)
  106.                 } else {
  107.                     console.log('No registration token available. Request permission to generate one.');
  108.                 }
  109.             }).catch((err) => {
  110.                 console.log('An error occurred while retrieving token. ', err);
  111.             });
  112.         })
  113.     }
  114. </script>