forked from summit-webapp/summit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
push-notifications.js
85 lines (77 loc) · 2.8 KB
/
push-notifications.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// import firebase, { initializeApp } from 'firebase/app';
import { initializeApp } from 'firebase/app';
import { getMessaging, getToken, deleteToken } from 'firebase/messaging';
import { requestPermission } from 'firebase/messaging';
export const initializeFirebase = () => {
console.log('firebase app init', initializeApp);
const firebaseConfig = {
messagingSenderId: '418835235707',
projectId: 'test-web-push-a2336',
apiKey: 'AIzaSyDN4CdmegnU636eD7peIPctuPhM4UtaIq8',
appId: '1:418835235707:web:600b74e364068af64c73de',
authDomain: 'test-web-push-a2336.firebaseapp.com',
storageBucket: 'test-web-push-a2336.appspot.com',
measurementId: 'G-61W9QD50GF',
};
initializeApp(firebaseConfig);
};
export const askForPermissionToReceiveNotifications = async () => {
console.log('Ask for permission');
try {
const messaging = getMessaging();
const currentToken = await getToken(messaging);
// if (currentToken) {
// // Token exists, delete the token
// await deleteToken(messaging, currentToken);
// console.log("Deleted existing token:", currentToken);
// }
// const newToken = await getToken(messaging);
console.log('New token:', currentToken);
return newToken;
} catch (error) {
console.log('Error:', error);
}
};
// export const askForPermissionToReceiveNotifications = async () => {
// console.log("Ask for permission");
// console.log("request permission", requestPermission);
// try {
// const messaging = getMessaging();
// // await requestPermission();
// const token = await getToken(messaging);
// console.log("User token:", token);
// return token;
// } catch (error) {
// console.log("err in catch");
// console.error(error);
// }
// };
// export const askForPermissionToReceiveNotifications = () =>
// {
// const messaging = getMessaging();
// getToken(messaging).then((currentToken) => {
// if (currentToken) {
// // Token exists, proceed with unsubscribe
// deleteToken(messaging, currentToken)
// .then(() => {
// console.log("Token unsubscribed successfully");
// // Subscribe to a new token
// getToken(messaging)
// .then((newToken) => {
// console.log("Subscribed to new token:", newToken);
// // Use the new token for sending notifications or other operations
// })
// .catch((error) => {
// console.log("Error retrieving new token:", error);
// });
// })
// .catch((error) => {
// console.log("Error unsubscribing token:", error);
// });
// } else {
// console.log("Token does not exist, no need to unsubscribe");
// }
// }).catch((error) => {
// console.log("Error retrieving token:", error);
// });
// }