diff --git a/Dockerfile b/Dockerfile index 8e2aa6f..ad7bf4f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,6 +6,7 @@ RUN apk add --no-cache --virtual .build-deps build-base git RUN apk add --upgrade --no-cache bash sqlite libffi-dev tzdata ENV TZ America/New_York +ENV PYTHONUNBUFFERED 1 # Set the working directory in the container to /app WORKDIR /app diff --git a/app.py b/app.py index 6f6b248..e13f70a 100644 --- a/app.py +++ b/app.py @@ -48,20 +48,27 @@ def create_app(config_name): def send_push_notification(subscription_info, message, vapid_key): + result = True try: - private_key_bytes = bytes.fromhex(vapid_key.private_key) + private_key_hex = vapid_key.private_key + private_key_der = bytes.fromhex(private_key_hex) + + private_key_base64 = base64.b64encode(private_key_der).decode() webpush( subscription_info=subscription_info, data=json.dumps(message), - vapid_private_key=private_key_bytes, + vapid_private_key=private_key_base64, vapid_claims={ "sub": "mailto:your-email@example.com" } ) - except WebPushException as e: + except Exception as e: + result = False print(f"Failed to send notification: {e}") + + return result def is_valid_base64_url(s): @@ -127,9 +134,9 @@ def create_app(config_name): } message = {"title": "Subscription Successful", "body": "Thank you for subscribing!"} - send_push_notification(subscription_info, message, vapid_key) - - return jsonify(success=True) + success = send_push_notification(subscription_info, message, vapid_key) + + return jsonify(success=success, message=vapid_key.private_key) @app.route('/web-push/unsubscribe', methods=['DELETE']) diff --git a/webpush.py b/webpush.py new file mode 100644 index 0000000..b21b723 --- /dev/null +++ b/webpush.py @@ -0,0 +1,34 @@ +from pywebpush import webpush, WebPushException + +import json + +# 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/e5jo1pnUZPA:APA91bEwHV2DeLybHc23G_-zJ4FXIdK_fh16ANVOrBekDeKNdmvZ0ukkPXsYGvq629KR2_ZK2w6rQHwC6hkbZ6Xqg0HktarvFT7wTQPjq7bkLgqqjOoONYZDPriDd9DvkD2vamzrPoBx +# BCj38CJs7M9rXDnZLBVU-XHYrAIdqCuTdscC_Waj96Z2KcMuTHI46kKssjmNwdkIae3Plr__UXbisdC5YPsIeFA +# 8cyYFZDqjGiZ4nBMfa2L8Q + +# MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgrIaKlYimnslibbhXyq5CsNZUKIq/c7DY8aa0P/8JNQihRANCAARm2kU8Hnk9fiGrY7M06A+WcVqpfleGObX+gJL4dS+m4fkYIySEbHC/GjSAQRunh+ZSvoBJo2oUKUaB90XExMT3 + +subscription_info = { + "endpoint": "https://fcm.googleapis.com/fcm/send/e5jo1pnUZPA:APA91bEwHV2DeLybHc23G_-zJ4FXIdK_fh16ANVOrBekDeKNdmvZ0ukkPXsYGvq629KR2_ZK2w6rQHwC6hkbZ6Xqg0HktarvFT7wTQPjq7bkLgqqjOoONYZDPriDd9DvkD2vamzrPoBx", + "keys": { + "p256dh": "BCj38CJs7M9rXDnZLBVU-XHYrAIdqCuTdscC_Waj96Z2KcMuTHI46kKssjmNwdkIae3Plr__UXbisdC5YPsIeFA", + "auth": "8cyYFZDqjGiZ4nBMfa2L8Q" + } +} + +data = json.dumps({"title": "test", "message": "here is a message"}) + +try: + webpush(subscription_info, + data, +# vapid_private_key="./private_key.pem", + vapid_private_key="MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgrIaKlYimnslibbhXyq5CsNZUKIq/c7DY8aa0P/8JNQihRANCAARm2kU8Hnk9fiGrY7M06A+WcVqpfleGObX+gJL4dS+m4fkYIySEbHC/GjSAQRunh+ZSvoBJo2oUKUaB90XExMT3", + vapid_claims={"sub": "mailto:matthew.raymer@gmail.com"}) +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"Response from remote service: {response_data}")