Browse Source

add image, add testing for vapid key, and script to manually push

main
John-Mark Gurney 11 months ago
parent
commit
f8f616d2a8
5 changed files with 36 additions and 5 deletions
  1. +4
    -1
      Makefile
  2. +18
    -0
      pushnotify.sh
  3. +14
    -4
      src/S4_server.py
  4. BIN
      static/i-banner.png
  5. BIN
      static/i-ico.png

+ 4
- 1
Makefile View File

@@ -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 > $@

+ 18
- 0
pushnotify.sh View File

@@ -0,0 +1,18 @@
#!/bin/sh -

subinfo="$1"

if ! [ -f "$1" ] || [ $# != 1 ]; then
echo "Usage: $0 <subinfo>"
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"

+ 14
- 4
src/S4_server.py View File

@@ -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"


BIN
static/i-banner.png View File

Before After
Width: 1  |  Height: 1  |  Size: 82 B

BIN
static/i-ico.png View File

Before After
Width: 1  |  Height: 1  |  Size: 82 B

Loading…
Cancel
Save