如何确保用户离线后登录时得到通知

时间:2019-06-16 07:20:03

标签: django-channels

我正在使用django-channels 2,为我的网站实施约会通知。

我能够在浏览器控制台中收到已连接到频道并在组中的登录用户的通知。

现在我面临的问题是,如何确保通知在用户脱机或在预订新约会后注销后再次在线时到达用户。

此外,我不知道应该在哪里进入AppointmentNotification模型。在信号器中还是在消费者中的appointment_notification中?

我在appointment_app中创建了一个信号。然后,我向该组广播,该组中的频道能够收到通知。

Channels组名是拥有该组的一个人的uid,其他用户在其组中(属于其部门)。与该人预约后,其部门中的所有人员都会收到通知。我照常这样做,到目前为止,它一直在起作用。

notification_app / signals.py

@receiver(post_save, sender=Appointment)
def announce_new_appointment(sender, instance, created, **kwargs):
    channel_layer = get_channel_layer()

    async_to_sync(channel_layer.group_send)(
    str(instance.user.uid), {"type": "appointment.notification",
                "event": "New appointment"
            }
    )
     print('in Signals')

notification_app / consumers.py

async def appointment_notification(self, event):
    print('COMMUNICATING')
    await self.send_json(event)
    print("New message {event} at {self.channel_name}")

notification_app / models.py

class AppointmentNotification(models.Model):
    receiver = models.ForeignKey(User, related_name='notification_receiver', on_delete=models.CASCADE) ##THIS GROUP which will receive the notification
    sender = models.ForeignKey(Appointment, related_name='notification_sender', on_delete=models.CASCADE)

    created = models.DateTimeField(auto_now_add=True)
    is_seen = models.BooleanField(_('Is seen?'), default=False)

    class Meta:
        verbose_name_plural = _('appointmentnotifications')
        ordering = ['-created']

0 个答案:

没有答案