Browse Source

Add a pause before sending to the gateway

pull/1/head
Matthew Raymer 1 year ago
parent
commit
efedd5fa08
  1. 7
      app.py
  2. 9
      b64.py
  3. 22
      webpush.py

7
app.py

@ -7,11 +7,12 @@ from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import ec
import binascii
import base64
import binascii
import json
import os
import re
import time
def create_app(config_name):
app = Flask(__name__)
@ -60,7 +61,7 @@ def create_app(config_name):
data=json.dumps(message),
vapid_private_key=private_key_base64,
vapid_claims={
"sub": "mailto:your-email@example.com"
"sub": "mailto:matthew.raymer@gmail.com"
}
)
@ -125,6 +126,8 @@ def create_app(config_name):
db.session.add(subscription)
db.session.commit()
time.sleep(5)
subscription_info = {
"endpoint": subscription.endpoint,
"keys": {

9
b64.py

@ -0,0 +1,9 @@
import base64
private_key_hex = "308187020100301306072a8648ce3d020106082a8648ce3d030107046d306b0201010420ac868a9588a69ec9626db857caae42b0d654288abf73b0d8f1a6b43fff093508a1440342000466da453c1e793d7e21ab63b334e80f96715aa97e578639b5fe8092f8752fa6e1f9182324846c70bf1a3480411ba787e652be8049a36a14294681f745c4c4c4f7"
private_key_der = bytes.fromhex(private_key_hex)
private_key_base64 = base64.b64encode(private_key_der).decode()
print(private_key_base64)

22
webpush.py

@ -1,20 +1,25 @@
from pywebpush import webpush, WebPushException
import json
import base64
# 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
# https://fcm.googleapis.com/fcm/send/eqNQV7MVPic:APA91bGrIMxqz3sQ4wboUkmZithJHMAdrNgjm6BYcIGmgJozgEGeg23JsXLlNpnKwzBCmUXh1ciHmE_3wZakHX-Rho5f9Xovc28nun4nH7w4BMoYzX27pOw_pC4FtfAkBQaQ-8jm36jf
# BDo2fIIN7qoA5bOVXdrHATZUSPHY7030V8PKW1mIHAZHDAxS-p6RggVeI7IZoi3bGxpR713RYY8H8vu-lX5LY1w
# sVR_s8J4JHv3h4ZmvemL5w
# MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgrIaKlYimnslibbhXyq5CsNZUKIq/c7DY8aa0P/8JNQihRANCAARm2kU8Hnk9fiGrY7M06A+WcVqpfleGObX+gJL4dS+m4fkYIySEbHC/GjSAQRunh+ZSvoBJo2oUKUaB90XExMT3
private_key_hex = "308187020100301306072a8648ce3d020106082a8648ce3d030107046d306b0201010420ac868a9588a69ec9626db857caae42b0d654288abf73b0d8f1a6b43fff093508a1440342000466da453c1e793d7e21ab63b334e80f96715aa97e578639b5fe8092f8752fa6e1f9182324846c70bf1a3480411ba787e652be8049a36a14294681f745c4c4c4f7"
private_key_der = bytes.fromhex(private_key_hex)
private_key_base64 = base64.b64encode(private_key_der).decode()
subscription_info = {
"endpoint": "https://fcm.googleapis.com/fcm/send/e5jo1pnUZPA:APA91bEwHV2DeLybHc23G_-zJ4FXIdK_fh16ANVOrBekDeKNdmvZ0ukkPXsYGvq629KR2_ZK2w6rQHwC6hkbZ6Xqg0HktarvFT7wTQPjq7bkLgqqjOoONYZDPriDd9DvkD2vamzrPoBx",
"endpoint": "https://fcm.googleapis.com/fcm/send/eqNQV7MVPic:APA91bGrIMxqz3sQ4wboUkmZithJHMAdrNgjm6BYcIGmgJozgEGeg23JsXLlNpnKwzBCmUXh1ciHmE_3wZakHX-Rho5f9Xovc28nun4nH7w4BMoYzX27pOw_pC4FtfAkBQaQ-8jm36jf",
"keys": {
"p256dh": "BCj38CJs7M9rXDnZLBVU-XHYrAIdqCuTdscC_Waj96Z2KcMuTHI46kKssjmNwdkIae3Plr__UXbisdC5YPsIeFA",
"auth": "8cyYFZDqjGiZ4nBMfa2L8Q"
"p256dh": "BDo2fIIN7qoA5bOVXdrHATZUSPHY7030V8PKW1mIHAZHDAxS-p6RggVeI7IZoi3bGxpR713RYY8H8vu-lX5LY1w",
"auth": "sVR_s8J4JHv3h4ZmvemL5w"
}
}
@ -24,7 +29,8 @@ try:
webpush(subscription_info,
data,
# vapid_private_key="./private_key.pem",
vapid_private_key="MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgrIaKlYimnslibbhXyq5CsNZUKIq/c7DY8aa0P/8JNQihRANCAARm2kU8Hnk9fiGrY7M06A+WcVqpfleGObX+gJL4dS+m4fkYIySEbHC/GjSAQRunh+ZSvoBJo2oUKUaB90XExMT3",
# vapid_private_key="MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgrIaKlYimnslibbhXyq5CsNZUKIq/c7DY8aa0P/8JNQihRANCAARm2kU8Hnk9fiGrY7M06A+WcVqpfleGObX+gJL4dS+m4fkYIySEbHC/GjSAQRunh+ZSvoBJo2oUKUaB90XExMT3",
vapid_private_key=private_key_base64,
vapid_claims={"sub": "mailto:matthew.raymer@gmail.com"})
except WebPushException as ex:
print(f"An error occurred: {ex}")

Loading…
Cancel
Save