Firebase Cloud Messaging and Chrome Extension Manifest V3 - mikecann.co.uk

PHOTO EMBED

Tue Mar 05 2024 19:36:10 GMT+0000 (Coordinated Universal Time)

Saved by @lebind12

import { getMessaging, onBackgroundMessage } from "firebase/messaging/sw"; // note: we MUST use the sw version of the messaging API and NOT the one from "firebase/messaging"
import { getToken } from "firebase/messaging";
import { initializeApp } from "firebase/app";

const firebase = initializeApp({
  // your Firebase config here
});

chrome.runtime.onInstalled.addListener(async () => {
  const token = await getToken(getMessaging(), {
    serviceWorkerRegistration: self.registration, // note: we use the sw of ourself to register with
  });

  // Now pass this token to your server and use it to send push notifications to this user
});

onBackgroundMessage(getMessaging(firebase), async (payload) => {
  console.log(`Huzzah! A Message.`, payload);

  // Note: you will need to open a notification here or the browser will do it for you.. something, something, security
});
content_copyCOPY

https://mikecann.co.uk/posts/firebase-cloud-messaging-and-chrome-extension-manifest-v3