-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
49 lines (46 loc) · 1.89 KB
/
index.html
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
<!-- [CODE START] index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FCM Web Push Notification Example</title>
</head>
<body>
<h1>Firebase Push Notification</h1>
<script type="module">
// [CODE START] Import Firebase modules
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-app.js";
import { getMessaging, getToken } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-messaging.js";
// [CODE END] Import Firebase modules
// [CODE START] Firebase configuration
const firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_AUTH_DOMAIN",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_STORAGE_BUCKET",
messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
appId: "YOUR_APP_ID",
measurementId: "YOUR_MEASUREMENT_ID"
};
// [CODE END] Firebase configuration
// Initialize Firebase app and messaging
const app = initializeApp(firebaseConfig);
const messaging = getMessaging(app);
// Service Worker registration and token retrieval
navigator.serviceWorker.register("sw.js").then(registration => {
getToken(messaging, { serviceWorkerRegistration: registration, vapidKey: 'YOUR_VAPID_KEY' })
.then((currentToken) => {
if (currentToken) {
console.log("Token is: " + currentToken);
} else {
console.log('No registration token available. Request permission to generate one.');
}
}).catch((err) => {
console.log('An error occurred while retrieving token. ', err);
});
});
</script>
</body>
</html>
<!-- [CODE END] index.html -->