firebase.initializeApp({ messagingSenderId: '959233513812' }); if ('Notification' in window) { var messaging = firebase.messaging(); if (Notification.permission === 'granted') { subscribe(); } subscribe(); } function subscribe() { messaging.requestPermission() .then(function () { messaging.getToken() .then(function (currentToken) { console.log(currentToken); if (currentToken) { sendTokenToServer(currentToken); } else { setTokenSentToServer(false); } }) .catch(function (err) { setTokenSentToServer(false); }); }) .catch(function (err) { }); } function sendTokenToServer(currentToken) { if (!isTokenSentToServer(currentToken)) { var url = 'https://public.wepo.com/web-push/subscribe'; $.get(url, { token: currentToken }); setTokenSentToServer(currentToken); } } // localStorage function isTokenSentToServer(currentToken) { return window.localStorage.getItem('sentFirebaseMessagingToken') == currentToken; } function setTokenSentToServer(currentToken) { window.localStorage.setItem( 'sentFirebaseMessagingToken', currentToken ? currentToken : '' ); }