File size: 1,277 Bytes
f7edd7e
 
 
 
7024c72
 
f7edd7e
 
7024c72
 
 
 
 
 
 
 
f7edd7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from fastapi import FastAPI
from pydantic import BaseModel
import requests

from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)


EXPO_TOKENS = set()

class RegisterRequest(BaseModel):
    expo_token: str

class IncidentRequest(BaseModel):
    incident_id: str = 1
    title: str = "Dummy Title"
    message: str = "message"

dummyData = IncidentRequest()

@app.post("/register")
def register_device(data: RegisterRequest):
    EXPO_TOKENS.add(data.expo_token)
    print(data.expo_token)
    return {"status": "registered"}

@app.get("/notify-incident")
def notify_incident():
    data=dummyData
    responses = []

    for token in EXPO_TOKENS:
        payload = {
            "to": token,
            "sound": "default",
            "title": data.title,
            "body": data.message,
            "priority": "high",
            "data": {
                "url": f"myapp://incidents/{data.incident_id}"
            },
        }

        r = requests.post(
            "https://exp.host/--/api/v2/push/send",
            json=payload,
            timeout=5,
        )
        responses.append(r.json())

    return {"sent": len(responses)}