Commit ddbdf64222b85c50d809bb69b930a6cadd3cbac2
1 parent
c8cbe6ae
Exists in
master
and in
2 other branches
Making app receive instant chat messages
Showing
1 changed file
with
38 additions
and
2 deletions
Show diff stats
chat/views.py
... | ... | @@ -25,12 +25,14 @@ from log.mixins import LogMixin |
25 | 25 | import time |
26 | 26 | |
27 | 27 | from fcm_django.models import FCMDevice |
28 | +from fcm_django.fcm import fcm_send_message | |
28 | 29 | |
29 | 30 | from categories.models import Category |
30 | 31 | from subjects.models import Subject |
31 | 32 | from users.models import User |
32 | 33 | |
33 | 34 | from .models import Conversation, TalkMessages, ChatVisualizations, ChatFavorites |
35 | +from .serializers import ChatSerializer | |
34 | 36 | from .forms import ChatMessageForm |
35 | 37 | |
36 | 38 | class GeneralIndex(LoginRequiredMixin, LogMixin, generic.ListView): |
... | ... | @@ -371,10 +373,44 @@ class SendMessage(LoginRequiredMixin, LogMixin, generic.edit.CreateView): |
371 | 373 | |
372 | 374 | Group("user-%s" % user.id).send({'text': notification}) |
373 | 375 | |
374 | - device = FCMDevice.objects.filter(user = user).first() | |
376 | + device = FCMDevice.objects.filter(user = user, active = True).first() | |
375 | 377 | |
376 | 378 | if not device is None: |
377 | - device.send_message(title = "Message", body = self.object.text) | |
379 | + data = { | |
380 | + "user_icon": self.object.user.image_url, | |
381 | + "notify_title": str(self.object.user), | |
382 | + "text_notify": simple_notify, | |
383 | + "image_url": "", | |
384 | + "message": strip_tags(self.object.text), | |
385 | + "datetime": str(self.object.create_date) | |
386 | + } | |
387 | + | |
388 | + if self.object.image: | |
389 | + data['image_url'] = self.object.image.url | |
390 | + | |
391 | + data = ChatSerializer(self.object) | |
392 | + | |
393 | + json_r = json.dumps(data.data) | |
394 | + json_r = json.loads(json_r) | |
395 | + | |
396 | + info = {} | |
397 | + | |
398 | + info["data"] = {} | |
399 | + info["data"]["messages"] = [] | |
400 | + info["data"]["message_sent"] = json_r | |
401 | + | |
402 | + info["message"] = "" | |
403 | + info["type"] = "" | |
404 | + info["title"] = "" | |
405 | + info["success"] = True | |
406 | + info["number"] = 1 | |
407 | + info['extra'] = 0 | |
408 | + | |
409 | + response = json.dumps(info) | |
410 | + | |
411 | + title = str(self.object.user) | |
412 | + | |
413 | + device.send_message(title = title, body = simple_notify, data = {"response": response}) | |
378 | 414 | |
379 | 415 | ChatVisualizations.objects.create(viewed = False, message = self.object, user = user) |
380 | 416 | ... | ... |