Last active
February 22, 2021 19:32
-
-
Save agronom81/15d79b65deb6c2bc7661a2d95afb1606 to your computer and use it in GitHub Desktop.
Revisions
-
agronom81 revised this gist
Feb 22, 2021 . 1 changed file with 0 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -13,18 +13,6 @@ try { }); var messaging = firebase.messaging(); self.addEventListener("notificationclick", function (event) { const target = event.notification.data.click_action || "/"; event.notification.close(); -
agronom81 created this gist
Feb 22, 2021 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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); }