Код: Выделить всё
import os
import firebase_admin
from firebase_admin import credentials, messaging
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from dotenv import load_dotenv
load_dotenv()
cred = credentials.Certificate(os.getenv("GOOGLE_APPLICATION_CREDENTIALS"))
firebase_admin.initialize_app(cred)
app = FastAPI()
class PushNotificationModel(BaseModel):
title: str
body: str
token: str
def send_push_notification(title: str, body: str, token: str) -> str:
try:
message = messaging.Message(
notification=messaging.Notification(title=title, body=body), topic=token
)
response = messaging.send(message)
return response
except Exception as e:
raise HTTPException(
status_code=500, detail=f"Error sending push notification: {str(e)}"
)
@app.post("/send-push/")
async def send_push(notification: PushNotificationModel):
response = send_push_notification(
title=notification.title, body=notification.body, token=notification.token
)
return {"message": "Push notification sent", "response": response}
Код: Выделить всё
import requests
def send_test_push():
url = "http://localhost:8000/send-push/"
data = {
"title": "Test title",
"body": "Test body",
"token": "bla-bla-bla",
}
response = requests.post(url, json=data)
print(response.json())
if __name__ == "__main__":
send_test_push()

Подробнее здесь: https://stackoverflow.com/questions/791 ... sdk-python
Мобильная версия