diff --git a/osinaweb/osichat/models.py b/osinaweb/osichat/models.py index 47a497ea..9b071f8a 100644 --- a/osinaweb/osichat/models.py +++ b/osinaweb/osichat/models.py @@ -11,60 +11,90 @@ from django.utils.safestring import mark_safe def send_notification(notification): - notification_data = { - 'title': notification.title, - 'body': mark_safe(notification.message), - } + try: + notification_data = { + 'title': notification.title, + 'body': mark_safe(notification.message), + } - if notification.type == 'Chat': + if notification.type == 'Chat': sound = 'outside_chat.wav' android_sound = 'outside_chat' - else: - sound = 'new_visitor.wav' - android_sound = 'new_visitor' - - android_config = AndroidConfig( - notification=AndroidNotification( - title=notification_data['title'], - body=notification_data['body'], - sound=android_sound + android_channel_id = 'chat' + else: + sound = 'new_visitor.wav' + android_sound = 'new_visitor' + android_channel_id = 'visitor' + + android_config = AndroidConfig( + notification=AndroidNotification( + title=notification_data['title'], + body=notification_data['body'], + sound=android_sound, + android_channel_id=android_channel_id ) - ) + ) - apns_config = APNSConfig( - payload=APNSPayload( - aps=Aps( - alert=ApsAlert( - title=notification_data['title'], - body=notification_data['body'], - ), - sound=sound, + apns_config = APNSConfig( + payload=APNSPayload( + aps=Aps( + alert=ApsAlert( + title=notification_data['title'], + body=notification_data['body'], + ), + sound=sound, + ) ) ) - ) - if notification.image: - FCMDevice.objects.send_message( - Message( - notification=NotificationFB( - title=notification_data['title'], - body=notification_data['body'], - image=notification.image, - ), - data={"image": notification.image}, - android=android_config, - apns=apns_config + message = Message( + notification=NotificationFB( + title=notification_data['title'], + body=notification_data['body'], + image=notification.image if notification.image else None, + ), + data={"id": notification.type_id, "type": notification.type}, + android=android_config, + apns=apns_config + ) + + + FCMDevice.objects.send_message(message) + + except Exception as e: + + error_message = f"Error sending notification: {str(e)}" + + + error_android_config = AndroidConfig( + notification=AndroidNotification( + title="Notification Error", + body=error_message, + sound="default", ) ) - else: + + error_apns_config = APNSConfig( + payload=APNSPayload( + aps=Aps( + alert=ApsAlert( + title="Notification Error", + body=error_message, + ), + sound="default", + ) + ) + ) + FCMDevice.objects.send_message( Message( notification=NotificationFB( - title=notification_data['title'], - body=notification_data['body'], + title="Notification Error", + body=error_message, ), - android=android_config, - apns=apns_config + data={"error": str(e)}, + android=error_android_config, + apns=error_apns_config ) )