-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
74 lines (68 loc) · 2.6 KB
/
app.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
// importScripts("https://www.gstatic.com/firebasejs/8.2.4/firebase-messaging.js");
const firebaseConfig = {
apiKey: "AIzaSyDjGhW6K1J6hrWdbBV_CpmrWj_Mmvk_6GM",
authDomain: "my-web-push-1dc51.firebaseapp.com",
databaseURL: "https://my-web-push-1dc51-default-rtdb.firebaseio.com",
projectId: "my-web-push-1dc51",
storageBucket: "my-web-push-1dc51.appspot.com",
messagingSenderId: "412483144779",
appId: "1:412483144779:web:2ec1e879e2ab666c7751ce",
measurementId: "G-PFPSSDG7G1"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const messaging = firebase.messaging();
function subscribe(){
console.log("Working");
Notification.requestPermission().then(permission=>{
console.log(permission);
if(permission =="granted"){
messaging.getToken({vapidKey:"BHZeQQQfY8Nd3S6IbfCMFRETPsApwhd_csxmj8NS_OXU1Bc5f1jA_A8l7O_aLP4yHAov-7mz8eSjZtcWqPMiIbQ"}).then(currentToken=>{
console.log(currentToken);
document.getElementById("token").innerHTML=
currentToken;
var tokenId=currentToken;
var name =document.getElementById('name').value
firebase.database().ref('Tokens/'+name).set({
TokenOfParticipant: tokenId
});
})
}
})
}
function getname(){
var participant=document.getElementById('participant').value
firebase.database().ref('Tokens/'+participant).on('value',function(snapshot){
document.getElementById('token').value= snapshot.val().TokenOfParticipant;
});
}
messaging.onMessage(res=>{
console.log(res);
})
function sendNotification(){
const token =document.getElementById('token').value
const title = document.getElementById('title').value
const msg =document.getElementById('msg').value
let body={
to:token,
notification:{
title:title,
body:msg,
icon:'unknown.png'
},
}
let options={
method:"POST",
headers: new Headers({
Authorization:
"key=AAAAYAnpTEs:APA91bHcD8QWjhjvERc88fNXy5_rXq8CdN9MPA0LvZ58XeyDTL4-k3a-QlEr5hq4pZ3Pw1RwTJH-4nwKyD9u17FySLVESWDOVx_qW5keCwgv1tWOahgmrwRevBcJV6vzPRlLLLlOwIgc",
"Content-Type": "application/json"
}),
body:JSON.stringify(body)
}
fetch("https://fcm.googleapis.com/fcm/send",options).then(res=>{
// console.log("SENT");
console.log(res);
}).catch(e =>console.log(e))
// console.log(body);
}