Wenn /dash aufgerufen wird, wird in der Konsole ein MSG TOKEN ausgegeben, dieser muss kopiert werden. Wenn localhost:7000/push/<MSGTOKEN> aufgerufen wird, bekommt der user dem der MSG TOKEN gehört eine push notification, sollange er den Browser offen hat
This commit is contained in:
Georg Reisinger
2019-01-17 19:56:50 +01:00
parent 26b9e99778
commit f9f7e656a7
5 changed files with 90 additions and 4 deletions

View File

@ -0,0 +1,25 @@
// Give the service worker access to Firebase Messaging.
// Note that you can only use Firebase Messaging here, other Firebase libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/4.8.1/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/4.8.1/firebase-messaging.js');
// Initialize the Firebase app in the service worker by passing in the
// messagingSenderId.
firebase.initializeApp({
'messagingSenderId': 'YOUR-SENDER-ID'
});
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(payload => {
const title = payload.data.title
const options = {
body: payload.data.text
}
return self.registration.showNotification(title, options)
})

View File

@ -6,11 +6,13 @@ var config = {
projectId: "test-667ca",
storageBucket: "test-667ca.appspot.com",
messagingSenderId: "221332577314"
};
};
firebase.initializeApp(config);
$(document).ready(function() {
firebase.auth().onAuthStateChanged(async function(user){if(user){firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idtoken) {
console.log("/shoppinglistx idtoken:", idtoken);

View File

@ -0,0 +1,32 @@
// 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 => {
console.log("MSG TOKEN: ", token)
})
.catch(err => {
console.error("Msg Error: ", err)
})
msg.onMessage(payload => {
const title = payload.data.title
new Notification(title, { body: payload.data.text});
})