From e46d649fdcd96a6c3766b0e00d9677679bfa4434 Mon Sep 17 00:00:00 2001 From: abutalib-kiwi Date: Mon, 14 Aug 2023 16:03:36 +0530 Subject: [PATCH] notification modification added more payload --- account/utils.py | 7 +++++ notifications/constants.py | 2 +- notifications/serializers.py | 3 ++- notifications/utils.py | 50 +++++++++++++++++++++++++++--------- notifications/views.py | 3 ++- 5 files changed, 50 insertions(+), 15 deletions(-) diff --git a/account/utils.py b/account/utils.py index fd2f40f..dcfb4a2 100644 --- a/account/utils.py +++ b/account/utils.py @@ -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 diff --git a/notifications/constants.py b/notifications/constants.py index b861142..46bfbef 100644 --- a/notifications/constants.py +++ b/notifications/constants.py @@ -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}." } } diff --git a/notifications/serializers.py b/notifications/serializers.py index a061369..14f1b20 100644 --- a/notifications/serializers.py +++ b/notifications/serializers.py @@ -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""" diff --git a/notifications/utils.py b/notifications/utils.py index ba980e6..38df5ba 100644 --- a/notifications/utils.py +++ b/notifications/utils.py @@ -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) ) diff --git a/notifications/views.py b/notifications/views.py index c66d655..f932f0f 100644 --- a/notifications/views.py +++ b/notifications/views.py @@ -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',