|
|
|
@ -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),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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(
|
|
|
|
|
Message(
|
|
|
|
|
notification=NotificationFB(
|
|
|
|
|
title=notification_data['title'],
|
|
|
|
|
body=notification_data['body'],
|
|
|
|
|
image= notification.image,
|
|
|
|
|
), data={"image": notification.image},android=android_config,apns=apns_config)
|
|
|
|
|
image=notification.image,
|
|
|
|
|
),
|
|
|
|
|
data={"image": notification.image},
|
|
|
|
|
android=android_config,
|
|
|
|
|
apns=apns_config
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
FCMDevice.objects.send_message(
|
|
|
|
|
Message(notification=NotificationFB(
|
|
|
|
|
Message(
|
|
|
|
|
notification=NotificationFB(
|
|
|
|
|
title=notification_data['title'],
|
|
|
|
|
body=notification_data['body'],
|
|
|
|
|
),
|
|
|
|
|
android=android_config,
|
|
|
|
|
apns=apns_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):
|
|
|
|
|