Created
June 24, 2016 17:37
-
-
Save flavioso16/7701b271b0d7d9139fd9d4e6527d24f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var urlApi = "https://argos4.me/api/chromeMessage/", subscriptionId; self.addEventListener("push", function (i) { i.waitUntil(self.registration.pushManager.getSubscription().then(function (i) { subscriptionId = i.endpoint.split("/")[i.endpoint.split("/").length - 1], console.log(subscriptionId); var t = urlApi + subscriptionId; return fetch(t).then(function (i) { if (200 !== i.status) throw console.log("Looks like there was a problem. Status Code: " + i.status), new Error; return i.json().then(function (i) { if (i) return console.log("Getting message id: " + i.id), self.registration.showNotification(i.title, { body: i.message, icon: i.iconURI, requireInteraction: !0, tag: i.tag, data: JSON.stringify({ idNotification: i.id, subscriptionId: subscriptionId, url: i.url }) }); throw console.log("Looks like there was a problem."), new Error }) }) })) }), self.addEventListener("notificationclick", function (i) { console.log("On notification click: ", i.notification.tag), i.notification.close(), i.waitUntil(clients.matchAll({ type: "window" }).then(function (t) { for (var o = 0; o < t.length; o++) { var n = t[o]; if ("/" == n.url && "focus" in n) return n.focus() } if (clients.openWindow && (void 0 != i.notification.data || null != i.notification.data)) { var e = JSON.parse(i.notification.data); if (void 0 != e.idNotification || null != e.idNotification) { var s = urlApi + e.idNotification + "?subscription_id=" + e.subscriptionId + "&message_status=CLICKED"; fetch(s, { method: "post" }) } if (void 0 != e.url || null != e.url) return clients.openWindow(decodeURIComponent(e.url)) } })) }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment