This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| /* eslint-enable max-len */ | |
| self.addEventListener('install', function(event) { | |
| console.log('Service Worker installing.'); | |
| }); | |
| self.addEventListener('activate', function(event) { | |
| console.log('Service Worker activating.'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| function urlB64ToUint8Array(base64String) { | |
| const padding = '='.repeat((4 - base64String.length % 4) % 4); | |
| const base64 = (base64String + padding) | |
| .replace(/\-/g, '+') | |
| .replace(/_/g, '/'); | |
| const rawData = window.atob(base64); | |
| const outputArray = new Uint8Array(rawData.length); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "endpoint":"https://fcm.googleapis.com/fcm/send/c434328ouoiuoiu32432cMR1nib-T2eDKDu3cZg3Y9M8y_gQa2VLtKeBerwerwerewr32423432432ewrrewrewfcaezv6ctv84-wKqTOeDZFN423409832049832049iuaroieuroewBgg3-26BtZ", "expirationTime":null, | |
| "keys":{ | |
| "p256dh":"BEuDDnZ4324234ewrewrewr2432432ewrew7gzfEsBKbe3z26ycB8l2h-h_v9yLLAw82CH83IQiCbT9238409348305Hgs", | |
| "auth":"i7jSDPe_1Nm321rrerew12321-XSA" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from pywebpush import webpush, WebPushException | |
| import json | |
| from flask import current_app | |
| def trigger_push_notification(push_subscription, title, body): | |
| try: | |
| response = webpush( | |
| subscription_info=json.loads(push_subscription.subscription_json), | |
| data=json.dumps({"title": title, "body": body}), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from pywebpush import webpush, WebPushException | |
| import json | |
| from flask import current_app | |
| def trigger_push_notification(push_subscription, title, body): | |
| try: | |
| response = webpush( | |
| subscription_info=json.loads(push_subscription.subscription_json), | |
| data=json.dumps({"title": title, "body": body}), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head> | |
| <link rel="icon" href="data:,"> | |
| </head> | |
| <body> | |
| <h1>Webpush Demo</h1> | |
| <script | |
| type="text/javascript" | |
| src="/static/register_service_worker.js"> | |
| </script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @app.route("/api/push-subscriptions", methods=["POST"]) | |
| def create_push_subscription(): | |
| json_data = request.get_json() | |
| subscription = PushSubscription.query.filter_by( | |
| subscription_json=json_data['subscription_json'] | |
| ).first() | |
| if subscription is None: | |
| subscription = PushSubscription( | |
| subscription_json=json_data['subscription_json'] | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @app.route("/admin-api/trigger-push-notifications", methods=["POST"]) | |
| def trigger_push_notifications(): | |
| json_data = request.get_json() | |
| subscriptions = PushSubscription.query.all() | |
| results = trigger_push_notifications_for_subscriptions( | |
| subscriptions, | |
| json_data.get('title'), | |
| json_data.get('body') | |
| ) | |
| return jsonify({ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite://" | |
| db = SQLAlchemy(app) | |
| class PushSubscription(db.Model): | |
| id = db.Column(db.Integer, primary_key=True, unique=True) | |
| subscription_json = db.Column(db.Text, nullable=False) | |
| db.create_all() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head> | |
| <link rel="icon" href="data:,"> | |
| <script | |
| src="https://code.jquery.com/jquery-3.4.1.min.js" | |
| integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" | |
| crossorigin="anonymous"></script> | |
| </head> | |
| <body> | |
| <h1>Trigger a Push Notification</h1> |