WIP FCM push messaging.

This commit is contained in:
Patrick Fic
2020-05-01 09:21:30 -07:00
parent c98e0b33fd
commit a9a8c3b6aa
10 changed files with 1327 additions and 37 deletions

View File

@@ -0,0 +1,44 @@
importScripts("https://www.gstatic.com/firebasejs/7.14.2/firebase-app.js");
importScripts(
"https://www.gstatic.com/firebasejs/7.14.2/firebase-messaging.js"
);
firebase.initializeApp({
apiKey: "AIzaSyDV9MsSHZmpLtjoaTK_ObvjFaJ-nMSd2KA",
authDomain: "bodyshop-dev-b1cb6.firebaseapp.com",
databaseURL: "https://bodyshop-dev-b1cb6.firebaseio.com",
projectId: "bodyshop-dev-b1cb6",
storageBucket: "bodyshop-dev-b1cb6.appspot.com",
messagingSenderId: "922785209028",
appId: "1:922785209028:web:96e9df15401eee5d784791",
measurementId: "G-2D5378VCHE",
});
const messaging = firebase.messaging();
//Handles Background Messages
messaging.setBackgroundMessageHandler(function (payload) {
console.log("backgroundMessageHandler", payload);
const promiseChain = clients
.matchAll({
type: "window",
includeUncontrolled: true,
})
.then((windowClients) => {
for (let i = 0; i < windowClients.length; i++) {
const windowClient = windowClients[i];
windowClient.postMessage(payload);
}
})
.then(() => {
return registration.showNotification(JSON.stringify(payload));
});
return promiseChain;
});
//Handles the notification getting clicked.
self.addEventListener("notificationclick", function (event) {
// do what you want
// ...
console.log("SW notificationclick", event);
});

View File

@@ -22,5 +22,6 @@
"start_url": ".",
"display": "standalone",
"theme_color": "#fff",
"background_color": "#fff"
"background_color": "#fff",
"gcm_sender_id": "103953800507"
}