-
Notifications
You must be signed in to change notification settings - Fork 1
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 #35 from QRTaxi/push-noti
Feat: 브라우저 푸시 알림 기능 추가
- Loading branch information
Showing
13 changed files
with
886 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,10 @@ | ||
VITE_BASE_URL = 'api.qrtaxi.co.kr' | ||
VITE_BASE_URL = 'api.qrtaxi.co.kr' | ||
|
||
VITE_FB_API_KEY = 'AIzaSyArnrcm0hW_k7suIGw_BgaFLIlJxTTxkPk' | ||
VITE_FB_AUTH_DOMAIN = 'qrtaxi-caf9c.firebaseapp.com' | ||
VITE_FB_PROJECT_ID = 'qrtaxi-caf9c' | ||
VITE_FB_STORAGE_BUCKET = 'qrtaxi-caf9c.appspot.com' | ||
VITE_FB_MESSAGING_SENDER_ID = '951135648092' | ||
VITE_FB_APP_ID = '1:951135648092:web:adcb142f98491e56e26999' | ||
VITE_FB_MEASUREMENT_ID = 'G-MKRQG86R31' | ||
VITE_FB_VAPID_KEY = 'BEccaYOA6A6N2Xi9mf9RSz9lBNQwHkN918FWBYkoebObXQxQcXmBebux53cKsWq7eOEGlXUdKU3zV8JaAQ7MH-g' |
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 @@ | ||
**/*.js |
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
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,42 @@ | ||
/// <reference lib="webworker" /> | ||
|
||
export default null; | ||
|
||
self.addEventListener('install', event => { | ||
console.log('fcm sw install..'); | ||
event.waitUntil(self.skipWaiting()); | ||
// .catch((error: Error) => console.error(error)) | ||
}); | ||
|
||
self.addEventListener('activate', () => { | ||
console.log('fcm sw activate..'); | ||
}); | ||
|
||
self.addEventListener('push', event => { | ||
if (!event.data) return; | ||
|
||
const message = event.data.json(); | ||
const notificationOptions = { | ||
body: message.notification.body, | ||
icon: '/icons/QT_initial_v1.svg', | ||
data: { url: `${self.location.origin}/${message.data.status}` }, | ||
}; | ||
event.waitUntil( | ||
self.registration.showNotification( | ||
message.notification.title, | ||
notificationOptions, | ||
), | ||
); | ||
}); | ||
|
||
self.addEventListener('notificationclick', event => { | ||
// Close the notification popout | ||
event.notification.close(); | ||
// Get all the Window clients | ||
event.waitUntil( | ||
clients | ||
.openWindow(event.notification.data.url) | ||
.then(windowClient => (windowClient ? windowClient.focus() : null)) | ||
.catch(error => console.error(error)), | ||
); | ||
}); |
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,63 @@ | ||
import { initializeApp } from 'firebase/app'; | ||
import { getMessaging, getToken, onMessage } from 'firebase/messaging'; | ||
import UserApi from '@/utils/api/user'; | ||
|
||
interface FirebaseConfig { | ||
apiKey: string; | ||
authDomain: string; | ||
projectId: string; | ||
storageBucket: string; | ||
messagingSenderId: string; | ||
appId: string; | ||
measurementId: string; | ||
} | ||
|
||
const firebaseConfig: FirebaseConfig = { | ||
apiKey: import.meta.env.VITE_FB_API_KEY as string, | ||
authDomain: import.meta.env.VITE_FB_AUTH_DOMAIN as string, | ||
projectId: import.meta.env.VITE_FB_PROJECT_ID as string, | ||
storageBucket: import.meta.env.VITE_FB_STORAGE_BUCKET as string, | ||
messagingSenderId: import.meta.env.VITE_FB_MESSAGING_SENDER_ID as string, | ||
appId: import.meta.env.VITE_FB_APP_ID as string, | ||
measurementId: import.meta.env.VITE_FB_MEASUREMENT_ID as string, | ||
}; | ||
|
||
// Initialize Firebase | ||
export const firebaseApp = initializeApp(firebaseConfig); | ||
export const messaging = getMessaging(firebaseApp); | ||
|
||
// getFirebaseToken function generates the FCM token | ||
export const handleFirebaseToken = async (assign_id: number) => { | ||
try { | ||
const fcm_token = await getToken(messaging, { | ||
vapidKey: import.meta.env.VITE_FB_VAPID_KEY as string, | ||
}); | ||
if (fcm_token && assign_id) { | ||
UserApi.postFirebaseToken({ assign_id, push_token: fcm_token }) | ||
.then((response: number) => { | ||
console.log(response); | ||
}) | ||
.catch((error: Error) => console.error(error)); | ||
// set token on localStorage | ||
localStorage.setItem('fcm_token', fcm_token); | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
}; | ||
|
||
export const requestPermission = async (assign_id: number) => { | ||
console.log('권한 요청 중...'); | ||
const permission = await Notification.requestPermission(); | ||
if (permission === 'denied') { | ||
console.log('알림 권한 허용 안됨'); | ||
return; | ||
} | ||
|
||
console.log('알림 권한이 허용됨'); | ||
handleFirebaseToken(assign_id).catch((error: Error) => console.error(error)); | ||
|
||
onMessage(messaging, payload => { | ||
console.log('메시지가 도착했습니다.', payload); | ||
}); | ||
}; |
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 |
---|---|---|
@@ -1,9 +1,12 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom/client'; | ||
import { RecoilRoot } from 'recoil'; | ||
import App from 'src/App'; | ||
|
||
ReactDOM.createRoot(document.getElementById('root')!).render( | ||
<React.StrictMode> | ||
<App /> | ||
<RecoilRoot> | ||
<App /> | ||
</RecoilRoot> | ||
</React.StrictMode>, | ||
); |
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
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
Oops, something went wrong.