1var push_notification = { 2 document_ready: false, 3 event_element: document.createElement("span"), 4 isFirstRequestPermission: true, 5 token: '', 6 supported: false, 7 onTokenReady: function () {}, 8 onUnsupported: function () {}, 9 onNoPermission: function (error) {}, 10 onPermissionDenied: function () {}, 11 get_query_params: function( queryString ) { 12 var params = {}, queries, temp, i, l; 13 // Split into key/value pairs 14 queries = queryString.split("&"); 15 // Convert the array of strings into an object 16 for ( i = 0, l = queries.length; i < l; i++ ) { 17 temp = queries[i].split('='); 18 params[temp[0]] = temp[1]; 19 } 20 return params; 21 }, 22 trackEvent: function (params) { 23 if (typeof ga == 'function' && typeof ga.getAll == 'function') { 24 var eventCategory = params.evtCategory; 25 var eventAction = params.evtAction; 26 var eventLabel = params.evtLabel; 27 28 var accounts = ga.getAll(); 29 for (var i = 0; i < accounts.length; i++) { 30 ga(accounts[i].get("name") + '.send', 'event', eventCategory, eventAction, eventLabel, { 31 nonInteraction: true 32 }); 33 } 34 } else { 35 setTimeout(function () { 36 push_notification.trackEvent(params); 37 }, 1000); 38 } 39 }, 40 /* 41 sent_token_to_service_worker: function () { 42 navigator.serviceWorker.controller.postMessage({token: this.token}); 43 }, 44 */ 45 send_token_to_server: function (token) { 46 47 var options = Intl.DateTimeFormat().resolvedOptions(); 48 //document.getElementById('token').innerHTML = token; 49 50 //this.sent_token_to_service_worker(); 51 // navigator.serviceWorker.controller.onstatechange = function (e) { 52 // console.log('controller status', e); 53 // }; 54 55 56 console.log('push notification create_token'); 57 push_notification_api.create_token(token, options.locale, options.timeZone, function (json) { 58 console.log('push notification add_topics'); 59 push_notification_api.add_topics(push_notification.token, ['default']); 60 }); 61 62 }, 63 get_token: function (request_permission) { 64 // Get Instance ID token. Initially this makes a network call, once retrieved 65 // subsequent calls to getToken will return from cache. 66 firebase.messaging().getToken() 67 .then(function(currentToken) { 68 if (push_notification.token === '' && currentToken !== null) { 69 console.log('token', currentToken); 70 push_notification.token = currentToken; 71 72 if (document.body) { 73 document.body.dispatchEvent(new Event('push_notification_token_ready')); 74 } 75 else { 76 document.addEventListener("DOMContentLoaded", function(event) { 77 document.body.dispatchEvent(new Event('push_notification_token_ready')); 78 }); 79 } 80 81 console.log('is_first', push_notification.isFirstRequestPermission); 82 //se è la prima volta che il token viene richiesto e non è vuoto, vuol dire che l'utente aveva già dato il consenso, altrimenti lo ha appena dato 83 if (!push_notification.isFirstRequestPermission) { 84 if (document.body) { 85 document.body.dispatchEvent(new Event('push_notification_granted')); 86 } 87 else { 88 document.addEventListener("DOMContentLoaded", function(event) { 89 document.body.dispatchEvent(new Event('push_notification_granted')); 90 }); 91 } 92 console.log('Notification permission granted.'); 93 push_notification.send_token_to_server(currentToken); 94 } 95 96 } 97 98 if (currentToken) { 99 100 if (window.location.search.length > 0) { 101 var string = window.location.search;
102 string = string.substring( string.indexOf('?') + 1 ); 103 var params = push_notification.get_query_params(string); 104 console.log(params); 105 if (typeof params.notification_id !== 'undefined' && typeof params.hash !== 'undefined') { 106 push_notification_api.track_notification_click(currentToken, params.notification_id, params.hash); 107 } 108 } 109 110 if (typeof push_notification.onTokenReady === 'function') { 111 push_notification.onTokenReady(); 112 } 113 114 //updateUIForPushEnabled(currentToken); 115 } else if (request_permission) { 116 // push_notification.supported = true; 117 // Show permission request. 118 console.log('No Instance ID token available. Request permission to generate one.'); 119 // Show permission UI. 120 //updateUIForPushPermissionRequired(); 121 //setTokenSentToServer(false); 122 123 firebase.messaging().requestPermission() 124 .then(function() { 125 // TODO(developer): Retrieve an Instance ID token for use with FCM. 126 push_notification.isFirstRequestPermission = false; 127 push_notification.get_token(); 128 129 }) 130 .catch(function(err) { 131 console.log('Unable to get permission to notify.', err); 132 if (typeof push_notification.onNoPermission === 'function') { 133 push_notification.onNoPermission(err); 134 } 135 }); 136 137 } else { 138 if (typeof push_notification.onTokenReady === 'function') { 139 push_notification.onTokenReady(); 140 } 141 } 142 }) 143 .catch(function(err) { 144 console.log('An error occurred while retrieving token. ', err); 145 console.log('Error retrieving Instance ID token. ', err); 146 147 if (typeof push_notification.onUnsupported === 'function') { 148 push_notification.onUnsupported(); 149 } 150 151 //setTokenSentToServer(false); 152 }); 153 }, 154 delete_token: function () { 155 firebase.messaging().getToken() 156 .then(function(currentToken) { 157 firebase.messaging().deleteToken(currentToken) 158 .then(function() { 159 console.log('Token deleted.'); 160 }) 161 .catch(function(err) { 162 console.log('Unable to delete token. ', err); 163 }); 164 }) 165 .catch(function(err) { 166 console.log('Error retrieving Instance ID token. ', err); 167 }); 168 } 169 /*, 170 sw_token: function () { 171 navigator.serviceWorker.controller.postMessage({action: 'show_token'}); 172 } 173 */ 174};
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.