Commit c751cd21 authored by Curtis Adam's avatar Curtis Adam

Refactor WebPushNotifications

parent 01991144
import {Meteor} from "meteor/meteor";
import {WebPushNotifications} from "./webPushSubscriptions";
export let CardsetNavigation = class CardsetNavigation {
/**
* Creates a web push subscription for the current device.
* The Browser ask the user for permissions and creates the subscription.
* Afterwards the subscription will be saved for the current user via the
* Meteor-method addWebPushSubscription.
*/
static subscribeForPushNotification () {
try {
navigator.serviceWorker.getRegistration()
.then(function (registration) {
return registration.pushManager.getSubscription()
.then(function () {
return registration.pushManager.subscribe({userVisibleOnly: true});
});
})
.then(function (subscription) {
if (subscription) {
var rawKey = subscription.getKey ? subscription.getKey('p256dh') : '';
const key = rawKey ? btoa(String.fromCharCode.apply(null, new Uint8Array(rawKey))) : '';
var rawAuthSecret = subscription.getKey ? subscription.getKey('auth') : '';
const authSecret = rawAuthSecret ? btoa(String.fromCharCode.apply(null, new Uint8Array(rawAuthSecret))) : '';
const endpoint = subscription.endpoint;
const sub = {
endpoint: endpoint,
key: key,
authSecret: authSecret
};
Meteor.call("addWebPushSubscription", sub, function (error) {
if (error) {
throw new Meteor.Error(error.statusCode, 'Error subscription failed');
}
});
}
});
} catch (error) {
console.log(error);
}
}
/**
* Add the current user to the leitner algorithm.
*/
static addToLeitner (cardset_id) {
this.subscribeForPushNotification();
WebPushNotifications.subscribeForPushNotification();
Meteor.call('addToLeitner', cardset_id);
}
};
......@@ -4,6 +4,47 @@ import webPush from 'web-push';
export const WebPushSubscriptions = new Mongo.Collection("webPushSubscriptions");
export let WebPushNotifications = class WebPushNotifications {
/**
* Creates a web push subscription for the current device.
* The Browser ask the user for permissions and creates the subscription.
* Afterwards the subscription will be saved for the current user via the
* Meteor-method addWebPushSubscription.
*/
static subscribeForPushNotification () {
try {
navigator.serviceWorker.getRegistration()
.then(function (registration) {
return registration.pushManager.getSubscription()
.then(function () {
return registration.pushManager.subscribe({userVisibleOnly: true});
});
})
.then(function (subscription) {
if (subscription) {
let rawKey = subscription.getKey ? subscription.getKey('p256dh') : '';
const key = rawKey ? btoa(String.fromCharCode.apply(null, new Uint8Array(rawKey))) : '';
let rawAuthSecret = subscription.getKey ? subscription.getKey('auth') : '';
const authSecret = rawAuthSecret ? btoa(String.fromCharCode.apply(null, new Uint8Array(rawAuthSecret))) : '';
const endpoint = subscription.endpoint;
const sub = {
endpoint: endpoint,
key: key,
authSecret: authSecret
};
Meteor.call("addWebPushSubscription", sub, function (error) {
if (error) {
throw new Meteor.Error(error.statusCode, 'Error subscription failed');
}
});
}
});
} catch (error) {
console.log(error);
}
}
};
Meteor.methods({
/**
* Stores a web push subscription for the current user in the "webPushSubscriptions" collection.
......
......@@ -8,7 +8,7 @@ import {Route} from "../../api/route.js";
import {CardEditor} from "../../api/cardEditor";
import {Filter} from "../../api/filter";
import {MarkdeepEditor} from "../../api/markdeepEditor";
import {CardsetNavigation} from "../../api/cardsetNavigation";
import {WebPushNotifications} from "../../api/webPushSubscriptions";
let loadingScreenTemplate = 'loadingScreen';
......@@ -834,7 +834,7 @@ Router.route('/admin/settings', {
layoutTemplate: 'admin_main',
data: function () {
Session.set('helpFilter', undefined);
CardsetNavigation.subscribeForPushNotification();
WebPushNotifications.subscribeForPushNotification();
},
action: function () {
if (this.ready()) {
......
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