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();
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) {
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(
notificationTitle,
notificationOptions
"[SW]" + payload.notification.title,
payload.notification
);
});

View File

@@ -48,9 +48,18 @@ if ("serviceWorker" in navigator) {
navigator.serviceWorker
.register("/firebase-messaging-sw.js")
.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) {
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);
};
messaging.onMessage((payload) => {
messaging.onMessage(async (payload) => {
console.log("**********UTILS Message received. ", payload);
navigator.serviceWorker.getRegistration().then((registration) => {
return registration.showNotification(
payload.notification.title + "from utils",
"[UTIL]" + payload.notification.title,
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]
// Update the UI to include the received message.
//appendMessage(payload);

View File

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

View File

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