28 lines
748 B
Python
28 lines
748 B
Python
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
|
|
|
|
|