-
Notifications
You must be signed in to change notification settings - Fork 2
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 #145 from Bestagons/21_Push_notification
21 push notification
- Loading branch information
Showing
13 changed files
with
290 additions
and
37 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,83 @@ | ||
from fastapi import APIRouter, Response, status, Depends, Security | ||
from ..auth_bearer import JWTBearer | ||
from fastapi.security import HTTPAuthorizationCredentials | ||
from database import db | ||
from models.user import User | ||
from app.auth_handler import decodeJWT | ||
|
||
router = APIRouter(prefix="/notification") | ||
|
||
security = JWTBearer() | ||
|
||
""" | ||
add_device binds a device to a user | ||
resp: Response | ||
The response to send back to the user which contains the status code and body | ||
device_code: str | ||
The UUID of the friend to be added | ||
returns Response | ||
Response.body: dict | ||
Provides any msgs/errs for the request | ||
Response.status: int | ||
The status code for the request | ||
""" | ||
@router.post("/add_device", dependencies=[Depends(security)]) | ||
async def add_device(resp: Response, device_code: str, token: HTTPAuthorizationCredentials = Security(security)): | ||
payload: dict = decodeJWT(token) | ||
user_id = payload["user_id"] | ||
user: User = User().get_user_by_id(user_id) | ||
|
||
# Add the device to the user | ||
devices = user.devices | ||
if devices is None: | ||
devices = [device_code] | ||
elif device_code in devices: # Allows multiple add. | ||
resp.body = {"msg": "Device already added"} | ||
return resp | ||
else: | ||
devices.append(device_code) | ||
# Update the user | ||
db.db["users"].update_one({"_id": user.id}, {"$set": {"devices": devices}}) | ||
|
||
resp.status = status.HTTP_200_OK | ||
resp.body = {"msg": "Device added successfully"} | ||
return resp | ||
|
||
|
||
""" | ||
remove_device removes a bind device from a user | ||
resp: Response | ||
The response to send back to the user which contains the status code and body | ||
device_code: str | ||
The UUID of the friend to be added | ||
returns Response | ||
Response.body: dict | ||
Provides any msgs/errs for the request | ||
Response.status: int | ||
The status code for the request | ||
""" | ||
@router.post("/remove_device") | ||
async def remove_device(resp: Response, device_code: str, token: HTTPAuthorizationCredentials = Security(security)): | ||
payload: dict = decodeJWT(token) | ||
user_id = payload["user_id"] | ||
user: User = User().get_user_by_id(user_id) | ||
|
||
# Add the device to the user | ||
devices = user.devices | ||
if devices is None or device_code not in devices: | ||
resp.body = {"msg": "Device not found"} | ||
return resp | ||
else: | ||
devices.remove(device_code) | ||
# Update the user | ||
db.db["users"].update_one({"_id": user.id}, {"$set": {"devices": devices}}) | ||
|
||
resp.status = status.HTTP_200_OK | ||
resp.body = {"msg": "Device removed successfully"} | ||
return resp |
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
Empty file.
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,41 @@ | ||
from firebase_admin import messaging | ||
|
||
|
||
def notify_one(token: str, title: str, body: str) -> str: | ||
""" | ||
:param token: The registration token of the device to which the message should be sent | ||
:param title: Notification title | ||
:param body: Notification body | ||
:return: | ||
Returns: | ||
string: A message ID string that uniquely identifies the sent message. | ||
""" | ||
message = messaging.Message( | ||
notification=messaging.Notification( | ||
title=title, | ||
body=body | ||
), | ||
token=token | ||
) | ||
return messaging.send(message) | ||
|
||
|
||
def notify_multiple(tokens: list, title: str, body: str) -> str: | ||
""" | ||
:param tokens: The registration token of the device to which the message should be sent | ||
:param title: Notification title | ||
:param body: Notification body | ||
:return: | ||
Returns: | ||
string: A message ID string that uniquely identifies the sent message. | ||
""" | ||
message = messaging.MulticastMessage( | ||
tokens=tokens, | ||
notification=messaging.Notification( | ||
title=title, | ||
body=body | ||
) | ||
) | ||
return messaging.send(message) |
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
Empty file.
Oops, something went wrong.