Skip to content

Instantly share code, notes, and snippets.

@chrisquinnr
Created October 2, 2017 09:30
Show Gist options
  • Save chrisquinnr/6e20efa32750b5ed9e439a75008e07fc to your computer and use it in GitHub Desktop.
Save chrisquinnr/6e20efa32750b5ed9e439a75008e07fc to your computer and use it in GitHub Desktop.

Revisions

  1. chrisquinnr created this gist Oct 2, 2017.
    86 changes: 86 additions & 0 deletions wapo push notifications service worker
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,86 @@
    (function() {
    var apiURL, recentNotificationsURL;
    var title = "Breaking News";
    var icon = "https://www.washingtonpost.com/wp-stat/desktop-notifications/icons/wp-icon-108.png";
    var Notifications = "push_notifications";
    var hasActiveNotification = false;
    recentNotificationsURL = "https://device-reg.wpdigital.net/pushv2/messages/recent?pushApp\x3dDESKTOP_ALERTS\x26days\x3d1";
    self.addEventListener("install", function(event) {
    self.skipWaiting();
    console.log("Installed", event)
    });
    self.addEventListener("activate", function(event) {
    console.log("Activated", event)
    });
    self.addEventListener("push", function(event) {
    if (Notification.permission == "granted") {
    console.log("Push message", event);
    var promise = fetch(recentNotificationsURL).then(function(response) {
    return response.json().then(function(data) {
    var notification_data = data["DESKTOP_ALERTS"];
    self.clients.matchAll().then(function(clients) {
    clients.forEach(function(client) {
    client.postMessage(notification_data)
    })
    });
    if (hasActiveNotification !== true) {
    hasActiveNotification = true;
    return self.registration.showNotification(title, {
    body: notification_data[0].headline,
    icon: icon,
    tag: "wapoNotif",
    data: notification_data
    }).then(function() {
    setTimeout(function() {
    hasActiveNotification = false
    }, 15E3)
    })
    } else
    return new Promise(function(resolve) {
    resolve()
    }
    )
    })
    })["catch"](function(error) {
    var message = {
    k: "PushNotification.APIResponseError",
    v: 1,
    tags: ["env:production"],
    sr: 1
    };
    fetch("https://pharos.arcpublishing.com/scout/count", {
    method: "POST",
    headers: {
    "Content-Type": "application/json;charset\x3dUTF-8"
    },
    body: JSON.stringify(message)
    }).then(function(response) {})
    });
    event.waitUntil(promise)
    }
    });
    self.addEventListener("notificationclick", function(event) {
    var notification_data = event.notification.data;
    var promise = clients.matchAll({
    type: "window"
    }).then(function(windowClients) {
    var contentURL = notification_data[0].contentURL
    , messageId = notification_data[0].pushID;
    for (var i = 0; i < windowClients.length; i++) {
    var client = windowClients[i];
    if (client.url === contentURL && "focus"in client)
    return client.focus()
    }
    if (clients.openWindow) {
    if (contentURL.indexOf("?") > -1)
    contentURL += "\x26tid\x3dnotifi_push_breaking-news\x26pushid\x3d" + messageId;
    else
    contentURL += "?tid\x3dnotifi_push_breaking-news\x26pushid\x3d" + messageId;
    event.notification.close();
    return clients.openWindow(contentURL)
    }
    });
    event.waitUntil(promise)
    })
    }
    )();