1var GCM_ENDPOINT = 'https://android.googleapis.com/gcm/send'; 2 3var userSubscribed = false; 4var userEndpoint = false; 5var log_enable = false; 6var log_to_div = false; 7 8 9function subscribe() { 10 WriteLog('Initialize subscribe.'); 11 12 navigator.serviceWorker.ready.then(function (serviceWorkerRegistration) { 13 14 15 serviceWorkerRegistration.pushManager 16 .subscribe({ userVisibleOnly: true }) 17 .then(function (subscription) { 18 // The subscription was successful 19 userSubscribed = true; 20 WriteLog('Push Messages ENABLED.'); 21 WriteStatus(true); 22 SubscribeMessageHandler(false, false, true); 23 24 // TODO: Send the subscription.endpoint to your server 25 // and save it to send a push message at a later date 26 return sendSubscriptionToServer(subscription, true); 27 }) 28 .catch(function (e) { 29 userSubscribed = false; 30 WriteStatus(false); 31 SubscribeMessageHandler(false, false, false); 32 33 if (Notification.permission === 'denied') { 34 // The user denied the notification permission which 35 // means we failed to subscribe and the user will need 36 // to manually change the notification permission to 37 // subscribe to push messages 38 WriteLog('Permission for Notifications was denied', true); 39 } else { 40 // A problem occurred with the subscription; common reasons 41 // include network errors, and lacking gcm_sender_id and/or 42 // gcm_user_visible_only in the manifest. 43 WriteLog('Unable to subscribe to push.' + e, true); 44 } 45 }); 46 }); 47} 48 49function sendSubscriptionToServer(subscription, subscribe) { 50
51 WriteLog('Send to server'); 52 53 var rawKey = subscription.getKey ? subscription.getKey('p256dh') : ''; 54 key = rawKey ? btoa(String.fromCharCode.apply(null, new Uint8Array(rawKey))) : ''; 55 var rawAuthSecret = subscription.getKey ? subscription.getKey('auth') : ''; 56 authSecret = rawAuthSecret ? btoa(String.fromCharCode.apply(null, new Uint8Array(rawAuthSecret))) : ''; 57 58 var endpoint = subscription.endpoint; 59 60 var chromeToken = ''; 61 userEndpoint = endpoint; 62 if (endpoint.startsWith(GCM_ENDPOINT)) { 63 var endpointParts = endpoint.split('/') 64 chromeToken = endpointParts[endpointParts.length - 1] 65 endpoint = GCM_ENDPOINT; 66 } 67 68 WriteLog('endpoint:' + endpoint + ' key:' + key + ' authSecret:' + authSecret); 69 WriteLog('registrationId: ' + chromeToken); 70 71 //subscribe 72 var url = ''; 73 if (subscribe) { 74 url = n_subscribe_user_url; 75 } 76 //unsubscribe 77 else { 78 url = n_unsubscribe_user_url; 79 } 80 81 82 jQuery.ajax({ 83 url: url, 84 data: { 'Endpoint': endpoint, 'Key': key, 'AuthSecret': authSecret, 'ChromeToken': chromeToken }, 85 success: function (data) { 86 87 }, 88 error: function (xhr, status) { 89 90 }, 91 complete: function () { 92 93 } 94 }); 95} 96 97function unsubscribe() { 98 WriteLog('Initialize UNsubscribe.'); 99 100 navigator.serviceWorker.ready.then(function (serviceWorkerRegistration) { 101 // To unsubscribe from push messaging, you need get the 102 // subcription object, which you can call unsubscribe() on. 103 serviceWorkerRegistration.pushManager.getSubscription().then( 104 function (pushSubscription) { 105 106 SubscribeMessageHandler(false, false, false); 107 108 // Check we have a subscription to unsubscribe 109 if (!pushSubscription) { 110 // No subscription object, so set the state 111 // to allow the user to subscribe to push 112 userSubscribed = false; 113 WriteStatus(false); 114 return; 115 } 116 117 // Make a request to your server to remove 118 // the users data from your data store so you 119 // don't attempt to send them push messages anymore 120 sendSubscriptionToServer(pushSubscription, false); 121 122 // We have a subcription, so call unsubscribe on it 123 pushSubscription.unsubscribe().then(function () { 124 userSubscribed = false; 125 WriteStatus(false); 126 }).catch(function (e) { 127 // We failed to unsubscribe, this can lead to 128 // an unusual state, so may be best to remove 129 // the subscription id from your data store and 130 // inform the user that you disabled push 131 userSubscribed = false; 132 WriteStatus(false); 133 WriteLog('Unsubscription error: ' + e, true); 134 }); 135 } 136 ).catch(function (e) { 137 WriteLog('Error thrown while unsubscribing from ' + 'push messaging.' + e, true); 138 }); 139 }); 140} 141 142function initialiseState() { 143 WriteLog('Iinitialise State'); 144 145 // Are Notifications supported in the service worker? 146 if (!('showNotification' in ServiceWorkerRegistration.prototype)) { 147 WriteLog('Notifications aren\'t supported.', true); 148 SubscribeMessageHandler(true, false, false); 149 return; 150 } 151 152 // Check the current Notification permission. 153 // If its not granted, it's a permanent block until the 154 // user changes the permission 155 if (Notification.permission === 'denied') { 156 WriteLog('The user has not granted the notification permission:' + Notification.permission, true); 157 SubscribeMessageHandler(false, true, false); 158 return; 159 } 160 161 // Check if push messaging is supported 162 if (!('PushManager' in window)) { 163 WriteLog('Push messaging isn\'t supported.', true); 164 SubscribeMessageHandler(true, false, false); 165 return; 166 } 167 168 // We need the service worker registration to check for a subscription 169 navigator.serviceWorker.ready.then(function (serviceWorkerRegistration) { 170 // Do we already have a push message subscription? 171 serviceWorkerRegistration.pushManager.getSubscription() 172 .then(function (subscription) { 173 // Enable any UI which subscribes / unsubscribes from 174 // push messages. 175 176 if (!subscription) { 177 // We aren't subscribed to push, so set UI 178 // to allow the user to enable push 179 userSubscribed = false; 180 WriteStatus(false); 181 SubscribeMessageHandler(false, false, false); 182 return; 183 } 184 185 WriteStatus(true); 186 SubscribeMessageHandler(false, false, true); 187 188 // Keep your server in sync with the latest subscriptionId 189 sendSubscriptionToServer(subscription, true); 190 191 userSubscribed = true; 192 }) 193 .catch(function (err) { 194 WriteLog('Error during getSubscription()' + err, true); 195 }); 196 }); 197 198} 199 200function WriteLog(msg, error) { 201 if (log_to_div) { 202 var style = error ? ' class="err"' : ''; 203 $('.js-notification-msgs').append('<p' + style + '>' + msg + '</p>'); 204 } 205 else if (log_enable) { 206 console.log(msg); 207 } 208} 209 210function WriteStatus(subscribed) { 211 if (log_to_div) { 212 if (subscribed)
213 $('.js-subs-status').html('SUBSCRIBED'); 214 else 215 $('.js-subs-status').html('UN-SUBSCRIBED'); 216 } 217 else if (log_enable) { 218 var msg = subscribed ? 'SUBSCRIBED' : 'UN-SUBSCRIBED'; 219 console.log(msg); 220 } 221} 222 223 224//on click subscribe/unsubscribe 225function changeSubscription() { 226 if (userSubscribed) { 227 unsubscribe(); 228 } else { 229 subscribe(); 230 } 231} 232 233 234 235function InitSubscribeClickHandler() { 236 if ($('.wp-subscribe').length > 0) { 237 $('.wp-subscribe').click(function (e) { 238 e.preventDefault(); 239 subscribe(); 240 return false; 241 }); 242 } 243} 244 245function SubscribeMessageHandler($browserNotSupported, $messagesDisabled, $userAlreadySubscribed) { 246 $('.wp-subscribe-browser-disabled').hide(); 247 $('.wp-subscribe-messages-disabled').hide(); 248 $('.wp-subscribe-already').hide(); 249 $('.wp-subscribe-offer').hide(); 250 251 if ($browserNotSupported) { 252 $('.wp-subscribe-browser-disabled').show(); 253 } 254 else if ($messagesDisabled) { 255 $('.wp-subscribe-messages-disabled').show(); 256 } 257 else if ($userAlreadySubscribed) { 258 $('.wp-subscribe-already').show(); 259 } 260 else { 261 $('.wp-subscribe-offer').show(); 262 } 263} 264 265$(document).ready(function () { 266 267 // Check that service workers are supported, if so, progressively 268 // enhance and add push messaging support, otherwise continue without it. 269 if ('serviceWorker' in navigator) { 270 navigator.serviceWorker.register('./service-worker.js?001') 271 .then(initialiseState).then(InitSubscribeClickHandler); 272 } else { 273 WriteLog('Service workers aren\'t supported in this browser.', true); 274 SubscribeMessageHandler(true, false, false); 275 } 276 277 278}); 279
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.