Moved firebase messaging notification handling to messaging generation.

This commit is contained in:
Patrick Fic
2020-07-23 08:30:28 -07:00
parent ffedc41c26
commit 3eb2c0d787
5 changed files with 49 additions and 31 deletions

View File

@@ -27,24 +27,15 @@ firebase.initializeApp({
const messaging = firebase.messaging(); const messaging = firebase.messaging();
self.addEventListener("fetch", () => console.log("fetch")); self.addEventListener("fetch", (fetch) => {
//required for installation as a PWA. Can ignore for now.
//console.log("fetch", fetch);
});
messaging.setBackgroundMessageHandler(function (payload) { messaging.setBackgroundMessageHandler(function (payload) {
alert();
console.log(
"**********[firebase-messaging-sw.js] Received background message ",
payload
);
// Customize notification here
const notificationTitle = "Background Message Title";
const notificationOptions = {
body: payload.notification.body + "FROM SW",
icon: "logo240.png",
};
return self.registration.showNotification( return self.registration.showNotification(
notificationTitle, "[SW]" + payload.notification.title,
notificationOptions payload.notification
); );
}); });

View File

@@ -48,9 +48,18 @@ if ("serviceWorker" in navigator) {
navigator.serviceWorker navigator.serviceWorker
.register("/firebase-messaging-sw.js") .register("/firebase-messaging-sw.js")
.then(function (registration) { .then(function (registration) {
console.log("**********FCM Registration successful, scope is:", registration.scope); console.log(
"**********FCM Registration successful, scope is:",
registration.scope
);
navigator.serviceWorker.addEventListener("message", (event) => {
console.log("Handler for Navigator Service Worker.", event);
});
}) })
.catch(function (err) { .catch(function (err) {
console.log("**********FCM Service worker registration failed, error:", err); console.log(
"**********FCM Service worker registration failed, error:",
err
);
}); });
} }

View File

@@ -61,14 +61,30 @@ export const logImEXEvent = (eventName, additionalParams, stateProp = null) => {
analytics.logEvent(eventName, eventParams); analytics.logEvent(eventName, eventParams);
}; };
messaging.onMessage((payload) => { messaging.onMessage(async (payload) => {
console.log("**********UTILS Message received. ", payload); console.log("**********UTILS Message received. ", payload);
navigator.serviceWorker.getRegistration().then((registration) => { navigator.serviceWorker.getRegistration().then((registration) => {
return registration.showNotification( return registration.showNotification(
payload.notification.title + "from utils", "[UTIL]" + payload.notification.title,
payload.notification payload.notification
); );
}); });
// if (!payload.clientId) return;
// // Get the client.
// const client = await clients.get(payload.clientId);
// // Exit early if we don't get the client.
// // Eg, if it closed.
// if (!client) return;
// // Send a message to the client.
// console.log("Posting to client.");
// client.postMessage({
// msg: "Hey I just got a fetch from you!",
// url: payload.request.url,
// });
// [START_EXCLUDE] // [START_EXCLUDE]
// Update the UI to include the received message. // Update the UI to include the received message.
//appendMessage(payload); //appendMessage(payload);

View File

@@ -28,24 +28,26 @@ exports.testResponse = async (req, res) => {
body: "Test Body", body: "Test Body",
}, },
tokens: uniqueTokens, tokens: uniqueTokens,
android: { // android: {
notification: { // notification: {
body: "This is an FCM notification specifically for android.", // body: "This is an FCM notification specifically for android.",
title: "FCM Notification for Android", // title: "FCM Notification for Android",
//image: "url-to-image", // image: "/logo192.png",
}, // badge: "/logo192.png",
}, // },
// },
webpush: { webpush: {
headers: { headers: {
// Urgency: "high", // Urgency: "high",
}, },
notification: { notification: {
body: "This is a message from FCM to web", body: "This is a message from FCM to web",
// requireInteraction: "true", requireInteraction: "true",
// actions: [{ action: "the action - matched in sw", title: "title" }], actions: [{ action: "the action - matched in sw", title: "Read" }],
// renotify: true, // renotify: true,
//tag: "1234", //tag: "1234", image: "/logo192.png",
badge: "/logo240.png",
//badge: "/badge-icon.png", //badge: "/badge-icon.png",
}, },
}, },

View File

@@ -75,9 +75,9 @@ exports.receive = (req, res) => {
const uniqueTokens = [...new Set(allTokens)]; const uniqueTokens = [...new Set(allTokens)];
var message = { var message = {
notification: { notification: {
title: `New SMS From ${phone(req.body.From)[0]}`, title: `SMS - ${phone(req.body.From)[0]}`,
body: req.body.Body, body: req.body.Body,
//click_action: "TEST CLICK ACTION", click_action: "TEST CLICK ACTION",
}, },
data: { data: {
jobid: "1234", jobid: "1234",