Create sendnotifications.py
Browse files- routes/sendnotifications.py +54 -0
routes/sendnotifications.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter
|
| 2 |
+
import httpx
|
| 3 |
+
import json
|
| 4 |
+
from google.oauth2 import service_account
|
| 5 |
+
from google.auth.transport.requests import Request
|
| 6 |
+
|
| 7 |
+
router = APIRouter()
|
| 8 |
+
|
| 9 |
+
# Caminho para o arquivo da conta de serviço
|
| 10 |
+
SERVICE_ACCOUNT_FILE = './closetcoach-461319-35bd15d6c289.json'
|
| 11 |
+
|
| 12 |
+
# Token de teste (fixo para agora)
|
| 13 |
+
TEST_DEVICE_TOKEN = 'cnL5SYv0RF-VHoVo8JoAvv:APA91bGeOzUhAicTfoboPXI1immeb4ueR_vBL5Pf1Kky5cltKfvw76EBnh7VDhviyvRn4qhV5IjfZieQKJXe-_kYxi0ZJOJJBrfXdeIwVsLQ-0bi2ElNwds'
|
| 14 |
+
|
| 15 |
+
def get_access_token():
|
| 16 |
+
credentials = service_account.Credentials.from_service_account_file(
|
| 17 |
+
SERVICE_ACCOUNT_FILE
|
| 18 |
+
)
|
| 19 |
+
scoped_credentials = credentials.with_scopes(['https://www.googleapis.com/auth/cloud-platform'])
|
| 20 |
+
scoped_credentials.refresh(Request())
|
| 21 |
+
return credentials.token
|
| 22 |
+
|
| 23 |
+
@router.get("/send-test-notification")
|
| 24 |
+
async def send_test_notification():
|
| 25 |
+
try:
|
| 26 |
+
access_token = get_access_token()
|
| 27 |
+
|
| 28 |
+
headers = {
|
| 29 |
+
'Authorization': f'Bearer {access_token}',
|
| 30 |
+
'Content-Type': 'application/json',
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
payload = {
|
| 34 |
+
"message": {
|
| 35 |
+
"notification": {
|
| 36 |
+
"title": "Teste ClosetCoach",
|
| 37 |
+
"body": "Esta é uma notificação de teste 🚀",
|
| 38 |
+
},
|
| 39 |
+
"token": TEST_DEVICE_TOKEN
|
| 40 |
+
}
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
url = "https://fcm.googleapis.com/v1/projects/closetcoach-461319/messages:send"
|
| 44 |
+
|
| 45 |
+
async with httpx.AsyncClient() as client:
|
| 46 |
+
response = await client.post(url, headers=headers, json=payload)
|
| 47 |
+
|
| 48 |
+
return {
|
| 49 |
+
"status_code": response.status_code,
|
| 50 |
+
"response": response.json()
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return {"error": str(e)}
|