'use strict'; var cid = cid || location.search.substring(5); function endpointWorkaround(pushSubscription) { if (pushSubscription.endpoint.indexOf('https://android.googleapis.com/gcm/send') !== 0) { return pushSubscription.endpoint; } var mergedEndpoint = pushSubscription.endpoint; if (pushSubscription.subscriptionId && pushSubscription.endpoint.indexOf(pushSubscription.subscriptionId) === -1) { mergedEndpoint = pushSubscription.endpoint + '/' + pushSubscription.subscriptionId; } return mergedEndpoint; } self.addEventListener('push', function(event) { console.log('Received a push message', event); event.waitUntil( event.currentTarget.registration.pushManager.getSubscription().then(function(subscription) { if (subscription.endpoint.indexOf('push.services.mozilla.com') != -1) { var subscriptionId = subscription.endpoint; } else { var mergedEndpoint = endpointWorkaround(subscription); var endpointSections = mergedEndpoint.split('/'); var subscriptionId = endpointSections[endpointSections.length - 1]; } var cdn = "https://cdn-jp.gsecondscreen.com"; if (location.host.search("localhost") != -1 || location.host.search("127.0.0.1") != -1) { cdn = "http://"+location.host } return fetch(cdn+"/newnaconf/"+cid+"/").then(function(response) { return response.json().then(function(cfg) { var proto = "https"; if (location.host.search("localhost") != -1 || location.host.search("127.0.0.1") != -1) { proto = "http"; } return fetch(proto+"://"+cfg['evbk']+"/wpn/?r="+subscriptionId+"&z="+(new Date().getTime())+""+parseInt(Math.random()*10000)) .then(function(response) { return response.json().then(function(data) { var payload = {} var title = data.title; var body = data.message; var icon = data.icon; var tag = {'url':data.activity,'evbk':cfg['evbk'],'trig_id':data.trig_id,'vid':data.vid}; payload['body'] = body; payload['icon'] = icon if('image' in data && data.image){ payload['image'] = data.image } if('actions' in data && data.actions){ payload['actions'] = data.actions tag["button1_url"] = data.button1_url tag["button2_url"] = data.button2_url } payload['tag'] = JSON.stringify(tag) payload['requireInteraction'] = true fetch(proto+"://"+cfg['evbk']+"/ev/?e="+encodeURIComponent("^push recved - "+data.trig_id)+"&c="+encodeURIComponent(cid)+"&v="+encodeURIComponent(data.vid)+"&s="+(new Date().getTime())+""+parseInt(Math.random()*10000)+"&t="+encodeURIComponent("abc")+"&z="+(new Date().getTime())+""+parseInt(Math.random()*10000)+"&ky=-&vl=-&tp=s",{'mode':'no-cors'}).then(function(){}); var r = self.registration.showNotification(title, payload); r.then(function(a) { console.log('push show done', a); }).catch(function(e) { console.log('push show err', e); }); return r; }); }); }) }) }) ); }); self.addEventListener('notificationclick', function(event) { console.log('On notification click: ', event.notification.tag); var data = JSON.parse(event.notification.tag); var url = data['url'] event.notification.close(); var action =''; if (event.action === 'like') { if (data['button1_url'] && data['button1_url'].search("http://") != 0 && data['button1_url'].search("https://") != 0) { url = "http://"+data['button1_url']; } else{ url = data['button1_url']; } action = "button1" } else if(event.action === 'reply') { if (data['button2_url'] && data['button2_url'].search("http://") != 0 && data['button2_url'].search("https://") != 0) { url = "http://"+data['button2_url']; } else { url = data['button2_url']; } action = "button2" } else if (data['url'].search("http://") != 0 && data['url'].search("https://") != 0) { url = "http://"+data['url']; action = "notification" } event.waitUntil( clients.openWindow(url) .then(function() { var proto = "https"; if (location.host.search("localhost") != -1 || location.host.search("127.0.0.1") != -1) { proto = "http"; } return fetch(proto+"://"+data['evbk']+"/ev/?e="+encodeURIComponent("^push open - "+data['trig_id'])+"&c="+encodeURIComponent(cid)+"&v="+encodeURIComponent(data['vid'])+"&s="+(new Date().getTime())+""+parseInt(Math.random()*10000)+"&t="+encodeURIComponent("abc")+"&z="+(new Date().getTime())+""+parseInt(Math.random()*10000)+"&ky=action&vl="+encodeURIComponent(action)+"&tp=s&ky=url&vl="+encodeURIComponent(url)+"&tp=s",{'mode':'no-cors'}).then(function(){}); }) ); });