if ('serviceWorker' in navigator) { window.addEventListener('load', function() { navigator.serviceWorker.register('/firebase-messaging-sw.js'); }); } var messaging; if (firebase.messaging.isSupported()) { messaging = firebase.messaging(); //在頁面停留 1分鐘 才執行 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) { console.log('FCM_click'); 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) { console.log('FCM_close'); fetch(data.event_action + '&action=close', {method: 'get'}); } } } fetch(data.ga_received, { method: 'get' }); if (data.event_action) { console.log('FCM_received'); 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) { //Insert cookie 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'); //delete cookie var expires = new Date(); expires.setTime(expires.getTime() - 1); document.cookie = "regId=;expires=" + expires.toUTCString() + ";samesite=Strict;"; } }