-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfcm-tpl.html
47 lines (42 loc) · 1.47 KB
/
fcm-tpl.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
<script src="https://www.gstatic.com/firebasejs/5.11.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.11.1/firebase-messaging.js"></script>
<script>
// Your web app's Firebase configuration
var firebaseConfig = {{firebaseConfig}};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
// Prepare to receive messages
const messaging = firebase.messaging();
messaging.usePublicVapidKey("{{fcmPublicVapidKey}}");
messaging.requestPermission().then(function() {
console.log('Notification permission granted.');
// TODO(developer): Retrieve an Instance ID token for use with FCM.
// ...
messaging.getToken().then(function(token) {
if (token) {
console.log('Token got', token);
sendToServer(token);
} else {
console.log('No Instance ID token available. Request permission to generate one.');
}
}).catch(function(err) {
console.log('An error occurred while retrieving token. ', err);
});
messaging.onMessage(function (payload) {
// no time to complete this function
console.log(payload);
});
}).catch(function(err) {
console.log('Unable to get permission to notify.', err);
});
messaging.onTokenRefresh(function() {
messaging.getToken().then((token) => {
sendToServer(token);
});
});
function sendToServer(token) {
window.fetch(SERVER_ADDRESS + '/addToken?token=' + token, {
credentials: 'include'
});
};
</script>