Last active
February 20, 2020 12:42
-
-
Save SuryaSankar/5a75506e924bbc052cd15a3022167f7b to your computer and use it in GitHub Desktop.
Basic webpush handler
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}), | |
| vapid_private_key=current_app.config["VAPID_PRIVATE_KEY"], | |
| vapid_claims={ | |
| "sub": "mailto:{}".format( | |
| current_app.config["VAPID_CLAIM_EMAIL"]) | |
| } | |
| ) | |
| return response.ok | |
| except WebPushException as ex: | |
| if ex.response and ex.response.json(): | |
| extra = ex.response.json() | |
| print("Remote service replied with a {}:{}, {}", | |
| extra.code, | |
| extra.errno, | |
| extra.message | |
| ) | |
| return False | |
| def trigger_push_notifications_for_subscriptions(subscriptions, title, body): | |
| return [trigger_push_notification(subscription, title, body) | |
| for subscription in subscriptions] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment