y: visible'; } else { div.style = 'display: none'; } } function requestPermission() { // console.log('Requesting permission...'); Notification.requestPermission().then((permission) => { if (permission === 'granted') { // console.log('Notification permission granted.'); resetUI(); } else { // console.log('Unable to get permission to notify.'); } }); } function deleteToken() { messaging.getToken().then((currentToken) => { messaging.deleteToken(currentToken).then(() => { // console.log('Token deleted.'); deleteSubscriptionOnServer(currentToken); setTokenSentToServer(false); resetUI(); }).catch((err) => { // console.log('Unable to delete token. ', err); }); }).catch((err) => { // console.log('Error retrieving Instance ID token. ', err); // showToken('Error retrieving Instance ID token. ', err); }); } // Add a message to the messages element. function appendMessage(payload) { const messagesElement = document.querySelector('#messages'); const dataHeaderELement = document.createElement('h5'); const dataElement = document.createElement('pre'); dataElement.style = 'overflow-x:hidden;'; dataHeaderELement.textContent = 'Received message:'; dataElement.textContent = JSON.stringify(payload, null, 2); messagesElement.appendChild(dataHeaderELement); messagesElement.appendChild(dataElement); } // Clear the messages element of all children. function clearMessages() { const messagesElement = document.querySelector('#messages'); while (messagesElement.hasChildNodes()) { messagesElement.removeChild(messagesElement.lastChild); } } function updateUIForPushEnabled(currentToken) { showHideDiv(tokenDivId, true); showHideDiv(permissionDivId, false); showToken(currentToken); } function updateUIForPushPermissionRequired() { showHideDiv(tokenDivId, false); showHideDiv(permissionDivId, true); } function sendSubscriptionOnServer(token) { const url = 'https://push.opovo.com.br/api/web/v1/firebase/subscribe?token=' + token; const other_params = { headers : { "content-type" : "application/json"}, method : "POST", mode: 'no-cors' }; fetch(url, other_params).then(function(response) { if (response) { return response; } else { setTokenSentToServer(false); throw new Error("Could not reach the API"); } }).catch(function(error) { setTokenSentToServer(false); // console.log(error.message); }); } function deleteSubscriptionOnServer(token) { const url = 'https://push.opovo.com.br/api/web/v1/firebase/unsubscribe?token=' + token; const other_params = { headers : { "content-type" : "application/json"}, method : "POST", mode: 'no-cors' }; fetch(url, other_params).then(function(response) { if (response) { return response; } else { throw new Error("Could not reach the API"); } }).catch(function(error) { // console.log(error.message); }); } resetUI();