Skip to content

Add Firebase Messaging #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: hosting
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"**/node_modules/**"
],
"rewrites": [
{
"source": "/firebase-messaging-sw.js",
"destination": "/firebase-messaging-sw.js"
},
{
"source": "**",
"destination": "/index.html"
Expand Down
37 changes: 37 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "firebase/vertexai";


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


Expand Down Expand Up @@ -204,6 +205,42 @@ async function fileToGenerativePart(file: Blob) {

// --- Event Listener Setup ---
document.addEventListener('DOMContentLoaded', () => {
// Add Firebase Messaging setup
const messaging = getMessaging();

// Request permission and get token
Notification.requestPermission().then((permission) => {
if (permission === 'granted') {
console.log('Notification permission granted.');
// Get registration token. Initially this makes a network call.
// If the token is already cached it's retrieved from cache.
getToken(messaging, { vapidKey: 'BH2rCQkMjwmOTRH9jkjykkmx5xUx25cDweLmL6cTlbJznP_nxtivrycwO4ltmlX3TyiHQjGGjJtZJqADYefGtPE' }).then((currentToken) => {
if (currentToken) {
// Send the token to your server and update the UI if necessary
console.log('FCM token:', currentToken);
// You can now send this token to your backend to send notifications
} else {
// Show permission request UI
console.log('No registration token available. Request permission to generate one.');
// TODO: Display some UI to the user to request permission to receive notifications.
}
}).catch((err) => {
console.log('An error occurred while retrieving token. ', err);
// TODO: Handle error.
});
} else {
console.log('Unable to get permission to notify.');
}
});

// Handle incoming messages
onMessage(messaging, (payload) => {
console.log('Message received. ', payload);
// Customize notification here
const notificationTitle = payload.notification?.title;
const notificationOptions = { body: payload.notification?.body, icon: '/firebase-logo.png' };
new Notification(notificationTitle || 'New Message', notificationOptions);
});
const bTextOnlyInference = document.getElementById('bTextOnlyInference') as HTMLButtonElement;
const bTextAndImageInference = document.getElementById('bTextAndImageInference') as HTMLButtonElement;

Expand Down