1firebase.initializeApp({ 2 messagingSenderId: '1081587556728' 3}); 4 5var notificationIsOn; 6 7var notificationClassPnIsOn; 8var notificationClassPnIsOff; 9var notificationTextPnIsOn; 10var notificationTextPnIsOff; 11 12var notificationUserId; 13var notificationAccessToken; 14var notificationHasSubscription; 15var notificationFcmToken; 16 17/* common notification methods */ 18 19var isNotificationSupported = function () { 20 return 'Notification' in window && 21 'serviceWorker' in navigator && 22 'localStorage' in window && 23 'fetch' in window && 24 'postMessage' in window; 25}; 26 27var notificationAskPermission = function () { 28 debug("askNotificationPermission"); 29 Notification.requestPermission().then(function (permission) { 30 if (permission === 'granted') { 31 console.log('Notification permission granted.'); 32 notificationInit(); 33 } else { 34 console.log('Unable to get permission to notify.'); 35 } 36 }); 37}; 38 39var notificationInit = function () { 40 debug("notificationInit"); 41 42 var notificationMessaging = firebase.messaging(); 43 notificationMessaging.requestPermission() 44 .then(function () { 45 console.log('have permission'); 46 return notificationMessaging.getToken(); 47 }) 48 .then(function (currentFcmToken) { 49 if (currentFcmToken) { 50 console.log(currentFcmToken); 51 notificationFcmToken = currentFcmToken; 52 notificationUserId = getCookie('user_id'); 53 notificationAccessToken = getCookie('access_token'); 54 var isProfilePage = window.location.pathname.search('cabinet/profile') > 0; 55 if (isProfilePage) { 56 notificationCheckSubscriptionStatus(currentFcmToken); 57 } 58 } 59 }) 60 .catch(function (err) { 61 console.log('Error Occured') 62 }); 63 64 // handle catch the notification on current page 65 notificationMessaging.onMessage(function (payload) { 66 console.log('Message received', payload); 67 68 // register fake ServiceWorker for show notification on mobile devices 69 //navigator.serviceWorker.register('/firebase-messaging-sw.js'); 70 if (!navigator.serviceWorker.controller) { 71 navigator.serviceWorker.register('/sw.js'); 72 } 73 navigator.serviceWorker.ready.then(function (registration) { 74 // Copy data object to get parameters in the click handler 75 payload.data.data = JSON.parse(JSON.stringify(payload.data)); 76 77 registration.showNotification(payload.data.title, payload.data); 78 }).catch(function (error) { 79 console.log(error); 80 }); 81 }); 82 83 // Callback fired if Instance ID token is updated. 84 notificationMessaging.onTokenRefresh(function () { 85 notificationMessaging.getToken() 86 .then(function (refreshedToken) { 87 console.log('Token refreshed'); 88 notificationRefreshToken(refreshedToken); 89 }) 90 .catch(function (error) { 91 console.log('Error while updating fcmToken'); 92 }); 93 }); 94}; 95 96var notificationSubscribe = function (fcmToken) { 97 debug("notificationSubscribe"); 98 var postData = { 99 access_token: notificationAccessToken, 100 endpoint: fcmToken, 101 platform: 'FCM_CHROME', 102 user_id: notificationUserId, 103 user_agent: navigator.userAgent 104 }; 105 $.ajax({url: "/api/notification/subscribe", type: "POST", data: postData}) 106 .done(function (response) { 107 debug("Notification subscribe result: "+JSON.stringify(response)); 108 if (response.error_code === 0) { 109 notificationSubscribeUI('Subscribed success!, fcmToken =' + fcmToken); 110 notificationHasSubscription = false; 111 notificationTokenSentToServer(false); 112 return; 113 } 114 notificationUnsubscribe(fcmToken); 115 return false; 116 }) 117 .fail(function () { 118 debug("Notification subscribe failed: "+JSON.stringify(response)); 119 // unsubscribe(fcmToken); 120 }); 121}; 122 123var notificationUnsubscribe = function (fcmToken) { 124 debug("notificationUnsubscribe"); 125 var postData = { 126 access_token: notificationAccessToken, 127 platform: 'FCM_CHROME', 128 endpoint: fcmToken, 129 user_id: notificationUserId 130 }; 131 $.ajax({url: "/api/notification/unsubscribe", type: "POST", data: postData}) 132 .done(function (response) { 133 notificationUnsubscribeUI('Unsubscribed success!'); 134 }) 135 .fail(function (jqXHR, textStatus) {
136 console.log("Call to support. Could not unregister device for push notification. Problem on server side."); 137 }); 138}; 139 140var notificationRefreshToken = function (refreshedToken) { 141 if (!isNotificationTokenAlreadyRefreshed(refreshedToken)) { 142 console.log('Sending token to server...'); 143 var existedToken = window.localStorage.getItem('sentFirebaseMessagingToken'); 144 notificationRefreshFcmToken(existedToken, refreshedToken); 145 } 146}; 147 148var notificationRefreshFcmToken = function (existedFcmToken, refreshedFcmToken) { 149 var postData = { 150 access_token: notificationAccessToken, 151 existedFcmToken: existedFcmToken, 152 refreshedFcmToken: refreshedFcmToken 153 }; 154 $.ajax({url: "/api/notification/refreshFcmToken", type: "POST", data: postData}) 155 .done(function (response) { 156 notificationTokenSentToServer(refreshedFcmToken); 157 }) 158 .fail(function (jqXHR, textStatus) { 159 console.log("Call to support. Could not unregister device for push notification. Problem on server side."); 160 }); 161}; 162 163var isNotificationTokenAlreadyRefreshed = function (refreshedToken) { 164 return window.localStorage.getItem('sentFirebaseMessagingToken') === refreshedToken; 165}; 166 167var notificationTokenSentToServer = function (currentToken) { 168 if (currentToken) { 169 window.localStorage.setItem('sentFirebaseMessagingToken', currentToken); 170 } else { 171 window.localStorage.removeItem('sentFirebaseMessagingToken'); 172 } 173}; 174 175var notificationCheckSubscriptionStatus = function (fcmToken) { 176 debug("notificationCheckSubscriptionStatus"); 177 $.ajax({ 178 url: "/api/notification/subscription", 179 type: "GET", 180 data: {access_token: notificationAccessToken, platform: 'FCM_CHROME', endpoint: fcmToken, user_id: notificationUserId} 181 }).done(function (response) { 182 if (response.error_code === 0) { 183 debug("notificationHasSubscription"); 184 notificationHasSubscription = true; 185 notificationTokenSentToServer(fcmToken); 186 } else { 187 debug("notificationCheckSubscriptionStatus response: "+JSON.stringify(response)); 188 var switchOnActionPage = window.location.pathname.search('cabinet/profile/notification') > 0; 189 if (switchOnActionPage) { 190 notificationSubscribe(fcmToken); 191 } 192 } 193 updateNotificationCheckboxState(fcmToken); 194 }).fail(function () { 195 196 }); 197}; 198 199/* end common notification methods */ 200 201/* common util methods */ 202 203var getCookie = function (name) { 204 var matches = document.cookie.match(new RegExp( 205 "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)" 206 )); 207 return matches ? decodeURIComponent(matches[1]) : undefined; 208}; 209 210/* end common util methods */ 211 212/* VMMO UI methods */ 213 214var updateNotificationCheckboxState = function (fcmToken) { 215 debug("updateNotificationCheckboxState"); 216 notificationClassPnIsOn = $('#notificationOnClass').text(); 217 notificationClassPnIsOff = $('#notificationOffClass').text(); 218 notificationTextPnIsOn = $('#notificationOn').text(); 219 notificationTextPnIsOff = $('#notificationOff').text(); 220 221 if (notificationHasSubscription) { 222 notificationSubscribeUI('Subscribed success!, fcmToken =' + fcmToken); 223 } else { 224 notificationUnsubscribeUI("We aren't subscribed to push, so set UI to allow the user to enable push."); 225 } 226 notificationHideSubscribeLink(); 227}; 228 229var notificationSwitcherChange = function notificationSwitcherChange() { 230 if (!isNotificationSupported()) { 231 return false; 232 } 233 234 if (!notificationIsOn) { 235 notificationSubscribe(notificationFcmToken); 236 return false; 237 } 238 239 notificationUnsubscribe(notificationFcmToken); 240}; 241 242var notificationUnsubscribeUI = function (message) { 243 debug("notificationUnsubscribeUI"); 244 $('#switcher1').removeClass(notificationClassPnIsOn); 245 $('#switcher1').addClass(notificationClassPnIsOff); 246 $('#switcher1').attr("href", ""); 247 $('#notification_status').text(notificationTextPnIsOff); 248 notificationIsOn = false;
249}; 250 251var notificationSubscribeUI = function (message) { 252 debug("notificationSubscribeUI: "+message); 253 $('#switcher1').removeClass(notificationClassPnIsOff); 254 $('#switcher1').addClass(notificationClassPnIsOn); 255 $('#switcher1').attr("href", ""); 256 $('#notification_status').text(notificationTextPnIsOn); 257 notificationIsOn = true; 258}; 259 260var notificationHideSubscribeLink = function () { 261 $('#switcher').css('display', 'none'); 262 $('#switcher1').css('display', 'block'); 263}; 264 265var notificationCheckState = function () { 266 if (!isNotificationSupported()) { 267 debug('notifications not supported'); 268 return; 269 } 270 271 var switchOnActionPage = window.location.pathname.search('cabinet/profile/notification') > 0; 272 273 if (switchOnActionPage) { 274 notificationAskPermission(); 275 } else if (Notification.permission === 'granted') { 276 notificationInit(); 277 } 278}; 279 280/* end VMMO UI methods */
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.