const firebaseConfig = { apiKey: "AIzaSyDFLdzq0UbIoEUjUfpci0YsLqZiHHqH6DU", authDomain: "pnj-push-notification.firebaseapp.com", projectId: "pnj-push-notification", storageBucket: "pnj-push-notification.appspot.com", messagingSenderId: "233267048687", appId: "1:233267048687:web:ee6813c56d46393709eb1a", measurementId: "G-K1CDGBJEK0", databaseURL: "https://pnj-push-notification-default-rtdb.asia-southeast1.firebasedatabase.app/", }; // Initialize Firebase firebase.initializeApp(firebaseConfig); firebase.analytics(); let messaging = firebase.messaging(); messaging.usePublicVapidKey('BB-zTHjoj_1dHiqDfES8wRrrm8wlKTRBJY6tRNgkJJcsQZEpaJT4uGwdBlxyqQsfsnRUSuWx3Ri84YGJODOIIPo'); messaging.onMessage(function(payload) { const notificationTitle = payload.notification.title; const notificationOptions = { body: payload.notification.body, image: payload.notification.image, icon: "https://cdn.pnj.io/images/logo/pnj.com.vn.png", vibrate: [200, 100, 200, 100, 200, 100, 200], actions: [{action: "open_url", title: "Xem ngay"}], data: { url:payload.notification.click_action }, //the url which we gonna use later }; // console.log(notificationTitle,notificationOptions) if (!("Notification" in window)) { console.log("This browser does not support system notifications."); } else if (Notification.permission === "granted") { // If it's okay let's create a notification var notification = new Notification(notificationTitle,notificationOptions); notification.onclick = function(event) { event.preventDefault(); switch(event.action){ case 'open_url': window.open(event.notification.data.url , '_blank'); break; case 'any_other_action': window.open("https://pnj.com.vn" , '_blank'); break; } notification.close(); } } }); const subscribeUser = () => { console.log('Requesting notifications permission...'); messaging.requestPermission().then(() => { // Notification permission granted. saveMessagingDeviceToken(); }).catch(function (error) { console.error('Unable to get permission to notify.', error); }); }; const saveMessagingDeviceToken = () => { messaging.getToken().then((currentToken) => { if (currentToken) { saveUserToken(currentToken); // sub } else { // Need to request permissions to show notifications. subscribeUser(); } messaging.onTokenRefresh(() => { messaging.getToken().then((currentToken) => { saveUserToken(currentToken); }); }); }).catch(function (error) { console.error('Unable to get messaging token.', error); }); }; const saveUserToken = (token) => { // Saving the Device Token to the database. //call ajax // call CDP web_event.queue.push(['track', 'notification', 'subscribed', { extra:{ firebase_token: token, identify_event: 'subscribed' } }]); var bodyFormData = new FormData(); bodyFormData.append('token', token); axios({ method: "post", url: location.protocol + "//" + location.host+"/?dispatch=firebase.register", data: bodyFormData, headers: { "Content-Type": "multipart/form-data" }, }) .then(function (response) { //handle success console.log(response); }) .catch(function (response) { //handle error console.log(response); }); }; //register service worker navigator.serviceWorker.register('firebase-messaging-sw.js').then(function(registration) { console.log('Service Worker Registered!', registration); }).catch(function(err) { console.error('Service Worker registration failed', err); }); subscribeUser() if (Notification.permission === 'default') { // subscribeUser() }