emile 9 months ago
parent fa20a8ac89
commit 855584aca5

Binary file not shown.

@ -0,0 +1,18 @@
# Generated by Django 4.2.5 on 2024-08-17 11:51
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('osichat', '0026_visitor_region'),
]
operations = [
migrations.AddField(
model_name='chatnotification',
name='type',
field=models.CharField(choices=[('Visitor', 'Visitor'), ('Chat', 'Chat')], max_length=8, null=True),
),
]

@ -9,57 +9,74 @@ from firebase_admin.messaging import Message,AndroidConfig,APNSConfig,APNSPayloa
from django.utils.safestring import mark_safe
def send_notification(notification):
notification_data = {
'title': notification.title,
'body': mark_safe(notification.message),
'body': mark_safe(notification.message),
}
sound = 'default'
if notification.type == 'Visitor':
sound = 'https://osina.ositcom.com/static/notifications/new-visitor.mp3'
android_config = AndroidConfig(
notification=AndroidNotification(
title=notification_data['title'],
body=notification_data['body'],
sound='default'
)
sound=sound
)
)
apns_config = APNSConfig(
payload= APNSPayload(
payload=APNSPayload(
aps=Aps(
alert=ApsAlert(
title=notification_data['title'],
body=notification_data['body'],
),
sound='default',
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(
notification=NotificationFB(
title=notification_data['title'],
body=notification_data['body'],
image=notification.image,
),
data={"image": notification.image},
android=android_config,
apns=apns_config
)
)
else:
FCMDevice.objects.send_message(
Message(notification=NotificationFB(
title=notification_data['title'],
body=notification_data['body'],
),
android=android_config,
apns=apns_config)
Message(
notification=NotificationFB(
title=notification_data['title'],
body=notification_data['body'],
),
android=android_config,
apns=apns_config
)
)
# Create your models here.
class ChatNotification(models.Model):
TYPES = (
('Visitor', 'Visitor'),
('Chat', 'Chat')
)
title = models.CharField(max_length=255)
message = models.TextField()
image = models.TextField(blank=True,null=True)
created_at = models.DateTimeField(auto_now_add=True)
type = models.CharField(max_length=8, choices=TYPES, null=True)
def save(self, *args, **kwargs):
is_new = not self.pk
super().save(*args, **kwargs)
@ -119,9 +136,18 @@ class Visitor(models.Model):
self.send_visitor_notification()
def send_visitor_notification(self):
first_log = self.visitorlog_set.order_by('visit_date').first()
body = ""
if first_log:
if first_log.title:
body = f"New visitor navigated to {first_log.title}"
elif first_log.url:
body = f"New visitor navigated to {first_log.url}"
notification = ChatNotification.objects.create(
title="New visitor on Ositcom!",
image = self.notification_flag_image_url
body = body,
image = self.notification_flag_image_url,
type = "Visitor"
)
@ -148,7 +174,6 @@ class VisitorLog(models.Model):
return f"{int(minutes):02}:{int(seconds):02}"
def save(self, *args, **kwargs):
is_new = not self.pk
if self.left_date:
action = 'end_log'
else:
@ -161,14 +186,9 @@ class VisitorLog(models.Model):
'action': action
}
async_to_sync(channel_layer.group_send)("osichat", event)
if is_new:
self.send_visitorlog_notification()
def send_visitorlog_notification(self):
notification = ChatNotification.objects.create(
title=f"Visitor navigated to: {self.title}.",
image=self.visitor.notification_flag_image_url
)
class ChatRoom(models.Model):

Loading…
Cancel
Save