notification: change send notification
This commit is contained in:
@@ -3,31 +3,51 @@ from firebase_admin import messaging
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
from core.apps.notifications.models import Notification
|
||||
|
||||
EXPO_API_URL = "https://exp.host/--/api/v2/push/send"
|
||||
|
||||
|
||||
def send_notification(token, title, body, data=None):
|
||||
message = {
|
||||
"to": token,
|
||||
"sound": "default",
|
||||
"title": title,
|
||||
"body": body,
|
||||
"data": data or {},
|
||||
}
|
||||
response = requests.post(
|
||||
"https://exp.host/--/api/v2/push/send",
|
||||
json=message,
|
||||
headers={"Content-Type": "application/json"},
|
||||
)
|
||||
return response.json()
|
||||
tokens = list(Notification.objects.exclude(token=token).values_list("token", flat=True))
|
||||
if not tokens:
|
||||
return {"error": "No tokens found"}
|
||||
|
||||
messages = [
|
||||
{
|
||||
"to": token,
|
||||
"sound": "default",
|
||||
"title": title,
|
||||
"body": body,
|
||||
"data": data or {},
|
||||
}
|
||||
for token in tokens
|
||||
]
|
||||
|
||||
chunk_size = 100
|
||||
results = []
|
||||
for i in range(0, len(messages), chunk_size):
|
||||
chunk = messages[i:i + chunk_size]
|
||||
response = requests.post(EXPO_API_URL, json=chunk, headers={"Content-Type": "application/json"})
|
||||
try:
|
||||
results.append(response.json())
|
||||
except Exception:
|
||||
results.append({"error": "Invalid JSON response"})
|
||||
return results
|
||||
|
||||
|
||||
def send_web_notification(token, title, body, data=None):
|
||||
tokens = list(Notification.objects.exclude(token=token).values_list('token', flat=True))
|
||||
if not tokens:
|
||||
return
|
||||
|
||||
message = messaging.MulticastMessage(
|
||||
notification=messaging.Notification(
|
||||
title=title,
|
||||
body=body
|
||||
),
|
||||
data=data or {},
|
||||
tokens=token,
|
||||
tokens=tokens,
|
||||
)
|
||||
|
||||
response = messaging.send_multicast(message)
|
||||
Reference in New Issue
Block a user