-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpush.html
72 lines (62 loc) · 2.62 KB
/
webpush.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Firebase Web Push Notifications</title>
</head>
<body>
<button id="notify-button">Notify Me</button>
</body>
<!-- Include the Firebase SDK -->
<script src="https://www.gstatic.com/firebasejs/8.3.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.3.1/firebase-messaging.js"></script>
<script>
// Initialize Firebase
var firebaseConfig = {
apiKey: "AIzaSyAtrhpG_hOWxNIFuacOqz4IN1kzJzxsJlI",
authDomain: "iitismclubs.firebaseapp.com",
databaseURL: "https://iitismclubs-default-rtdb.firebaseio.com",
projectId: "iitismclubs",
storageBucket: "iitismclubs.appspot.com",
messagingSenderId: "242765089478",
appId: "1:242765089478:web:a06957583a09551e1965c7"
};
firebase.initializeApp(firebaseConfig);
const messaging = firebase.messaging();
// Set the target date (one day from now)
let x; // for year
let y; // for month and months begun from 0 and ends at 11 that is january is 0 and april is 3
let z; // for date
const targetDate = new Date(x,y,z,0,0,0);
targetDate.setDate(targetDate.getDate());
// Calculate the start of the target day (e.g. midnight)
const targetDayStart = new Date(targetDate.getFullYear(), targetDate.getMonth(), targetDate.getDate());
// Calculate the difference in milliseconds between now and the start of the target day
const timeDiff = targetDayStart.getTime() - new Date().getTime();
// Trigger the event at any time during the day before the target date
setTimeout(() => {
console.log('Event triggered!');
}, timeDiff - (24 * 60 * 60 * 1000));
const button = document.querySelector('#notify-button');
let NotificationTime = timeDiff - (24 * 60 * 60 * 1000);
button.addEventListener('click', () => {
messaging.requestPermission().then(() => {
console.log('Notification permission granted.');
messaging.getToken().then((token) => {
console.log('Token:', token);
setTimeout(()=>{
const notificationTitle = 'Hey';
const notificationOptions = {
body: 'This is a test message.'
};
new Notification(notificationTitle, notificationOptions);
},NotificationTime);
}).catch((err) => {
console.log('Error getting token:', err);
});
}).catch((err) => {
console.log('Unable to get permission to notify:', err);
});
});
</script>
</html>