1/* 2* 3* Push Notifications codelab 4* Copyright 2015 Google Inc. All rights reserved. 5* 6* Licensed under the Apache License, Version 2.0 (the "License"); 7* you may not use this file except in compliance with the License. 8* You may obtain a copy of the License at 9* 10* https://www.apache.org/licenses/LICENSE-2.0 11* 12* Unless required by applicable law or agreed to in writing, software 13* distributed under the License is distributed on an "AS IS" BASIS, 14* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15* See the License for the specific language governing permissions and 16* limitations under the License 17* 18*/ 19 20/* eslint-env browser, es6 */ 21 22'use strict'; 23 24const applicationServerPublicKey = 'BGeq7fPwu-FvKDCsPCJr_AH_WF6KrFRMuwv44PX7GIOqWMzkhRZqU2hPlSqA43ZOQ9j2qCpqbu1AcQzfzkzfYmI'; 25 26const pushButton = document.querySelector('.js-push-btn'); 27 28let isSubscribed = false; 29let swRegistration = null; 30 31function urlB64ToUint8Array(base64String) { 32 const padding = '='.repeat((4 - base64String.length % 4) % 4); 33 const base64 = (base64String + padding) 34 .replace(/\-/g, '+') 35 .replace(/_/g, '/'); 36 37 const rawData = window.atob(base64); 38 const outputArray = new Uint8Array(rawData.length); 39 40 for (let i = 0; i < rawData.length; ++i) { 41 outputArray[i] = rawData.charCodeAt(i); 42 } 43 return outputArray; 44} 45 46function updateBtn() { 47 if (isSubscribed) { 48 pushButton.textContent = 'Disable Push Messaging'; 49 } else { 50 pushButton.textContent = 'Enable Push Messaging'; 51 } 52 53 pushButton.disabled = false; 54} 55 56function updateSubscriptionOnServer(subscription) { 57 // TODO: Send subscription to application server 58 59 if (subscription) { 60 //aqui se guarda en la bd 61 var suscriptionJson = JSON.stringify(subscription); 62 63 var enviar = {}; 64 enviar.json = suscriptionJson; 65 66 if(typeof actual != 'undefined' && actual == 'fichapiso.php') { 67 enviar.datoofe = datoofeGet; 68 } 69 70 if(typeof numagencia != 'undefined') 71 enviar.numagencia = numagencia; 72 73 74 if(typeof clienteEscaparate != 'undefined') { 75 if(!clienteEscaparate) 76 return; 77 78 var url = '../plugins/__ajx/suscripcionpush/guardarAuthPushServidor.php'; 79 enviar.cliente = clienteEscaparate; 80 }else 81 var url = 'plugins/__ajx/suscripcionpush/guardarAuthPushServidor.php'; 82 83 84 $.ajax({ 85 url: url, 86 method: 'POST', 87 data: enviar 88 }) 89 .done(function( data ) { 90 }); 91 92 console.log(suscriptionJson); 93 } 94} 95 96function subscribeUser() { 97 const applicationServerKey = urlB64ToUint8Array(applicationServerPublicKey); 98 swRegistration.pushManager.subscribe({ 99 userVisibleOnly: true, 100 applicationServerKey: applicationServerKey 101 }) 102 .then(function(subscription) { 103 console.log('User is subscribed'); 104 105 updateSubscriptionOnServer(subscription); 106 107 isSubscribed = true; 108 109 // updateBtn(); 110 }) 111 .catch(function(err) { 112 console.log('Failed to subscribe the user: ', err); 113 // updateBtn(); 114 }); 115} 116 117function initializeUI() { 118 119 if(typeof clienteEscaparate == 'undefined' && desactivarNotificacionPush) 120 return false; 121 122 // Set the initial subscription value 123 swRegistration.pushManager.getSubscription() 124 .then(function(subscription) { 125 isSubscribed = !(subscription === null); 126 127 128 if (isSubscribed) { 129 swRegistration.update(); 130 updateSubscriptionOnServer(subscription); 131 } else { 132 133 var hora = localStorage.getItem('horaUltimoPromptPush'); 134 //5 minutos en milisegundos 135 var minutosMargen = 300000; 136 137 var d = new Date(); 138 var timestampActual = d.getTime(); 139
140 if( ( Notification.permission != 'denied' && hora == null || hora != null && timestampActual > (parseInt(hora) + minutosMargen) ) && (typeof clienteEscaparate == 'undefined' || typeof clienteEscaparate != 'undefined' && clienteEscaparate ) ) { 141 mostrarConfirmacionPush(); 142 return; 143 } 144 145 146 } 147 148 }); 149 150 151 152} 153 154function mostrarConfirmacionPush() 155{ 156 157 if(typeof clienteEscaparate == 'undefined' && desactivarNotificacionPush) 158 return false; 159 160 if(typeof clienteEscaparate != 'undefined') { 161 var url = '../plugins/__ajx/suscripcionpush/modalNotificacionesPush.php?cliente=' + clienteEscaparate; 162 var logo = '../img/header/logo.png'; 163 }else { 164 var url = 'plugins/__ajx/suscripcionpush/modalNotificacionesPush.php?cliente=0'; 165 var logo = 'img/header/logo.png'; 166 } 167 168 169 $.ajax({ 170 url: url, 171 type: "get", 172 dataType:"html", 173 async:true, 174 success: function(html){ 175 html = html.replace('img/header/logo.png', logo); 176 177 $(document).ready(function(){ 178 if(typeof clienteEscaparate != 'undefined') { 179 var obj = $('html'); 180 }else{ 181 var obj = $('body'); 182 } 183 184 $.when(obj.append(html)).then(function(){ 185 186 $('#confirmaPush_cancelar').off('click').on('click', function(){ 187 $(this).closest('#confirmaPush_Cont').remove(); 188 }); 189 190 $('#confirmaPush_aceptar').off('click').on('click', function(){ 191 subscribeUser(); 192 $(this).closest('#confirmaPush_Cont').remove(); 193 }); 194 195 }); 196 197 var d = new Date(); 198 var timestampActual = d.getTime(); 199 localStorage.setItem('horaUltimoPromptPush', timestampActual) 200 201 202 }); 203 204 } 205 }); 206 207} 208 209 210// if ('serviceWorker' in navigator && 'PushManager' in window) { 211// console.log('Service Worker and Push is supported'); 212// if(typeof clienteEscaparate != 'undefined') { 213// var swRoute = '../js/sw.js'; 214// }else { 215// var swRoute = 'js/sw.js'; 216// } 217// 218// navigator.serviceWorker.register(swRoute) 219// .then(function(swReg) { 220// console.log('Service Worker is registered', swReg); 221// 222// swRegistration = swReg; 223// initializeUI(); 224// }) 225// .catch(function(error) { 226// console.error('Service Worker Error', error); 227// }); 228// } else { 229// console.warn('Push messaging is not supported'); 230// }
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.