Commit 6d242bdc authored by Daniel Nassauer's avatar Daniel Nassauer

implemented web notifications from server

parent 3bc27bf2
import {Meteor} from "meteor/meteor";
import {Mongo} from "meteor/mongo";
import webPush from 'web-push';
export const WebPushSubscriptions = new Mongo.Collection("webPushSubscriptions");
......@@ -8,5 +9,22 @@ Meteor.methods({
WebPushSubscriptions.upsert({userId: Meteor.userId()}, {
$addToSet: {subscriptions: subscription}
});
},
sendPushNotificationsToUser: function (userId, message) {
webPush.setGCMAPIKey(Meteor.settings.private.FCM_API_KEY);
var data = WebPushSubscriptions.findOne({userId: userId});
data.subscriptions.forEach(function (sub) {
var subscription = {
endpoint: sub.endpoint,
keys: {
p256dh: sub.key,
auth: sub.authSecret
}
};
console.log(subscription);
webPush.sendNotification(subscription, message);
});
}
});
......@@ -63,6 +63,8 @@ function subscribeForPushNotification() {
Meteor.call("addWebPushSubscription", sub, function (error) {
if (error) {
throw new Meteor.Error(error.statusCode, 'Error subscription failed');
} else {
Meteor.call("sendPushNotificationsToUser", Meteor.userId(), "Yeah! Push Notifications!");
}
});
}
......
......@@ -8,7 +8,8 @@
"babel-runtime": "^6.20.0",
"chart.js": "^2.4.0",
"meteor-node-stubs": "~0.2.0",
"react": "^15.0.2"
"react": "^15.0.2",
"web-push": "^3.2.1"
},
"version": "1.0.0",
"main": "index.js",
......
......@@ -95,12 +95,11 @@ self.addEventListener('fetch', (event) => {
});
self.addEventListener('push', function (event) {
var payload = event.data ? event.data.text() : 'no payload';
event.waitUntil(
self.registration.showNotification('Title', {
lang: 'de',
body: 'Body',
icon: 'https://git.thm.de/uploads/project/avatar/374/cards_logo.png',
vibrate: [500, 100, 500],
self.registration.showNotification('THMcards', {
body: payload,
icon: 'https://git.thm.de/uploads/project/avatar/374/cards_logo.png'
})
);
});
......
......@@ -18,7 +18,8 @@
"BT_PUBLIC_KEY": "bf7p9crbdv2359gv"
},
"private": {
"BT_PRIVATE_KEY": "b508f71a0e0b01940b0fbe75940debb9"
"BT_PRIVATE_KEY": "b508f71a0e0b01940b0fbe75940debb9",
"FCM_API_KEY": "AAAAIST6Cb4:APA91bEazKYwihnbcAyqMaX_oNZvNlzoplnCKXpVtBKx-HfGqh9Oof9zufvxvcsINOxIs1guu9cUtXC1YGWFNIAJEDJN_vnc5xMqg1koTJd5bZ5jux3q2Qnstu4kiB_WrbehSPswvw_hIB-q-QNTmH0bVT1YTAXD3A"
},
"admin": {
"name": "hg13345"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment