if('serviceWorker'in navigator){window.addEventListener('load',function(){navigator.serviceWorker.register('/firebase-messaging-sw.js');});} var messaging;if(firebase.messaging.isSupported()){messaging=firebase.messaging();messaging.usePublicVapidKey("BK3l9lc_i02_wRJcnB2n4cYeZ8q45lHRDP8c0_5V98CWQrvBoNWcIAQHOugBkdfPFxymKAKvH3WGKvVJDP4lqEk");setTimeout("requestPermission()",60000);messaging.onMessage(function(payload){if(Notification.permission==='granted'){var data=payload.data;var requireInteraction=false;if(data.requireInteraction=='true'){var requireInteraction=true;} var notificationTitle=data.title;var notificationOptions={body:data.body,icon:data.icon,requireInteraction:requireInteraction,event_action:data.event_action};var open_status=false;var notification=new Notification(notificationTitle,notificationOptions);notification.onclick=function(e){e.preventDefault();window.open(data.click_action,'_blank');fetch(data.ga_click,{method:'get'});if(data.event_action){fetch(data.event_action+'&action=open',{method:'get'});} open_status=true;notification.close();} notification.onclose=function(e){if(open_status==false){e.preventDefault();fetch(data.ga_close,{method:'get'});if(data.event_action){fetch(data.event_action+'&action=close',{method:'get'});}}} fetch(data.ga_received,{method:'get'});if(data.event_action){fetch(data.event_action+'&action=received',{method:'get'});}}});} function requestPermission(){Notification.requestPermission().then(function(permission){if(permission==='granted'){FCM();tokenRefresh();}}).catch(function(err){});} function FCM(){messaging.getToken().then(function(currentToken){if(currentToken){sendTokenToServer(currentToken);}else{setTokenSentToServer(false);}}).catch(function(err){setTokenSentToServer(false);});} function tokenRefresh(){messaging.onTokenRefresh(function(){messaging.getToken().then(function(refreshedToken){setTokenSentToServer(false);sendTokenToServer(refreshedToken);}).catch(function(err){});});} function sendTokenToServer(currentToken){if(!isTokenSentToServer(currentToken)){var ajax_url="ajax/firebase/fcm_fetchToken.php";$.ajax({url:ajax_url,data:{notify_token:currentToken,device_id:navigator.userAgent.toLowerCase()},type:'POST',cache:false,dataType:'json'});setTokenSentToServer(true);}} function isTokenSentToServer(currentToken){var d=new Date();d.setTime(d.getTime()+(7*24*60*60*1000));document.cookie="regId="+currentToken+";expires="+d.toUTCString()+";samesite=Strict;";if(window.localStorage.getItem('sent_status')==='7'){return true;} return false;} function setTokenSentToServer(sent){if(sent){window.localStorage.setItem('sent_status','7');}else{window.localStorage.removeItem('sent_status');var expires=new Date();expires.setTime(expires.getTime()-1);document.cookie="regId=;expires="+expires.toUTCString()+";samesite=Strict;";}}