-
Notifications
You must be signed in to change notification settings - Fork 247
/
index.js
70 lines (64 loc) · 2.12 KB
/
index.js
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
import firebase from "firebase/app";
import "firebase/messaging";
function getMessagingObject() {
// [START messaging_get_messaging_object]
const messaging = firebase.messaging();
// [END messaging_get_messaging_object]
}
function receiveMessage() {
const messaging = firebase.messaging();
// [START messaging_receive_message]
// Handle incoming messages. Called when:
// - a message is received while the app has focus
// - the user clicks on an app notification created by a service worker
// `messaging.onBackgroundMessage` handler.
messaging.onMessage((payload) => {
console.log('Message received. ', payload);
// ...
});
// [END messaging_receive_message]
}
function getToken() {
const messaging = firebase.messaging();
// [START messaging_get_token]
// Get registration token. Initially this makes a network call, once retrieved
// subsequent calls to getToken will return from cache.
messaging.getToken({ vapidKey: '<YOUR_PUBLIC_VAPID_KEY_HERE>' }).then((currentToken) => {
if (currentToken) {
// Send the token to your server and update the UI if necessary
// ...
} else {
// Show permission request UI
console.log('No registration token available. Request permission to generate one.');
// ...
}
}).catch((err) => {
console.log('An error occurred while retrieving token. ', err);
// ...
});
// [END messaging_get_token]
}
function requestPermission() {
// [START messaging_request_permission]
Notification.requestPermission().then((permission) => {
if (permission === 'granted') {
console.log('Notification permission granted.');
// TODO(developer): Retrieve a registration token for use with FCM.
// ...
} else {
console.log('Unable to get permission to notify.');
}
});
// [END messaging_request_permission]
}
function deleteToken() {
const messaging = firebase.messaging();
// [START messaging_delete_token]
messaging.deleteToken().then(() => {
console.log('Token deleted.');
// ...
}).catch((err) => {
console.log('Unable to delete token. ', err);
});
// [END messaging_delete_token]
}