Add push notifications for forgotten departures #26

Merged
o.winkels merged 8 commits from push-notifications into master 2021-06-11 02:07:11 +02:00
6 changed files with 40 additions and 8 deletions
Showing only changes of commit 39a461df56 - Show all commits

View File

@ -187,10 +187,10 @@ sudo npm install -g web-push
web-push generate-vapid-keys web-push generate-vapid-keys
``` ```
The public Key needs to be copied into `web/main.js` (first line), while the The keys then need to be copied into the config options `push_public_key` and
private key is put into the config option `push_private_key`. `push_private_key` respectively so the backend can handle the rest.
Then, to be VAPID compliant you have to announce an contact address claim to Next, to be VAPID compliant you have to announce an contact address claim to
the push services so they can contact you if anything is going wrong with your the push services so they can contact you if anything is going wrong with your
notifications. Do this by entering your email address as a `mailto:` link in notifications. Do this by entering your email address as a `mailto:` link in
the `push_sender_info` option, like `mailto:it@fasttube.de`. the `push_sender_info` option, like `mailto:it@fasttube.de`.

View File

@ -26,8 +26,10 @@ guideline_url = https://fasttube.de/wp-content/uploads/2020/12/Cororna-Regeln-St
json_indent = 4 json_indent = 4
# VAPID credentials for push notifications # VAPID credentials for push notifications
# private key: base64url encoded public part of an EC-Prime256v1 keypair. See INSTALL.md
# private key: base64url encoded private part of an EC-Prime256v1 keypair. See INSTALL.md # private key: base64url encoded private part of an EC-Prime256v1 keypair. See INSTALL.md
# sender info: usually mailto link to responsible party to contact about issues # sender info: usually mailto link to responsible party to contact about issues
push_public_key = BBwBPYxhogHLU3B1FpxfQNzO3q7qZpmD1n1KaaL8WJbcVmJSHhi1uB-VmvsVjjUHWYCeqKyLT7w-1LBfpIcbbcg
push_private_key = abcdefghijklm_NOPQRSTUVWXYZ-0123456789 push_private_key = abcdefghijklm_NOPQRSTUVWXYZ-0123456789
push_sender_info = mailto:it@fasttube.de push_sender_info = mailto:it@fasttube.de
# when to notify users, in hours after arrival # when to notify users, in hours after arrival

View File

@ -171,6 +171,25 @@ def get_data():
return json.dumps(r, indent=SPACES), 200 return json.dumps(r, indent=SPACES), 200
@app.route('/pushinfo')
def get_pushinfo():
if config['notify_after_hrs']:
r = {
'enabled': True,
'publickey': config['push_public_key']
}
else:
r = {
'enabled': False,
'publickey': None
}
return json.dumps(r, indent=SPACES), 200
@app.route('/pushsubscribe', methods=['POST']) @app.route('/pushsubscribe', methods=['POST'])
def post_pushsub(): def post_pushsub():

View File

@ -26,8 +26,10 @@ guideline_url = https://youtu.be/oHg5SJYRHA0
json_indent = 4 json_indent = 4
# VAPID credentials for push notifications # VAPID credentials for push notifications
# private key: base64url encoded public part of an EC-Prime256v1 keypair. See INSTALL.md
# private key: base64url encoded private part of an EC-Prime256v1 keypair. See INSTALL.md # private key: base64url encoded private part of an EC-Prime256v1 keypair. See INSTALL.md
# sender info: usually mailto link to responsible party to contact about issues # sender info: usually mailto link to responsible party to contact about issues
push_public_key = abcdefghijklm_NOPQRSTUVWXYZ-0123456789abcdefghijklm_NOPQRSTUVWXYZ-0123456789abcdefghijklm_NOPQRSTUVWXYZ-0123456789
push_private_key = abcdefghijklm_NOPQRSTUVWXYZ-0123456789 push_private_key = abcdefghijklm_NOPQRSTUVWXYZ-0123456789
push_sender_info = mailto:admin@example.com push_sender_info = mailto:admin@example.com
# when to notify users, in hours after arrival # when to notify users, in hours after arrival

View File

@ -10,11 +10,10 @@ then
web-push generate-vapid-keys --json > $VAPID_CREDS_FILE web-push generate-vapid-keys --json > $VAPID_CREDS_FILE
echo "Patching public key into frontend ..." echo "Patching keypair into config ..."
PUB_KEY=`cat $VAPID_CREDS_FILE | jq -r .publicKey` PUB_KEY=`cat $VAPID_CREDS_FILE | jq -r .publicKey`
sed -i "s/pushServerPublicKey = '[a-zA-Z0-9_\-]*'/pushServerPublicKey = '${PUB_KEY}'/" /var/www/html/ftracker/main.js echo "pushServerPublicKey = ${PUB_KEY}" >> /var/www/html/ftracker/main.js
echo "Patching private key into backend config ..."
PRIV_KEY=`cat $VAPID_CREDS_FILE | jq -r .privateKey` PRIV_KEY=`cat $VAPID_CREDS_FILE | jq -r .privateKey`
echo "push_private_key = ${PRIV_KEY}" >> /etc/ftracker/config.ini echo "push_private_key = ${PRIV_KEY}" >> /etc/ftracker/config.ini

View File

@ -1,5 +1,3 @@
var pushServerPublicKey = 'BBwBPYxhogHLU3B1FpxfQNzO3q7qZpmD1n1KaaL8WJbcVmJSHhi1uB-VmvsVjjUHWYCeqKyLT7w-1LBfpIcbbcg'
var spage = document.getElementById('startpage') var spage = document.getElementById('startpage')
var mform = document.getElementById('mainform') var mform = document.getElementById('mainform')
@ -226,6 +224,18 @@ function initPush(name) {
return return
} }
fetch('/pushinfo').then(function(res) {
if (res.ok)
res.json().then(function(push) {
if (push.enabled)
registerPush(name, push.publickey);
});
});
}
function registerPush(name, pushServerPublicKey) {
// Register service worker // Register service worker
navigator.serviceWorker.register("/sw.js").then(function(swRegistration) { navigator.serviceWorker.register("/sw.js").then(function(swRegistration) {
console.log("ServiceWorker registered:", swRegistration) console.log("ServiceWorker registered:", swRegistration)