1// Your web app's Firebase configuration 2//window.localStorage.setItem('sentToServer', '0'); 3const firebaseConfig = { 4 apiKey: "AIzaSyDMqHBujTXLut_qZ-YOY4qZU4vf5q04X68", 5 authDomain: "novosti-web-push-notifikacije.firebaseapp.com", 6 databaseURL: "https://novosti-web-push-notifikacije.firebaseio.com", 7 projectId: "novosti-web-push-notifikacije", 8 storageBucket: "novosti-web-push-notifikacije.appspot.com", 9 messagingSenderId: "620558234693", 10 appId: "1:620558234693:web:a8f2b5ce38ce0d7baadf2c" 11}; 12 13// Initialize Firebase 14firebase.initializeApp(firebaseConfig); 15 16 17// [START get_messaging_object] 18// Retrieve Firebase Messaging object. 19const messaging = firebase.messaging(); 20// [END get_messaging_object] 21// [START set_public_vapid_key] 22// Add the public key generated from the console here. 23messaging.usePublicVapidKey('BOG1RnDqfQx00FlgfE5ps2xO_y0e-u2mptVr0RIBKkqXT9rI0zRCZTCmdDP9SsjR3KrLmziGHMQV79kwGT2TxK0'); 24// [END set_public_vapid_key] 25 26 27//if ('serviceWorker' in navigator) { 28// navigator.serviceWorker.register('/firebase-messaging-sw.js') 29// .then(function (registration) { 30// //messaging.useServiceWorker(registration); 31// //registration.update(); 32// // registration worked 33// console.log('Registration succeeded.'); 34// }).catch(function (error) { 35// // registration failed 36// console.log('Registration failed with ' + error); 37// }); 38//}; 39 40// [START refresh_token] 41// Callback fired if Instance ID token is updated. 42messaging.onTokenRefresh(() => { 43 messaging.getToken().then((refreshedToken) => { 44 console.log('Token refreshed.'); 45 // Indicate that the new Instance ID token has not yet been sent to the 46 // app server. 47 setTokenSentToServer(false); 48 // Send Instance ID token to app server. 49 sendTokenToServer(refreshedToken); 50 // [START_EXCLUDE] 51 // Display new Instance ID token and clear UI of all previous messages. 52 resetUI(); 53 }).catch((err) => { 54 console.log('Unable to retrieve refreshed token ', err); 55 }); 56}); 57// [END refresh_token] 58 59// [START receive_message] 60// Handle incoming messages. Called when: 61// - a message is received while the app has focus 62// - the user clicks on an app notification created by a service worker 63// `messaging.setBackgroundMessageHandler` handler. 64messaging.onMessage((payload) => { 65 console.log('Message received. ', payload); 66}); 67// [END receive_message] 68 69function resetUI() { 70 // [START get_token] 71 // Get Instance ID token. Initially this makes a network call, once retrieved 72 // subsequent calls to getToken will return from cache. 73 messaging.getToken().then((currentToken) => { 74 if (currentToken) { 75 // ok 76 sendTokenToServer(currentToken); 77 } else { 78 // Show permission request. 79 console.log('No Instance ID token available. Request permission to generate one.'); 80 // Show permission UI. 81 setTokenSentToServer(false); 82 } 83 }).catch((err) => { 84 //console.log('An error occurred while retrieving token. ', err); 85 setTokenSentToServer(false); 86 }); 87 // [END get_token] 88} 89 90 91// ok 92// Send the Instance ID token your application server, so that it can: 93// - send messages back to this app 94// - subscribe/unsubscribe the token from topics 95function sendTokenToServer(currentToken) { 96 // if (!isTokenSentToServer()) { 97 console.log('Sending token to server...'); 98 // TODO(developer): Send the current token to your server. 99 // ok 100 setTokenSentToServer(true); 101 102 var oldToken = 0; 103 if (window.localStorage.getItem("activeToken") !== currentToken) { 104 oldToken = window.localStorage.getItem('activeToken'); 105 } 106 107 $.ajax({ 108 type: 'GET', 109 url: '/ajax/webpushtoken', 110 data: { 111 token : currentToken, 112 old: oldToken 113 } 114 }); 115 116 window.localStorage.setItem('activeToken', currentToken); 117 118 // } else { 119 // console.log('Token already sent to server so won\'t send it again ' + 'unless it changes'); 120 // } 121 122} 123// OK 124function isTokenSentToServer() { 125 return window.localStorage.getItem('sentToServer') === '1'; 126} 127// OK 128function setTokenSentToServer(sent) { 129 window.localStorage.setItem('sentToServer', sent ? '1' : '0'); 130} 131 132function requestPermission() { 133 console.log('Requesting permission...'); 134 // [START request_permission] 135 Notification.requestPermission().then((permission) => { 136 if (permission === 'granted') { 137 console.log('Notification permission granted.'); 138 // TODO(developer): Retrieve an Instance ID token for use with FCM. 139 // [START_EXCLUDE] 140 // In many cases once an app has been granted notification permission, 141 // it should update its UI reflecting this. 142 resetUI(); 143 // [END_EXCLUDE] 144 } else { 145 console.log('Unable to get permission to notify.'); 146 } 147 }); 148 // [END request_permission] 149} 150 151resetUI(); 152 153var updateInterval = 24 * 60 * 60 * 1000; 154var currentTimestamp = new Date().getTime(); 155var lastUpdatedTimestamp = localStorage.getItem('firebase_timestamp') || 0; 156 157if ((currentTimestamp - lastUpdatedTimestamp) > updateInterval) { 158 // send token every day 159 setTokenSentToServer(false); 160 resetUI(); 161 if ('serviceWorker' in navigator) { 162 navigator.serviceWorker.register('/firebase-messaging-sw.js') 163 .then(function() { 164 localStorage.setItem('firebase_timestamp', currentTimestamp); 165 console.log('Service worker registered!'); 166 }, 167 function() { 168 console.log('err - Service Worker Registration Failed !') 169 }); 170 171 navigator.serviceWorker.getRegistrations().then(function (registrations) { 172 for (let registration of registrations) { 173 if (registration.active.scriptURL != 'https://www.novosti.rs
173/firebase-messaging-sw.js') { 174 registration.unregister(); 175 } 176 177 if (registration.active.scriptURL == 'https://www.novosti.rs/firebase-messaging-sw.js') { 178 registration.update(); 179 } 180 } 181 }); 182 } 183} 184 185
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.