51 lines
1.3 KiB
JavaScript
51 lines
1.3 KiB
JavaScript
|
|
// Initialize Firebase
|
|
// const firebaseconfig = {
|
|
// apiKey: "AIzaSyCuvwf78cmSDoZ2yS4XxHZhnjUn7yIHYfw",
|
|
// authDomain: "test-667ca.firebaseapp.com",
|
|
// databaseURL: "https://test-667ca.firebaseio.com",
|
|
// projectId: "test-667ca",
|
|
// storageBucket: "test-667ca.appspot.com",
|
|
// messagingSenderId: "221332577314"
|
|
// };
|
|
// firebase.initializeApp(firebaseconfig);
|
|
|
|
|
|
|
|
var msg = firebase.messaging();
|
|
msg.requestPermission()
|
|
.then(function(){
|
|
console.log("Zugriff auf msg");
|
|
return msg.getToken();
|
|
})
|
|
.then(token => {
|
|
updateUser(token);
|
|
|
|
})
|
|
.catch(err => {
|
|
console.error("Msg Error: ", err);
|
|
})
|
|
|
|
function updateUser(token) {
|
|
firebase.auth().onAuthStateChanged(function(user){if(user){firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idtoken) {
|
|
$.ajax({
|
|
type: "POST",
|
|
url: "/user",
|
|
data:{
|
|
idtoken: idtoken,
|
|
message_id: token
|
|
},
|
|
success(){
|
|
console.log("User updated");
|
|
},
|
|
error(err){
|
|
console.error("Error: " + err);
|
|
}
|
|
});
|
|
}).catch((error) => console.error("Get id token client error: ", error));}else{console.log("Check Auth error", user)}});
|
|
}
|
|
|
|
msg.onMessage(payload => {
|
|
const title = payload.data.title
|
|
new Notification(title, { body: payload.data.text});
|
|
}) |