initial commit
This commit is contained in:
27
app/notifications.py
Normal file
27
app/notifications.py
Normal file
@@ -0,0 +1,27 @@
|
||||
import json
|
||||
from typing import Optional
|
||||
import requests
|
||||
|
||||
from .config import settings
|
||||
|
||||
|
||||
def send_push_notification(fcm_token: str, title: str, body: str, data: Optional[dict] = None) -> bool:
|
||||
if not settings.fcm_server_key:
|
||||
return False
|
||||
payload = {
|
||||
"to": fcm_token,
|
||||
"notification": {"title": title, "body": body},
|
||||
"data": data or {},
|
||||
"priority": "high",
|
||||
}
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": f"key={settings.fcm_server_key}",
|
||||
}
|
||||
try:
|
||||
resp = requests.post("https://fcm.googleapis.com/fcm/send", headers=headers, data=json.dumps(payload), timeout=5)
|
||||
return resp.status_code == 200
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
Reference in New Issue
Block a user