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

Loading…
Cancel
Save