Skip to content

Instantly share code, notes, and snippets.

@chokychou
Last active July 17, 2023 01:33
Show Gist options
  • Save chokychou/9b046036023f106deb7a28ddc3b79767 to your computer and use it in GitHub Desktop.
Save chokychou/9b046036023f106deb7a28ddc3b79767 to your computer and use it in GitHub Desktop.
Sample snippets for firebase messaging notification in Nextjs.
// This file goes to: public/firebase-messaging-sw.js
// Reference: https://stackoverflow.com/a/69207889/13091479
// Scripts for firebase and firebase messaging
importScripts("https://www.gstatic.com/firebasejs/8.2.0/firebase-app.js");
importScripts("https://www.gstatic.com/firebasejs/8.2.0/firebase-messaging.js");
// Initialize the Firebase app in the service worker by passing the generated config
const firebaseConfig = {
apiKey: "YOURDATA",
authDomain: "YOURDATA",
projectId: "YOURDATA",
storageBucket: "YOURDATA",
messagingSenderId: "YOURDATA",
appId: "YOURDATA",
measurementId: "YOURDATA",
};
firebase.initializeApp(firebaseConfig);
// Retrieve firebase messaging
const messaging = firebase.messaging();
messaging.onBackgroundMessage(function(payload) {
console.log("Received background message ", payload);
const notificationTitle = payload.notification.title;
const notificationOptions = {
body: payload.notification.body,
};
self.registration.showNotification(notificationTitle, notificationOptions);
});
// This file goes to: src/script.js
// Then import it to App.js.
// Reference: https://stackoverflow.com/a/69207889/13091479
"use client";
// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
import { getAnalytics } from "firebase/analytics";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
import { getMessaging, getToken } from "firebase/messaging";
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
measurementId: process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID,
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
async function requestPermission() {
Notification.requestPermission().then((permission) => {
if (permission === 'granted') {
console.log('Notification permission granted.');
// Get registration token. Initially this makes a network call, once retrieved
// subsequent calls to getToken will return from cache.
const messaging = getMessaging();
getToken(messaging, {
vapidKey:
process.env.NEXT_PUBLIC_FIREBASE_VAPID_KEY
}).then((currentToken) => {
if (currentToken) {
// Send the token to your server and update the UI if necessary
console.log("currentToken:", currentToken);
} else {
// Show permission request UI
console.log('No registration token available. Request permission to generate one.');
// ...
}
}).catch((err) => {
console.log('An error occurred while retrieving token. ', err);
// ...
});
} else {
console.log('Unable to get permission.');
}
});
}
requestPermission();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment