From ffedc41c2681e5ad359953837693cab71da20e01 Mon Sep 17 00:00:00 2001 From: Patrick Fic Date: Wed, 22 Jul 2020 16:55:48 -0700 Subject: [PATCH] Testing firebase messaging properties. --- client/public/firebase-messaging-sw.js | 61 ++++------------------ client/src/firebase/firebase.utils.js | 6 +++ server.js | 6 +-- server/_test/test.js | 72 ++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 55 deletions(-) create mode 100644 server/_test/test.js diff --git a/client/public/firebase-messaging-sw.js b/client/public/firebase-messaging-sw.js index 7203506af..1266de67c 100644 --- a/client/public/firebase-messaging-sw.js +++ b/client/public/firebase-messaging-sw.js @@ -27,36 +27,10 @@ firebase.initializeApp({ 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]; -// console.log("[fbmsw] Posting Paylout to window client."); -// windowClient.postMessage(payload); -// } -// }) -// .then(() => { -// console.log("[fbmsw] Showing notification."); -// return registration.showNotification(JSON.stringify(payload)); -// }); -// return promiseChain; -// }); - -// messaging.onMessage((payload) => { -// console.log("Message received. ", payload); -// // ... -// }); - -self.addEventListener('fetch',() => console.log("fetch")); +self.addEventListener("fetch", () => console.log("fetch")); messaging.setBackgroundMessageHandler(function (payload) { + alert(); console.log( "**********[firebase-messaging-sw.js] Received background message ", payload @@ -74,30 +48,15 @@ messaging.setBackgroundMessageHandler(function (payload) { ); }); -// self.addEventListener("message", (message) => { -// const { payload } = message.data.firebaseMessaging; - -// navigator.serviceWorker.getRegistration().then((registration) => -// registration.showNotification(payload.notification.title, { -// body: payload.notification.body + "FROM SW", -// icon: "logo240.png", -// badge: "logo240.png", -// actions: [ -// { -// action: "respond", -// title: "Respond", -// }, -// ], -// }) -// ); -// }); - //Handles the notification getting clicked. self.addEventListener("notificationclick", function (event) { console.log("SW notificationclick", event); + // event.notification.close(); + if (event.action === "archive") { + // Archive action was clicked + archiveEmail(); + } else { + // Main body of notification was clicked + clients.openWindow("/inbox"); + } }); - -// self.addEventListener("message", (message) => { -// console.log("Push from SW", message); -// // registration.showNotification("Push from SW" + JSON.stringify(message.data)); -// }); diff --git a/client/src/firebase/firebase.utils.js b/client/src/firebase/firebase.utils.js index 609405130..59c6a4c66 100644 --- a/client/src/firebase/firebase.utils.js +++ b/client/src/firebase/firebase.utils.js @@ -63,6 +63,12 @@ export const logImEXEvent = (eventName, additionalParams, stateProp = null) => { messaging.onMessage((payload) => { console.log("**********UTILS Message received. ", payload); + navigator.serviceWorker.getRegistration().then((registration) => { + return registration.showNotification( + payload.notification.title + "from utils", + payload.notification + ); + }); // [START_EXCLUDE] // Update the UI to include the received message. //appendMessage(payload); diff --git a/server.js b/server.js index eaf4a23f0..e91764e4f 100644 --- a/server.js +++ b/server.js @@ -35,10 +35,8 @@ app.post("/sendemail", sendEmail.sendEmail); app.get("/test", async function (req, res) { res.status(200).send("OK"); }); - -app.post("/test", async function (req, res) { - res.status(200).send("OK"); -}); +const test = require("./server/_test/test.js"); +app.post("/test", test.testResponse); //Accounting-IIF const accountingIIF = require("./server/accounting/iif/iif"); diff --git a/server/_test/test.js b/server/_test/test.js new file mode 100644 index 000000000..0607464d9 --- /dev/null +++ b/server/_test/test.js @@ -0,0 +1,72 @@ +const path = require("path"); +const admin = require("../firebase/firebase-handler").admin; + +require("dotenv").config({ + path: path.resolve( + process.cwd(), + `.env.${process.env.NODE_ENV || "development"}` + ), +}); + +exports.testResponse = async (req, res) => { + console.log("Test"); + + const uniqueTokens = [ + "f7B-k-ceDNCEAIFYCfhF3M:APA91bEn-xOmUahCBMJBBDqXpVOZJnnb_qhWlo8eOPrIkvFeSc2nqaKd4D8zs3qqZ_VNgS_OhifsetJXcwtczO8N4k3xfDzCyI3i6j6YTUNK56QC-WNmVOLR2C_g-owy7hSvhGhWilZ3", + "eNdzsUqRBBZCM8LQKvqk6e:APA91bFgL0VQLf_TooYmHKQ7_b4H--ZmUYCdgiZpT7dxHSyEkpcCHUz33K7sKqgifUk8rMAEhSsHWa0TJgLbOJxWD6lJaGEpXn8G3PbunkJsJCNCA3CprMONylBr9d6hnQ5wnjUX2Gt6", + ]; + + var message = { + notification: { + title: "Test Notification", + body: "Test Body", + //click_action: "TEST CLICK ACTION", + }, + data: { + jobid: "1234", + title: "Test Notification", + 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", + }, + }, + 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" }], + + // renotify: true, + //tag: "1234", + //badge: "/badge-icon.png", + }, + }, + }; + + // Send a message to the device corresponding to the provided + // registration token. + admin + .messaging() + .sendMulticast(message) + .then((response) => { + // Response is a message ID string. + console.log( + "[TEST] Successfully sent FCM Broadcast.:", + response + //JSON.stringify(response) + ); + }) + .catch((error) => { + console.log("Error sending message:", error); + }); + + res.status(200).send("OK"); +};