var SetWebPush = function () {
var firebaseConfig = {
apiKey: 'AIzaSyDh-oKNFC2VaIPWmZTTgTMui7e5gcCPBd0',
authDomain: 'mamawaytest-18a3c.firebaseapp.com',
databaseURL: 'https://mamawaytest-18a3c.firebaseio.com',
projectId: 'mamawaytest-18a3c',
storageBucket: 'mamawaytest-18a3c.appspot.com',
messagingSenderId: '1056894828813',
appId: '1:1056894828813:web:821a864bdea94e20f2b044',
measurementId: 'G-SNRJBVM82P'
};
var publicVapidKey = 'BE-B8Bh5im2oW35EWGV28_sEe90fLmfrHETW_OLqKUcFq2a9CxS69MXT1iIp0QKf8eXPKr1bqVPG5PW3H_-2S5E';
firebase.initializeApp(firebaseConfig);
firebase.analytics();
var messaging = firebase.messaging();
navigator.serviceWorker.register('https://www.mamaway.com.tw/plugin/WebPushServiceWorker.js?date=' + new Date().getTime()).then(registration => {
messaging.useServiceWorker(registration);
messaging.usePublicVapidKey(publicVapidKey);
messaging.onTokenRefresh(() => {
messaging.getToken().then((refreshedToken) => {
console.log('Token refreshed. :' + refreshedToken);
}).catch((err) => {
// console.log('Unable to retrieve refreshed token ', err);
});
});
messaging.getToken().then((currentToken) => {
if (currentToken) {
var cookie = getCookie('ESi_Id_1');
fetch('https://cdp-mamaway.esi-tech.net/api/channel/webpush/SetToken', {
method: 'POST', body: JSON.stringify({
token: currentToken, cookie: cookie
}), headers: {
'Content-Type': 'application/json; charset=utf-8'
}
}).then(res => { });
console.log('Get Token :' + currentToken);
} else {
console.log('No Instance ID token available. Request permission to generate one.');
}
}).catch((err) => {
console.log('An error occurred while retrieving token. ', err);
});
});
}
var loadScripts = function () {
var urls = [
{ src: 'https://www.gstatic.com/firebasejs/7.17.2/firebase-app.js', async: false },
{ src: 'https://www.gstatic.com/firebasejs/7.17.2/firebase-auth.js', async: false },
{ src: 'https://www.gstatic.com/firebasejs/7.17.2/firebase-messaging.js', async: false },
{ src: 'https://www.gstatic.com/firebasejs/7.17.2/firebase-analytics.js', async: false },
{ src: 'https://www.googletagmanager.com/gtag/js?id=UA-175674502-1', async: true }];
var loadCount = 0;
var check = function () {
++loadCount;
if (loadCount === urls.length)
SetWebPush();
};
var headTag = document.getElementsByTagName('head')[0];
for (var i = 0; i < urls.length; i++) {
var tag = document.createElement('script');
tag.type = 'text/javascript';
tag.src = urls[i].src;
tag.async = urls[i].async;
tag.onload = check;
headTag.appendChild(tag);
}
};
var getCookie = function (cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return undefined;
}
if (typeof window.onload != 'function') {
window.onload = loadScripts;
}
else {
var oldHandler = window.onload;
window.onload = function () {
if (oldHandler) {
oldHandler();
}
loadScripts()
};
}