|
@ -6,10 +6,12 @@ from pywebpush import webpush, WebPushException |
|
|
import json |
|
|
import json |
|
|
import os |
|
|
import os |
|
|
|
|
|
|
|
|
|
|
|
def create_app(config_name): |
|
|
app = Flask(__name__) |
|
|
app = Flask(__name__) |
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///webpush.db' |
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///webpush.db' |
|
|
db.init_app(app) |
|
|
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() |
|
@ -21,11 +23,11 @@ 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( |
|
@ -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) |
|
|
|
|
|