Skip to content

Instantly share code, notes, and snippets.

@agronom81
Last active February 22, 2021 19:32
Show Gist options
  • Select an option

  • Save agronom81/15d79b65deb6c2bc7661a2d95afb1606 to your computer and use it in GitHub Desktop.

Select an option

Save agronom81/15d79b65deb6c2bc7661a2d95afb1606 to your computer and use it in GitHub Desktop.

Revisions

  1. agronom81 revised this gist Feb 22, 2021. 1 changed file with 0 additions and 12 deletions.
    12 changes: 0 additions & 12 deletions firebase-messaging-sw
    Original file line number Diff line number Diff line change
    @@ -13,18 +13,6 @@ try {
    });
    var messaging = firebase.messaging();

    messaging.setBackgroundMessageHandler(function (payload) {
    console.log("Handling background message", payload);

    // Copy data object to get parameters in the click handler
    payload.data.data = JSON.parse(JSON.stringify(payload.data));

    return self.registration.showNotification(
    payload.data.title,
    payload.data
    );
    });

    self.addEventListener("notificationclick", function (event) {
    const target = event.notification.data.click_action || "/";
    event.notification.close();
  2. agronom81 created this gist Feb 22, 2021.
    71 changes: 71 additions & 0 deletions firebase-messaging-sw
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    importScripts("https://www.gstatic.com/firebasejs/8.2.6/firebase-app.js");
    importScripts("https://www.gstatic.com/firebasejs/8.2.6/firebase-messaging.js");

    try {
    firebase.initializeApp({
    apiKey: "AIzaSyAZW895vVsq-sGPPRm52fQI3V3j6yvylRc",
    authDomain: "push-project-e1ffa.firebaseapp.com",
    projectId: "push-project-e1ffa",
    storageBucket: "push-project-e1ffa.appspot.com",
    messagingSenderId: "237926722474",
    appId: "1:237926722474:web:46601acb7c7a30e8bc59d3",
    measurementId: "G-XQ36BHPE6Y"
    });
    var messaging = firebase.messaging();

    messaging.setBackgroundMessageHandler(function (payload) {
    console.log("Handling background message", payload);

    // Copy data object to get parameters in the click handler
    payload.data.data = JSON.parse(JSON.stringify(payload.data));

    return self.registration.showNotification(
    payload.data.title,
    payload.data
    );
    });

    self.addEventListener("notificationclick", function (event) {
    const target = event.notification.data.click_action || "/";
    event.notification.close();

    // This looks to see if the current is already open and focuses if it is
    event.waitUntil(
    clients
    .matchAll({
    type: "window",
    includeUncontrolled: true,
    })
    .then(function (clientList) {
    // clientList always is empty?!
    for (var i = 0; i < clientList.length; i++) {
    var client = clientList[i];
    if (client.url === target && "focus" in client) {
    return client.focus();
    }
    }

    return clients.openWindow(target);
    })
    );
    });

    messaging.onBackgroundMessage((payload) => {
    console.log(
    "[firebase-messaging-sw.js] Received background message ",
    payload
    );
    // Customize notification here
    const notificationTitle = payload.notification.title;
    const notificationOptions = {
    body: payload.notification.body,
    };

    self.registration.showNotification(
    notificationTitle,
    notificationOptions
    );
    });
    } catch (e) {
    console.log(e);
    }