| 1 |
from . import NotificationError |
| 2 |
from .conf import get_manager |
| 3 |
|
| 4 |
from pywebpush import webpush |
| 5 |
from pywebpush import WebPushException |
| 6 |
|
| 7 |
|
| 8 |
class WebPushError(NotificationError): |
| 9 |
pass |
| 10 |
|
| 11 |
|
| 12 |
def get_subscription_info(application_id, uri, browser, auth, p256dh): |
| 13 |
url = get_manager().get_wp_post_url(application_id, browser) |
| 14 |
return { |
| 15 |
"endpoint": "%s/%s" % (url, uri), |
| 16 |
"keys": { |
| 17 |
"auth": auth, |
| 18 |
"p256dh": p256dh |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
def webpush_send_message(uri, message, browser, auth, p256dh, application_id=None, **kwargs): |
| 24 |
try: |
| 25 |
response = webpush( |
| 26 |
subscription_info=get_subscription_info(application_id, uri, browser, auth, p256dh), |
| 27 |
data=message, |
| 28 |
vapid_private_key=get_manager().get_wp_private_key(application_id), |
| 29 |
vapid_claims=get_manager().get_wp_claims(application_id), |
| 30 |
**kwargs) |
| 31 |
results = {"results": [{}]} |
| 32 |
if not response.ok: |
| 33 |
results["results"][0]['error'] = response.content |
| 34 |
results["results"][0]['original_registration_id'] = response.content |
| 35 |
else: |
| 36 |
results["success"] = 1 |
| 37 |
return results |
| 38 |
except WebPushException as e: |
| 39 |
raise WebPushError(e.message) |