|
@ -6,11 +6,13 @@ from pywebpush import webpush, WebPushException |
|
|
import json |
|
|
import json |
|
|
import os |
|
|
import os |
|
|
|
|
|
|
|
|
app = Flask(__name__) |
|
|
def create_app(config_name): |
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///webpush.db' |
|
|
app = Flask(__name__) |
|
|
db.init_app(app) |
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///webpush.db' |
|
|
|
|
|
db.init_app(app) |
|
|
|
|
|
|
|
|
def generate_and_save_vapid_keys(): |
|
|
|
|
|
|
|
|
def generate_and_save_vapid_keys(): |
|
|
vapid = Vapid() |
|
|
vapid = Vapid() |
|
|
vapid.generate_keys() |
|
|
vapid.generate_keys() |
|
|
private_key = vapid.get_private_key().to_pem().decode('utf-8').strip() |
|
|
private_key = vapid.get_private_key().to_pem().decode('utf-8').strip() |
|
@ -21,12 +23,12 @@ def generate_and_save_vapid_keys(): |
|
|
db.session.commit() |
|
|
db.session.commit() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.before_first_request |
|
|
def initialize(): |
|
|
def initialize(): |
|
|
|
|
|
if not VAPIDKey.query.first(): |
|
|
if not VAPIDKey.query.first(): |
|
|
generate_and_save_vapid_keys() |
|
|
generate_and_save_vapid_keys() |
|
|
|
|
|
|
|
|
def send_push_notification(subscription_info, message, vapid_key): |
|
|
|
|
|
|
|
|
def send_push_notification(subscription_info, message, vapid_key): |
|
|
try: |
|
|
try: |
|
|
webpush( |
|
|
webpush( |
|
|
subscription_info=subscription_info, |
|
|
subscription_info=subscription_info, |
|
@ -40,16 +42,16 @@ def send_push_notification(subscription_info, message, vapid_key): |
|
|
print(f"Failed to send notification: {e}") |
|
|
print(f"Failed to send notification: {e}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/web-push/vapid', methods=['GET']) |
|
|
@app.route('/web-push/vapid', methods=['GET']) |
|
|
def get_vapid(): |
|
|
def get_vapid(): |
|
|
key = VAPIDKey.query.first() |
|
|
key = VAPIDKey.query.first() |
|
|
if key: |
|
|
if key: |
|
|
return jsonify(public_key=key.public_key) |
|
|
return jsonify(public_key=key.public_key) |
|
|
return jsonify(error='No VAPID keys found'), 404 |
|
|
return jsonify(error='No VAPID keys found'), 404 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/web-push/subscribe', methods=['POST']) |
|
|
@app.route('/web-push/subscribe', methods=['POST']) |
|
|
def subscribe(): |
|
|
def subscribe(): |
|
|
content = request.json |
|
|
content = request.json |
|
|
vapid_key = VAPIDKey.query.first() |
|
|
vapid_key = VAPIDKey.query.first() |
|
|
|
|
|
|
|
@ -77,8 +79,8 @@ def subscribe(): |
|
|
return jsonify(success=True) |
|
|
return jsonify(success=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/web-push/unsubscribe', methods=['DELETE']) |
|
|
@app.route('/web-push/unsubscribe', methods=['DELETE']) |
|
|
def unsubscribe(): |
|
|
def unsubscribe(): |
|
|
content = request.json |
|
|
content = request.json |
|
|
endpoint = content['endpoint'] |
|
|
endpoint = content['endpoint'] |
|
|
subscription = Subscription.query.filter_by(endpoint=endpoint).first() |
|
|
subscription = Subscription.query.filter_by(endpoint=endpoint).first() |
|
@ -90,6 +92,4 @@ def unsubscribe(): |
|
|
else: |
|
|
else: |
|
|
return jsonify(success=False, error="Subscription not found"), 404 |
|
|
return jsonify(success=False, error="Subscription not found"), 404 |
|
|
|
|
|
|
|
|
|
|
|
return app |
|
|
if __name__ == '__main__': |
|
|
|
|
|
app.run(debug=True) |
|
|
|
|
|