diff --git a/client/public/firebase-messaging-sw.js b/client/public/firebase-messaging-sw.js index 1266de67c..e418e3a89 100644 --- a/client/public/firebase-messaging-sw.js +++ b/client/public/firebase-messaging-sw.js @@ -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 ); }); diff --git a/client/src/components/fcm-notification/fcm-notification.component.jsx b/client/src/components/fcm-notification/fcm-notification.component.jsx index 6f3fa32fa..d7a6f8bad 100644 --- a/client/src/components/fcm-notification/fcm-notification.component.jsx +++ b/client/src/components/fcm-notification/fcm-notification.component.jsx @@ -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 + ); }); } diff --git a/client/src/firebase/firebase.utils.js b/client/src/firebase/firebase.utils.js index 59c6a4c66..694d1d1bb 100644 --- a/client/src/firebase/firebase.utils.js +++ b/client/src/firebase/firebase.utils.js @@ -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); diff --git a/server/_test/test.js b/server/_test/test.js index 0607464d9..fd6c2f10b 100644 --- a/server/_test/test.js +++ b/server/_test/test.js @@ -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", }, }, diff --git a/server/sms/receive.js b/server/sms/receive.js index 4294916e8..9d7dab74d 100644 --- a/server/sms/receive.js +++ b/server/sms/receive.js @@ -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",