emile 10 months ago
parent ded8313420
commit b4acae2bf3

@ -11,6 +11,7 @@ from django.utils.safestring import mark_safe
def send_notification(notification): def send_notification(notification):
try:
notification_data = { notification_data = {
'title': notification.title, 'title': notification.title,
'body': mark_safe(notification.message), 'body': mark_safe(notification.message),
@ -19,15 +20,18 @@ def send_notification(notification):
if notification.type == 'Chat': if notification.type == 'Chat':
sound = 'outside_chat.wav' sound = 'outside_chat.wav'
android_sound = 'outside_chat' android_sound = 'outside_chat'
android_channel_id = 'chat'
else: else:
sound = 'new_visitor.wav' sound = 'new_visitor.wav'
android_sound = 'new_visitor' android_sound = 'new_visitor'
android_channel_id = 'visitor'
android_config = AndroidConfig( android_config = AndroidConfig(
notification=AndroidNotification( notification=AndroidNotification(
title=notification_data['title'], title=notification_data['title'],
body=notification_data['body'], body=notification_data['body'],
sound=android_sound sound=android_sound,
android_channel_id=android_channel_id
) )
) )
@ -43,28 +47,54 @@ def send_notification(notification):
) )
) )
if notification.image: message = Message(
FCMDevice.objects.send_message(
Message(
notification=NotificationFB( notification=NotificationFB(
title=notification_data['title'], title=notification_data['title'],
body=notification_data['body'], body=notification_data['body'],
image=notification.image, image=notification.image if notification.image else None,
), ),
data={"image": notification.image}, data={"id": notification.type_id, "type": notification.type},
android=android_config, android=android_config,
apns=apns_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( FCMDevice.objects.send_message(
Message( Message(
notification=NotificationFB( notification=NotificationFB(
title=notification_data['title'], title="Notification Error",
body=notification_data['body'], body=error_message,
), ),
android=android_config, data={"error": str(e)},
apns=apns_config android=error_android_config,
apns=error_apns_config
) )
) )

Loading…
Cancel
Save