Skip to content

Commit

Permalink
CW-firebase-messaging-for-web Updated config
Browse files Browse the repository at this point in the history
  • Loading branch information
pvm-code committed Aug 15, 2024
1 parent 6b7537e commit 92e2dfc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 32 deletions.
59 changes: 32 additions & 27 deletions public/firebase-messaging-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,41 @@ importScripts(
"https://www.gstatic.com/firebasejs/10.7.1/firebase-messaging-compat.js",
);

let firebaseConfig = {};
let firebaseConfig = {
apiKey: "AIzaSyDbTFuksgOkIVWDiFe_HG7-BE8X6Dwsg-0",
authDomain: "common-dev-34b09.firebaseapp.com",
databaseURL: "https://common-dev-34b09.firebaseio.com",
projectId: "common-dev-34b09",
storageBucket: "common-dev-34b09.appspot.com",
messagingSenderId: "870639147922",
appId: "1:870639147922:web:9ee954bb1dd52e25cb7f4b",
};

self.addEventListener("message", (event) => {
console.log("--INIT_ENV", event);
if (event.data && event.data.type === "INIT_ENV") {
firebaseConfig = event.data.env;
initializeFirebase();
}
});
// self.addEventListener("message", (event) => {
// console.log("--INIT_ENV", event);
// if (event.data && event.data.type === "INIT_ENV") {
// firebaseConfig = event.data.env;
// initializeFirebase();
// }
// });

function initializeFirebase() {
if (firebaseConfig.apiKey) {
console.log("--firebaseConfig", firebaseConfig);
firebase.initializeApp(firebaseConfig);
// function initializeFirebase() {
if (firebaseConfig.apiKey) {
console.log("--firebaseConfig", firebaseConfig);
firebase.initializeApp(firebaseConfig);

const messaging = firebase.messaging();
const messaging = firebase.messaging();

messaging.onBackgroundMessage((payload) => {
console.log("--notif-back", payload);
const notificationTitle = payload.notification.title;
const notificationOptions = {
body: payload.notification.body,
data: payload.data,
icon: "/logo.png",
};
messaging.onBackgroundMessage((payload) => {
console.log("--notif-back", payload);
const notificationTitle = payload.notification.title;
const notificationOptions = {
body: payload.notification.body,
data: payload.data,
icon: "/logo.png",
};

self.registration.showNotification(
notificationTitle,
notificationOptions,
);
});
}
self.registration.showNotification(notificationTitle, notificationOptions);
});
}
// }
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ const NotificationsHandler: FC = () => {
}
})();

return () => {
unsubscribeLoad && unsubscribeLoad();
unsubscribeOnMessage && unsubscribeOnMessage();
};
// return () => {
// unsubscribeLoad && unsubscribeLoad();
// unsubscribeOnMessage && unsubscribeOnMessage();
// };
}, [userId]);

// useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/services/Notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class NotificationService {
}

public onForegroundMessage = () => {
console.log('-subscribe');
console.log('-subscribe', firebase.messaging().onMessage);
const unsubscribe = firebase.messaging().onMessage((payload) => {
console.log('Message received. ', payload);

Expand Down

0 comments on commit 92e2dfc

Please sign in to comment.