-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
95 lines (86 loc) · 3.16 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>Firebase Web Push Notification Example</h2>
<p id="token"></p>
<!-- <script src="https://www.gstatic.com/firebasejs/7.14.6/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.14.6/firebase-messaging.js"></script> -->
<script src="firebase-app.js"></script>
<script src="firebase-messaging.js"></script>
<script>
const firebaseConfig = {
apiKey: "AIzaSyBs-mmPWkn8TrT3-rN7OaMSnXTrbYhO8JY",
authDomain: "php-test-e37a6.firebaseapp.com",
projectId: "php-test-e37a6",
storageBucket: "php-test-e37a6.appspot.com",
messagingSenderId: "385347176363",
appId: "1:385347176363:web:d1f12ecc4006d0fcd874ee",
measurementId: "G-BFD8TX2G1D"
};
//sleep(5000);
if(firebase.messaging.isSupported()){
console.log("FCM Present");
}
else{
console.log("NO FCM");
}
firebase.initializeApp(firebaseConfig);
sleep(2000);
console.log("Back from Initialize,, going to call .messaging");
const messaging = firebase.messaging();
console.log("Back from Messaging,, going to call .messaging");
function sleep(milliseconds) {
console.log("sleeping" + milliseconds)
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
}
function IntitalizeFireBaseMessaging() {
messaging
.requestPermission()
.then(function () {
console.log("Notification Permission");
return messaging.getToken();
})
.then(function (token) {
console.log("Token : " + token);
document.getElementById("token").innerHTML = token;
})
.catch(function (reason) {
console.log(reason);
});
}
messaging.onMessage(function (payload) {
console.log(payload);
const notificationOption = {
body: payload.notification.body,
icon: payload.notification.icon
};
if (Notification.permission === "granted") {
var notification = new Notification(payload.notification.title, notificationOption);
notification.onclick = function (ev) {
ev.preventDefault();
window.open(payload.notification.click_action, '_blank');
notification.close();
}
}
});
messaging.onTokenRefresh(function () {
messaging.getToken()
.then(function (newtoken) {
console.log("New Token : " + newtoken);
})
.catch(function (reason) {
console.log(reason);
})
})
IntitalizeFireBaseMessaging();
</script>
</body>
</html>