Skip to content

Commit

Permalink
Merge pull request #366 from shreyas1434shinde/InAppNotification
Browse files Browse the repository at this point in the history
Issue #PS-2350 feat: Set up device token generation in local storage
  • Loading branch information
itsvick authored Nov 5, 2024
2 parents cd117fa + 74d3ff5 commit 371a23f
Show file tree
Hide file tree
Showing 14 changed files with 2,510 additions and 150 deletions.
12 changes: 11 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,15 @@
"label": "ଓଡ଼ିଆ",
"code": "or"
}
]
],
"firebaseConfig": {
"apiKey": "process.env.NEXT_PUBLIC_FCM_API_kEY",
"authDomain": "backend-e99c8.firebaseapp.com",
"projectId": "backend-e99c8",
"storageBucket": "backend-e99c8.appspot.com",
"messagingSenderId": "839139131975",
"appId": "1:839139131975:web:90af50d01e2eb0f510e762",
"measurementId": "G-5237RSF3TC"
},
"vapidKey": "process.env.NEXT_PUBLIC_VAPID_KEY"
}
36 changes: 36 additions & 0 deletions firebase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { initializeApp } from "firebase/app";
import { getMessaging, onMessage, getToken } from "firebase/messaging";
import config from './config.json';

const firebaseApp = initializeApp(config.firebaseConfig);
let messaging;

if (typeof window !== 'undefined') {
messaging = getMessaging();
}

export const requestPermission = async () => {
console.log('Requesting notification permission...');
const permission = await Notification.requestPermission();

if (permission === 'granted') {
const token = await getToken(messaging, {
vapidKey: process.env.NEXT_PUBLIC_VAPID_KEY,
});
console.log('Notification token:', token);
return token;
} else {
console.warn("Notification permission denied");
return null;
}
};

export const onMessageListener = () =>
new Promise((resolve) => {
if (messaging) {
onMessage(messaging, (payload) => {
console.log("Received foreground message:", payload);
resolve(payload);
});
}
});
Loading

0 comments on commit 371a23f

Please sign in to comment.