notification modification added more payload

This commit is contained in:
abutalib-kiwi
2023-08-14 16:03:36 +05:30
parent 08dc9f8538
commit e46d649fdc
5 changed files with 50 additions and 15 deletions

View File

@ -234,3 +234,10 @@ def generate_code(value, user_id):
OTP_EXPIRY = timezone.now() + timezone.timedelta(days=1)
def get_user_full_name(user_obj):
"""
to get user's full name
"""
return f"{user_obj.first_name} {user_obj.last_name}" if user_obj.last_name else user_obj.first_name

View File

@ -67,6 +67,6 @@ NOTIFICATION_DICT = {
},
TEST_NOTIFICATION: {
"title": "Test Notification",
"body": "This notification is for testing purpose"
"body": "This notification is for testing purpose from {from_user}."
}
}

View File

@ -35,7 +35,8 @@ class NotificationListSerializer(serializers.ModelSerializer):
class Meta(object):
"""meta info"""
model = Notification
fields = ['id', 'data', 'is_read']
fields = ['id', 'data', 'is_read', 'created_at']
class ReadNotificationSerializer(serializers.ModelSerializer):
"""User task Serializer"""

View File

@ -10,6 +10,7 @@ from firebase_admin.messaging import Message, Notification as FirebaseNotificati
from django.contrib.auth import get_user_model
from account.models import UserNotification
from account.utils import get_user_full_name
from notifications.constants import NOTIFICATION_DICT
from notifications.models import Notification
@ -39,30 +40,55 @@ def remove_fcm_token(user_id: int, access_token: str, registration_id) -> None:
print(e)
def get_basic_detail(notification_type, from_user_id, to_user_id):
""" used to get the basic details """
notification_data = NOTIFICATION_DICT[notification_type]
def get_basic_detail(from_user_id, to_user_id):
"""
used to get the basic details
"""
from_user = User.objects.get(id=from_user_id) if from_user_id else None
to_user = User.objects.get(id=to_user_id)
return notification_data, from_user, to_user
return from_user, to_user
def get_notification_data(notification_type, from_user, to_user, extra_data):
"""
get notification and push data
:param notification_type: notification_type
:param from_user: from_user obj
:param to_user: to_user obj
:param extra_data: any extra data provided
:return: notification and push data
"""
push_data = NOTIFICATION_DICT[notification_type].copy()
notification_data = push_data.copy()
notification_data['to_user'] = get_user_full_name(to_user)
if from_user:
from_user_name = get_user_full_name(from_user)
push_data['body'] = push_data['body'].format(from_user=from_user_name)
notification_data['body'] = notification_data['body'].format(from_user=from_user_name)
notification_data['from_user'] = from_user_name
notification_data.update(extra_data)
return notification_data, push_data
@shared_task()
def send_notification(notification_type, from_user_id, to_user_id, extra_data):
""" used to send the push for the given notification type """
(notification_data, from_user, to_user) = get_basic_detail(notification_type, from_user_id, to_user_id)
"""
used to send the push for the given notification type
"""
(from_user, to_user) = get_basic_detail(from_user_id, to_user_id)
notification_data, push_data = get_notification_data(notification_type, from_user, to_user, extra_data)
user_notification_type = UserNotification.objects.filter(user=to_user).first()
data = notification_data
notification_data.update({'badge': Notification.objects.filter(notification_to=to_user, is_read=False).count()})
Notification.objects.create(notification_type=notification_type, notification_from=from_user,
notification_to=to_user, data=data)
notification_to=to_user, data=notification_data)
if user_notification_type.push_notification:
data.update({'badge': Notification.objects.filter(notification_to=to_user, is_read=False).count()})
send_push(to_user, data)
send_push(to_user, push_data)
def send_push(user, data):
""" used to send push notification to specific user """
notification_data = data.pop('data', None)
user.fcmdevice_set.filter(active=True).send_message(
Message(notification=FirebaseNotification(data['title'], data['body']), data=notification_data)
Message(notification=FirebaseNotification(data['title'], data['body']), data=data)
)

View File

@ -56,7 +56,8 @@ class NotificationViewSet(viewsets.GenericViewSet):
to send test notification
:return:
"""
send_notification.delay(TEST_NOTIFICATION, None, request.auth.payload['user_id'], {})
send_notification(TEST_NOTIFICATION, None, request.auth.payload['user_id'],
{'task_id': None})
return custom_response(SUCCESS_CODE["3000"])
@action(methods=['get'], detail=False, url_path='list', url_name='list',