|
@ -29,19 +29,22 @@ def create_app(config_name): |
|
|
) |
|
|
) |
|
|
public_key_base64 = base64.b64encode(public_key_bytes).decode() |
|
|
public_key_base64 = base64.b64encode(public_key_bytes).decode() |
|
|
|
|
|
|
|
|
private_key_hex = binascii.hexlify(private_key.private_bytes( |
|
|
private_key_bytes = private_key.private_bytes( |
|
|
encoding=serialization.Encoding.DER, |
|
|
encoding=serialization.Encoding.DER, |
|
|
format=serialization.PrivateFormat.PKCS8, |
|
|
format=serialization.PrivateFormat.PKCS8, |
|
|
encryption_algorithm=serialization.NoEncryption() |
|
|
encryption_algorithm=serialization.NoEncryption() |
|
|
)).decode() |
|
|
) |
|
|
|
|
|
|
|
|
except Exception as e: |
|
|
private_key_base64 = base64.b64encode(private_key_bytes).decode() |
|
|
print(f"Error generating VAPID keys: {e}") |
|
|
|
|
|
|
|
|
|
|
|
key = VAPIDKey(public_key=public_key_base64, private_key=private_key_hex) |
|
|
key = VAPIDKey(public_key=public_key_base64, private_key=private_key_base64) |
|
|
db.session.add(key) |
|
|
db.session.add(key) |
|
|
db.session.commit() |
|
|
db.session.commit() |
|
|
|
|
|
|
|
|
|
|
|
except Exception as e: |
|
|
|
|
|
print(f"Error generating VAPID keys: {e}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def initialize(): |
|
|
def initialize(): |
|
|
if not VAPIDKey.query.first(): |
|
|
if not VAPIDKey.query.first(): |
|
@ -51,17 +54,14 @@ def create_app(config_name): |
|
|
def send_push_notification(subscription_info, message, vapid_key): |
|
|
def send_push_notification(subscription_info, message, vapid_key): |
|
|
result = True |
|
|
result = True |
|
|
try: |
|
|
try: |
|
|
private_key_hex = vapid_key.private_key |
|
|
private_key_base64 = vapid_key.private_key |
|
|
private_key_der = bytes.fromhex(private_key_hex) |
|
|
|
|
|
|
|
|
|
|
|
private_key_base64 = base64.b64encode(private_key_der).decode() |
|
|
|
|
|
|
|
|
|
|
|
webpush( |
|
|
webpush( |
|
|
subscription_info=subscription_info, |
|
|
subscription_info=subscription_info, |
|
|
data=json.dumps(message), |
|
|
data=json.dumps(message), |
|
|
vapid_private_key=private_key_base64, |
|
|
vapid_private_key=private_key_base64, |
|
|
vapid_claims={ |
|
|
vapid_claims={ |
|
|
"sub": "mailto:matthew.raymer@gmail.com" |
|
|
"sub": "mailto:info@timesafari.org" |
|
|
} |
|
|
} |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|