-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
68 lines (60 loc) · 2.77 KB
/
test.html
File metadata and controls
68 lines (60 loc) · 2.77 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<!DOCTYPE html>
<html>
<head>
<title>Get FCM Token</title>
</head>
<body>
<h2>FCM Token Kamu:</h2>
<textarea id="tokenDisplay" style="width:100%; height:100px;"></textarea>
<button onclick="askPermission()">Ambil Token</button>
<script type="module">
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.0.0/firebase-app.js";
import { getMessaging, getToken } from "https://www.gstatic.com/firebasejs/9.0.0/firebase-messaging.js";
import { onMessage } from "https://www.gstatic.com/firebasejs/9.0.0/firebase-messaging.js";
const firebaseConfig = {
apiKey: "AIzaSyAS4Gb80L-qGj0s9MwE8iX2OkbaMwsSGZM",
authDomain: "connectx-50b64.firebaseapp.com",
projectId: "connectx-50b64",
storageBucket: "connectx-50b64.firebasestorage.app",
messagingSenderId: "356178114666",
appId: "1:356178114666:web:16ea31c4fbd3aabe05f213",
measurementId: "G-ZPTGJW889K"
};
const app = initializeApp(firebaseConfig);
const messaging = getMessaging(app);
// Pendengar pesan masuk saat tab lagi dibuka (Foreground)
onMessage(messaging, (payload) => {
console.log('Pesan diterima pas tab dibuka: ', payload);
// Cek apakah izin sudah diberikan sebelum munculin pop-up
if (Notification.permission === 'granted') {
const notificationTitle = payload.notification.title;
const notificationOptions = {
body: payload.notification.body,
icon: 'https://cdn-icons-png.flaticon.com/512/732/732200.png',
// Tambahkan tag agar notifikasi tidak menumpuk
tag: 'fcm-notification'
};
// Munculkan pop-up
new Notification(notificationTitle, notificationOptions);
} else {
console.log("Izin notifikasi belum diberikan.");
}
});
window.askPermission = () => {
Notification.requestPermission().then((permission) => {
if (permission === 'granted') {
getToken(messaging, { vapidKey: 'BKpOKU8MKEaWLLEmOlrS3Eg6c-fIHa_m1hjJK52gt7GR0SIWj1RIP1LFUMCOVUFN_hS7M4uJDzJsnccOsljGUHI' })
.then((currentToken) => {
if (currentToken) {
document.getElementById('tokenDisplay').value = currentToken;
console.log("Token: ", currentToken);
}
});
} else {
alert("Izin notif ditolak!");
}
});
}
</script>
</body>
</html>