1 2import { initializeApp } from 'https://www.gstatic.com/firebasejs/10.13.1/firebase-app.js' 3import { deleteToken, getMessaging, getToken, onMessage } from 'https://www.gstatic.com/firebasejs/10.13.1/firebase-messaging.js'; 4import { firebaseConfig, vapidKey } from '/static/js/firebase-config.js'; 5 6initializeApp(firebaseConfig); 7 8const messaging = getMessaging(); 9 10 11if ('serviceWorker' in navigator) { 12 navigator.serviceWorker.getRegistrations().then(function(registrations) { 13 for (let registration of registrations) { 14 console.log("FCM REG:" + registration); 15 console.log("FCM ACTIVE SCRIPTURL:" + registration.active.scriptURL); 16 if (registration.active.scriptURL == 'https://udn.com/umedia/sw.js') { 17 console.log("remove old sw "+registration.active.scriptURL); 18 registration.unregister(); 19 } 20 if (registration.active.scriptURL.includes("https://udn.com/static/js/firebase-messaging-sw.js")) { 21 console.log("remove old sw "+registration.active.scriptURL); 22 registration.unregister(); 23 } 24 if (registration.active.scriptURL == 'https://lab7-news.udn.com/static/js/firebase-messaging-sw.js') { 25 console.log("remove old sw "+registration.active.scriptURL); 26 registration.unregister(); 27 } 28 } 29 }); 30 } 31 32 33 34 35Notification.requestPermission().then((permission) => { 36 if (permission === 'granted') { 37 console.log('[firebase] Notification permission granted.'); 38 // TODO(developer): Retrieve a registration token for use with FCM. 39 // In many cases once an app has been granted notification permission, 40 // it should update its UI reflecting this. 41 //console.log('[firebase] vapidKey: '+ vapidKey); 42 getToken(messaging, { vapidKey }).then((currentToken) => { 43 if (currentToken) { 44 console.log('[firebase] currentToken: '+currentToken); 45 // Send Instance ID token to app server. 46 sendTokenToServer(currentToken); 47 showToken(currentToken); 48 } else { 49 // Show permission request. 50 console.log('[firebase] No registration token available. Request permission to generate one.'); 51 // Show permission UI. 52 //updateUIForPushPermissionRequired(); 53 //setTokenSentToServer(false); 54 } 55 }).catch((err) => { 56 console.log('[firebase] An error occurred while retrieving token. ', err); 57 showToken('Error retrieving registration token.'); 58 //setTokenSentToServer(false); 59 }); 60 } else { 61 console.log('[firebase] Unable to get permission to notify.'); 62 } 63}); 64 65 66// Handle incoming messages. Called when: 67// - a message is received while the app has focus 68// - the user clicks on an app notification created by a service worker 69// `messaging.setBackgroundMessageHandler` handler. 70onMessage(messaging, (payload) => { 71 console.log('[firebase] incoming Message received. ', payload); 72 // Update the UI to include the received message. 73 //appendMessage(payload); 74}); 75 76function showToken(currentToken) { 77 console.log('[firebase] current used token:', currentToken); 78 saveToken(currentToken); 79} 80 81 82// Send the Instance ID token your application server, so that it can: 83// - send messages back to this app 84// - subscribe/unsubscribe the token from topics 85function sendTokenToServer(currentToken) { 86 if (!isTokenSentToServer(currentToken)) { 87 88 var data = { 89 token: currentToken, 90 account: user_id 91 }; 92 console.log(data); 93 // TODO(developer): Send the current token to your server. 94 //console.log( isTokenNeedReSend() ); 95 if (isTokenNeedReSend()) { 96 data.update = true; 97 //console.log( 'FCM isTokenNeedReSend' ); 98 /*if (data.account.length == 0) { 99 //���å©ï¿½ï¿½ï¿½ 100 return; 101 }*/ 102 } 103 console.log('[firebase] Sending token to server...'); 104 fetch('https://fcm.udn.com/fcm/fetch/' + firebaseConfig['projectId'], { 105 method: "POST", 106 cache: 'no-cache', 107 mode: 'cors', 108 body: JSON.stringify(data) 109 }) 110 //.then(data => console.log(data)) 111 .catch(error => console.error(error)); 112 //console.log( "FCM DATA ACCOUNT:"+ data.account.length,data.account ); 113 if (data.account === undefined || data.account.length == 0) { 114 setTokenSentToServer('2'); 115 } else { 116 setTokenSentToServer('1'); 117 } 118 } else { 119 console.log('[firebase] Token already sent to server so won\'t send it again unless it changes'); 120 } 121} 122 123function isTokenNeedReSend() { 124 return window.localStorage.getItem('sentToServer') === '2'; 125} 126 127function isTokenSentToServer(token) { 128 return false;//wait for token refetch 129 // return window.localStorage.getItem('sentToServer') === '1' && window.localStorage.getItem('token_'+firebaseConfig['projectId']) === token; 130} 131//0 not sent 1 sent 2 need account 132function setTokenSentToServer(sent) { 133 window.localStorage.setItem('sentToServer', sent); 134} 135 136function saveToken(token) { 137 window.localStorage.setItem('token_'+firebaseConfig['projectId'], token); 138} 139
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.