-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from admtlab/develop
Updates notifications
- Loading branch information
Showing
6 changed files
with
87 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
""" | ||
Handle user notifications | ||
Author: Mark Silvis | ||
""" | ||
from exponent_server_sdk import ( | ||
PushClient, PushMessage, PushServerError, PushResponseError | ||
) | ||
from tornado.escape import json_decode | ||
|
||
from db import User | ||
from handlers.base import BaseHandler, CORSHandler, SecureHandler | ||
from notifier import send_push_notification | ||
|
||
|
||
class NotificationHandler(SecureHandler): | ||
def post(self, path: str): | ||
user_id = self.get_user_id() | ||
user = User.get_by_id(user_id) | ||
if user.email not in ('[email protected]', '[email protected]'): | ||
self.write_error(403, 'Insufficient permissions') | ||
else: | ||
# get json body | ||
data = json_decode(self.request.body) | ||
# message field is required | ||
if not all(key in data for key in ('title', 'body')): | ||
self.write_error(400, f'Missing field(s): {", ".join({"title", "body"}-data.keys())}') | ||
else: | ||
# send message to all users | ||
users = User.get_all() | ||
for user in users: | ||
if user.expo_token: | ||
print(f"sending notification to user: {user.id}") | ||
print(f"notification values\ntitle:{data['title']}\nbody:{data['body']}\ndata:{data.get('data')}") | ||
notification_data = data.get('data') or dict() | ||
notification_data['title'] = data['title'] | ||
notification_data['body'] = data['body'] | ||
notification_data['type'] = 'message' | ||
send_push_notification(user.expo_token, | ||
data['title'], data['body'], | ||
notification_data) |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
""" | ||
Send notifications to users | ||
Author: Mark Silvis | ||
""" | ||
|
||
from typing import Any, Dict, Tuple, Union | ||
|
||
from exponent_server_sdk import ( | ||
PushClient, PushMessage, PushServerError, PushResponseError, | ||
) | ||
|
||
|
||
class InvalidExpoToken(Exception): | ||
def __init__(self): | ||
self.message = "Invalid expo token" | ||
super(ValidationError, self).__init__(self.message) | ||
|
||
|
||
def send_push_notification(expo_token: str, | ||
title: str, | ||
body: str, | ||
data: Dict[Any, Any]=None): | ||
"""Send notification to specified expo token | ||
:expo_token: token to send notificaton to | ||
:title: notification title | ||
:body: notification body | ||
:data: extra notification data (total payload must be under 4096 bytes) | ||
:raises: ConnectionError, DeviceNotRegisterdError, HTTPError, | ||
InvalidExpoToken, PushServerError, PushResponseError | ||
""" | ||
assert expo_token, "Expo token cannot be None" | ||
if PushClient().is_exponent_push_token(expo_token): | ||
message = PushMessage(to=expo_token, title=title, body=body, data=data) | ||
response = PushClient().publish(message) | ||
response.validate_response() | ||
else: | ||
raise InvalidExpoToken() |