Push v1
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:
parent
26b9e99778
commit
f9f7e656a7
25
express-server/public/firebase-messaging-sw.js
Normal file
25
express-server/public/firebase-messaging-sw.js
Normal 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)
|
||||||
|
})
|
@ -6,11 +6,13 @@ var config = {
|
|||||||
projectId: "test-667ca",
|
projectId: "test-667ca",
|
||||||
storageBucket: "test-667ca.appspot.com",
|
storageBucket: "test-667ca.appspot.com",
|
||||||
messagingSenderId: "221332577314"
|
messagingSenderId: "221332577314"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
firebase.initializeApp(config);
|
firebase.initializeApp(config);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
firebase.auth().onAuthStateChanged(async function(user){if(user){firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idtoken) {
|
firebase.auth().onAuthStateChanged(async function(user){if(user){firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idtoken) {
|
||||||
console.log("/shoppinglistx idtoken:", idtoken);
|
console.log("/shoppinglistx idtoken:", idtoken);
|
||||||
|
32
express-server/public/javascripts/firebase-app.js
Normal file
32
express-server/public/javascripts/firebase-app.js
Normal 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});
|
||||||
|
})
|
@ -27,6 +27,29 @@ var firebaseAdmin = admin.initializeApp({
|
|||||||
databaseURL: 'https://test-667ca.firebaseio.com'
|
databaseURL: 'https://test-667ca.firebaseio.com'
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//Push
|
||||||
|
router.get('/push/:msgtoken', function (req, res, next) {
|
||||||
|
var message = {
|
||||||
|
data: {
|
||||||
|
title: 'Nachricht',
|
||||||
|
text: 'Die Einkaufsliste wurde geupdated'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const token = req.params.msgtoken
|
||||||
|
firebaseAdmin.messaging().sendToDevice(token, message)
|
||||||
|
.then(function(response) {
|
||||||
|
// See the MessagingDevicesResponse reference documentation for
|
||||||
|
// the contents of response.
|
||||||
|
console.log('Successfully sent message:', response);
|
||||||
|
})
|
||||||
|
.catch(function(error) {
|
||||||
|
console.log('Error sending message:', error);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
// Login und Dash
|
// Login und Dash
|
||||||
router.get('/', function(req, res, next) {
|
router.get('/', function(req, res, next) {
|
||||||
@ -35,8 +58,8 @@ router.get('/', function(req, res, next) {
|
|||||||
|
|
||||||
|
|
||||||
router.get('/dash/:idtoken', function(req, res, next) {
|
router.get('/dash/:idtoken', function(req, res, next) {
|
||||||
|
// const msgtoken = req.params.msgtoken;
|
||||||
var token = req.params.idtoken;
|
const token = req.params.idtoken;
|
||||||
var uid;
|
var uid;
|
||||||
firebaseAdmin.auth().verifyIdToken(token)
|
firebaseAdmin.auth().verifyIdToken(token)
|
||||||
.then(function(decodedToken) {
|
.then(function(decodedToken) {
|
||||||
@ -48,7 +71,9 @@ router.get('/dash/:idtoken', function(req, res, next) {
|
|||||||
}).catch(function(error) {
|
}).catch(function(error) {
|
||||||
console.log(error)
|
console.log(error)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
res.render('dash');
|
res.render('dash');
|
||||||
});
|
});
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">
|
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">
|
||||||
<link rel='stylesheet' href='/bootstrap/dist/css/bootstrap.min.css'>
|
<link rel='stylesheet' href='/bootstrap/dist/css/bootstrap.min.css'>
|
||||||
<script src="https://www.gstatic.com/firebasejs/5.7.0/firebase.js"></script>
|
<script src="https://www.gstatic.com/firebasejs/5.7.0/firebase.js"></script>
|
||||||
|
|
||||||
</head>
|
</head>
|
||||||
<body id="vue-app">
|
<body id="vue-app">
|
||||||
|
|
||||||
@ -112,5 +113,6 @@
|
|||||||
<script src="/javascripts/ajax.js"></script>
|
<script src="/javascripts/ajax.js"></script>
|
||||||
<script src="https://unpkg.com/ionicons@4.4.8/dist/ionicons.js"></script>
|
<script src="https://unpkg.com/ionicons@4.4.8/dist/ionicons.js"></script>
|
||||||
<script src="/javascripts/test.js"></script>
|
<script src="/javascripts/test.js"></script>
|
||||||
|
<script src="/javascripts/firebase-app.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
Loading…
x
Reference in New Issue
Block a user