-
Notifications
You must be signed in to change notification settings - Fork 1
/
firebase-messaging-sw.js
41 lines (32 loc) · 1.15 KB
/
firebase-messaging-sw.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { initializeApp } from "firebase/app";
import { getMessaging, getToken, onMessage } from "firebase/messaging";
const firebaseConfig = {
apiKey: process.env.REACT_APP_API_KEY,
authDomain: "test-messaging-b4969.firebaseapp.com",
projectId: "test-messaging-b4969",
storageBucket: "test-messaging-b4969.appspot.com",
messagingSenderId: process.env.REACT_APP_MESSAGING_SENDER_ID,
appId: process.env.REACT_APP_APP_ID,
measurementId: process.env.REACT_APP_MEASUREMENT_ID,
};
const app = initializeApp(firebaseConfig);
const messaging = getMessaging(app);
async function requestPermission() {
console.log("권한 요청 중...");
const permission = await Notification.requestPermission();
if (permission === "denied") {
console.log("알림 권한 허용 안됨");
return;
}
console.log("알림 권한이 허용됨");
const token = await getToken(messaging, {
vapidKey: process.env.REACT_APP_VAPID_KEY,
});
if (token) console.log("token: ", token);
else console.log("Can not get Token");
onMessage(messaging, (payload) => {
console.log("메시지가 도착했습니다.", payload);
// ...
});
}
requestPermission();