From f8f616d2a896dc20733a492f0ff15e889a3932cc Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Sat, 18 Nov 2023 10:59:08 -0800 Subject: [PATCH] add image, add testing for vapid key, and script to manually push --- Makefile | 5 ++++- pushnotify.sh | 18 ++++++++++++++++++ src/S4_server.py | 18 ++++++++++++++---- static/i-banner.png | Bin 0 -> 82 bytes static/i-ico.png | Bin 0 -> 82 bytes 5 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 pushnotify.sh create mode 100644 static/i-banner.png create mode 100644 static/i-ico.png diff --git a/Makefile b/Makefile index 5d0e0db..12b3995 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ PYTHON=python3 .PHONY: run -run: templates/S2_perm_sw.html templates/S4_server.py venv static/i-ico.png +run: templates/S2_perm_sw.html templates/S4_server.py venv static/i-ico.png static/i-banner.png ($(VENVACT) && python templates/S4_server.py ) VENVACT=. ./venv/bin/activate @@ -26,3 +26,6 @@ keys/public_key.txt keys/private_key.txt: venv S1_vapid.py static/i-ico.png: echo P6 1 1 255 255 0 0 | pnmtopng > $@ + +static/i-banner.png: + echo P6 1 1 255 0 0 255 | pnmtopng > $@ diff --git a/pushnotify.sh b/pushnotify.sh new file mode 100644 index 0000000..c66feb8 --- /dev/null +++ b/pushnotify.sh @@ -0,0 +1,18 @@ +#!/bin/sh - + +subinfo="$1" + +if ! [ -f "$1" ] || [ $# != 1 ]; then + echo "Usage: $0 " + exit 1 +fi + +tmpfname="tmp.pushinfo.$$" +cat - > "$tmpfname" + +title=$(grep -i "^Subject:" "$tmpfname" | sed -e 's/.*:[ ]*//') + +jq --rawfile msg "$tmpfname" --arg title "$title" -n '{ "title": $title, "body": $msg, "icon" : "static/i-ico.png", "image" : "static/i-banner.png" }' | + pywebpush -v --data /dev/stdin --info "$subinfo" + +rm "$tmpfname" diff --git a/src/S4_server.py b/src/S4_server.py index 9a9d33d..13f78c0 100644 --- a/src/S4_server.py +++ b/src/S4_server.py @@ -46,6 +46,17 @@ def donotify(sub, sleep=10): print('sending notification...') try: + # Mozilla appears to not require the vapid key. + # Chrome requires the vapid key. + if True: + kwargs = dict(vapid_private_key=None, + #vapid_claims=None, + ) + else: + kwargs = dict(vapid_private_key=VAPID_PRIVATE, + vapid_claims={ "sub": VAPID_SUBJECT }, + ) + webpush( subscription_info = sub, data = json.dumps({ @@ -53,10 +64,9 @@ def donotify(sub, sleep=10): "body" : "Yes, it works!", "icon" : "static/i-ico.png", "image" : "static/i-banner.png" - }), - vapid_private_key = VAPID_PRIVATE, - vapid_claims = { "sub": VAPID_SUBJECT } + }), **kwargs ) + print('done') except WebPushException as ex: print(ex) @@ -71,7 +81,7 @@ def push(): # (B3-1) GET SUBSCRIBER sub = json.loads(request.form["sub"]) import sys - print('sub:', repr(sub), file=sys.stderr) + print('sub:', repr(json.dumps(sub)), file=sys.stderr) # (B3-2) TEST PUSH NOTIFICATION result = "OK" diff --git a/static/i-banner.png b/static/i-banner.png new file mode 100644 index 0000000000000000000000000000000000000000..1db25472af7d6570128ceada89dc09587b0daee4 GIT binary patch literal 82 zcmeAS@N?(olHy`uVBq!ia0vp^j3CU&3?x-=hn)ga%mF?jt_BJQ20mw8fLtz57sn8e bnaK%2HWLHmm%@}~Ko*0itDnm{r-UW|I(iU1 literal 0 HcmV?d00001 diff --git a/static/i-ico.png b/static/i-ico.png new file mode 100644 index 0000000000000000000000000000000000000000..64ca5bb19cb1562a39f7663eb474f6dc8d5a7fc1 GIT binary patch literal 82 zcmeAS@N?(olHy`uVBq!ia0vp^j3CU&3?x-=hn)ga%mF?ju12P&CE{Egfm|+67sn8e bnaK%2HWLHmm%@}~Ko*0itDnm{r-UW|H02Ny literal 0 HcmV?d00001