-
Notifications
You must be signed in to change notification settings - Fork 80
/
index.html
74 lines (63 loc) · 2.85 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
<html>
<head>
<title>Firebase Demo</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
</head>
<body>
<div class="container mt-5">
<div class="form-group">
<label>Token:</label>
<div class="alert alert-primary text-break" role="alert" id="token"></div>
</div>
<div class="form-group">
<label>Messages:</label>
<div class="alert alert-secondary text-break" role="alert" id="messages"></div>
</div>
<div class="form-group">
<label>Errors:</label>
<div class="alert alert-danger text-break" role="alert" id="error"></div>
</div>
</div>
<script src="https://www.gstatic.com/firebasejs/7.16.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.16.1/firebase-messaging.js"></script>
<script>
messagesElement = document.getElementById('messages');
tokenElement = document.getElementById('token');
errorElement = document.getElementById('error');
var config = {
'messagingSenderId': '{MESSAGING_SENDER_ID}',
'apiKey': '{API_KEY}',
'projectId': '{PROJECT_ID}',
'appId': '{APP_ID}'
};
firebase.initializeApp(config);
const messaging = firebase.messaging();
messaging.requestPermission()
.then(function () {
console.log('Notification permission granted.');
return messaging.getToken()
})
.then(function (token) {
tokenElement.innerHTML = token
})
.catch(function (err) {
errorElement.innerHTML = err
console.log('Unable to get permission to notify.', err);
});
messaging.onMessage((payload) => {
console.log('Message received. ', payload);
appendMessage(payload);
});
function appendMessage(payload) {
const messagesElement = document.querySelector('#messages');
const dataHeaderElement = document.createElement('h5');
const dataElement = document.createElement('pre');
dataElement.style = 'overflow-x:hidden;';
dataHeaderElement.textContent = 'Received message:';
dataElement.textContent = JSON.stringify(payload, null, 2);
messagesElement.appendChild(dataHeaderElement);
messagesElement.appendChild(dataElement);
}
</script>
</body>
</html>