emile 10 months ago
parent ded8313420
commit b4acae2bf3

@ -11,6 +11,7 @@ from django.utils.safestring import mark_safe
def send_notification(notification):
try:
notification_data = {
'title': notification.title,
'body': mark_safe(notification.message),
@ -19,15 +20,18 @@ def send_notification(notification):
if notification.type == 'Chat':
sound = 'outside_chat.wav'
android_sound = 'outside_chat'
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
sound=android_sound,
android_channel_id=android_channel_id
)
)
@ -43,28 +47,54 @@ def send_notification(notification):
)
)
if notification.image:
FCMDevice.objects.send_message(
Message(
message = Message(
notification=NotificationFB(
title=notification_data['title'],
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,
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
)
)

Loading…
Cancel
Save