-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #366 from shreyas1434shinde/InAppNotification
Issue #PS-2350 feat: Set up device token generation in local storage
- Loading branch information
Showing
14 changed files
with
2,510 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
}); |
Oops, something went wrong.