Installation

firebase.js

import { getMessaging, onMessage, getToken } from "firebase/messaging";

const firebaseConfig = {
	apiKey: "xxx",
	authDomain: "xxx",
	databaseURL: "xxx",
	projectId: "xxx",
	storageBucket: "xxx",
	messagingSenderId: "xxxx",
	appId: "xxx",
	measurementId: "xxx",
	token_option: "xxx" // your vapid key
};

const configMessage = getMessaging(app)

export const fetchToken = async (setTokenId) => {
	try {
		const token = await getToken(configMessage, { vapidKey: firebaseConfig.token_option })
		if (token) {
			console.log(token,'this is push notif token')
			setTokenId(token);
		}
		else{
			console.log('no push notif token for now')
		}
	} catch (error) {
	}
}

export const onMessageListener = (toast) => {
	onMessage(configMessage, (payload) => {
		const { notification } = payload
		const { title, body } = notification
		toast({
			title: title,
			description: body,
			position: 'top-right',
			isClosable: true,
		})
	});
};

Last updated

Was this helpful?