notification: add firebase notification to web

This commit is contained in:
behruz-dev
2025-10-30 14:59:59 +05:00
parent b6b2875e0d
commit 9b4f343f77
10 changed files with 198 additions and 79 deletions

View File

@@ -0,0 +1,24 @@
# Generated by Django 5.2.4 on 2025-10-30 14:56
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('notifications', '0001_initial'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.AddField(
model_name='notification',
name='type',
field=models.CharField(choices=[('web', 'web'), ('mobile', 'mobile')], default='mobile', max_length=6),
),
migrations.AlterUniqueTogether(
name='notification',
unique_together={('type', 'user', 'token')},
),
]

View File

@@ -3,7 +3,15 @@ from django.db import models
from core.apps.shared.models import BaseModel
from core.apps.accounts.models import User
class Notification(BaseModel):
type = models.CharField(
choices=[('web', 'web'), ('mobile', 'mobile')],
max_length=6,
default='mobile'
)
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='notifications')
token = models.CharField(max_length=255, unique=True)
class Meta:
unique_together = ('type', 'user', 'token')

View File

@@ -7,5 +7,5 @@ class NotificationSerializer(serializers.ModelSerializer):
class Meta:
model = Notification
fields = [
'token'
'type', 'token'
]

View File

@@ -1,8 +1,11 @@
from core.apps.notifications.models import Notification
from core.apps.notifications.utils.send_notification import send_notification
from core.apps.notifications.utils.send_notification import send_notification, send_web_notification
def notify_user(user, title, body, data):
tokens = Notification.objects.filter(user=user)
for token in tokens:
send_notification(token.token, title, body, data)
if token.type == 'mobile':
send_notification(token.token, title, body, data)
if token.type == 'web':
send_web_notification(token.token, title, body, data)

View File

@@ -1,4 +1,8 @@
import requests
from firebase_admin import messaging
from django.conf import settings
def send_notification(token, title, body, data=None):
message = {
@@ -11,6 +15,19 @@ def send_notification(token, title, body, data=None):
response = requests.post(
"https://exp.host/--/api/v2/push/send",
json=message,
headers={"Content-Type": "application/json"}
headers={"Content-Type": "application/json"},
)
return response.json()
return response.json()
def send_web_notification(token, title, body, data=None):
message = messaging.MulticastMessage(
notification=messaging.Notification(
title=title,
body=body
),
data=data or {},
tokens=token,
)
response = messaging.send_multicast(message)

View File

@@ -16,7 +16,8 @@ class RegisterExpoPushToken(generics.GenericAPIView):
if serializer.is_valid():
Notification.objects.get_or_create(
user=request.user,
token=serializer.validated_data['token']
token=serializer.validated_data['token'],
type=serializer.validated_data.get('type'),
)
return Response({"message": "Token saqlandi"}, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)