You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

93 lines
3.4 KiB

# Test subscription message
#
# Setup with `pip install -r requirements.txt` and then `source venv/bin/activate`
#
# Usage: python webpush.py "ENDPOINT|P256DH|AUTH" PRIVATE_KEY TITLE
# ... where, yes, that second arg includes the "|" character, matching sqlite output.
# A title of "DIRECT_NOTIFICATION" will show a message, bypassing TimeSafari filters.
from pywebpush import webpush, WebPushException
import base64
import datetime
import json
import sys
# Subscription Info
# these below will only work for the browser that acquired the subscription info. You will need to extract those from our console OR the db.
# https://fcm.googleapis.com/fcm/send/eqNQV7MVPic:APA91bGrIMxqz3sQ4wboUkmZithJHMAdrNgjm6BYcIGmgJozgEGeg23JsXLlNpnKwzBCmUXh1ciHmE_3wZakHX-Rho5f9Xovc28nun4nH7w4BMoYzX27pOw_pC4FtfAkBQaQ-8jm36jf
# BDo2fIIN7qoA5bOVXdrHATZUSPHY7030V8PKW1mIHAZHDAxS-p6RggVeI7IZoi3bGxpR713RYY8H8vu-lX5LY1w
# sVR_s8J4JHv3h4ZmvemL5w
subscription_info = {
"endpoint": "https://fcm.googleapis.com/fcm/send/eqNQV7MVPic:APA91bGrIMxqz3sQ4wboUkmZithJHMAdrNgjm6BYcIGmgJozgEGeg23JsXLlNpnKwzBCmUXh1ciHmE_3wZakHX-Rho5f9Xovc28nun4nH7w4BMoYzX27pOw_pC4FtfAkBQaQ-8jm36jf",
"keys": {
"p256dh": "BDo2fIIN7qoA5bOVXdrHATZUSPHY7030V8PKW1mIHAZHDAxS-p6RggVeI7IZoi3bGxpR713RYY8H8vu-lX5LY1w",
"auth": "sVR_s8J4JHv3h4ZmvemL5w"
}
}
# set subscription_info from arg 1 (sqlite results)
if len(sys.argv) > 1:
argument = sys.argv[1]
parts = argument.split('|')
if len(parts) > 0:
subscription_info['endpoint'] = parts[0]
if len(parts) > 1:
subscription_info['keys']['p256dh'] = parts[1]
if len(parts) > 2:
subscription_info['keys']['auth'] = parts[2]
title = "Test"
if len(sys.argv) > 3:
title = sys.argv[3]
now = datetime.datetime.now().isoformat()
message = f"Message at {now}"
data = json.dumps({"title": title, "message": message})
# Private parts
# if you've got hex
#private_key_hex = "308187020100301306072a8648ce3d020106082a8648ce3d030107046d306b0201010420ac868a9588a69ec9626db857caae42b0d654288abf73b0d8f1a6b43fff093508a1440342000466da453c1e793d7e21ab63b334e80f96715aa97e578639b5fe8092f8752fa6e1f9182324846c70bf1a3480411ba787e652be8049a36a14294681f745c4c4c4f7"
#private_key_der = bytes.fromhex(private_key_hex)
#private_key_base64 = base64.b64encode(private_key_der).decode()
# if you've got base64
private_key_base64 = "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgtMWLjpglbBDApUkuWjhJhdik3zzpPFjwC5IO9J8v25uhRANCAARWOqEBw7A01F44GqE9SEX1FX7qcHCv9eBsbgGwcF/XQK48o+WGv38Q4g9o+W2WvakiNjFCDa6cePY1+ZyEnbca"
# set private_key_base64 if supplied from arg 2
if len(sys.argv) > 2:
private_key_base64 = sys.argv[2]
try:
#print(str(subscription_info))
#sys.exit(0)
response = webpush(
subscription_info,
data,
vapid_private_key=private_key_base64,
vapid_claims={"sub": "mailto:info@timesafari.app"}
)
print(f"Response: {response}")
print(f"Full response: {response.__dict__}")
# log the .reason from Apple
# https://developer.apple.com/documentation/usernotifications/sending_web_push_notifications_in_web_apps_and_browsers#3994594
except WebPushException as ex:
print(f"An error occurred: {ex}")
# Check if there is a response from the remote service.
if ex.response:
response_data = ex.response.json()
print(f"Error response from remote service: {response_data}")