Commit 6937f6790c158190e7d3cad3d0b45675b167f79a
1 parent
4c93f8fa
Exists in
master
and in
2 other branches
Start sending 'beta' notifications from chat to app
Showing
2 changed files
with
40 additions
and
0 deletions
Show diff stats
api/views.py
... | ... | @@ -33,6 +33,8 @@ from oauth2_provider.views.generic import ProtectedResourceView |
33 | 33 | from oauth2_provider.models import Application |
34 | 34 | from django.http import HttpResponse |
35 | 35 | |
36 | +from fcm_django.models import FCMDevice | |
37 | + | |
36 | 38 | @csrf_exempt |
37 | 39 | def getToken(request): |
38 | 40 | oauth = Application.objects.filter(name = "amadeus-droid") |
... | ... | @@ -111,6 +113,37 @@ class LoginViewset(viewsets.ReadOnlyModelViewSet): |
111 | 113 | |
112 | 114 | return HttpResponse(response) |
113 | 115 | |
116 | + @csrf_exempt | |
117 | + @list_route(methods = ['POST'], permissions_classes = [IsAuthenticated]) | |
118 | + def register_device(self, request): | |
119 | + username = request.data['email'] | |
120 | + device = request.data['device'] | |
121 | + | |
122 | + user = self.queryset.get(email = username) | |
123 | + response = "" | |
124 | + json_r = {} | |
125 | + | |
126 | + if not user is None: | |
127 | + fcm_d = FCMDevice() | |
128 | + fcm_d.name = "phone" | |
129 | + fcm_d.registration_id = device | |
130 | + fcm_d.type = 'android' | |
131 | + fcm_d.user = user | |
132 | + | |
133 | + fcm_d.save() | |
134 | + | |
135 | + if not fcm_d.pk is None: | |
136 | + json_r["message"] = "" | |
137 | + json_r["type"] = "" | |
138 | + json_r["title"] = "" | |
139 | + json_r["success"] = True | |
140 | + json_r["number"] = 1 | |
141 | + json_r['extra'] = 0 | |
142 | + | |
143 | + response = json.dumps(json_r) | |
144 | + | |
145 | + return HttpResponse(response) | |
146 | + | |
114 | 147 | class SubjectViewset(viewsets.ReadOnlyModelViewSet): |
115 | 148 | queryset = Subject.objects.all() |
116 | 149 | permissions_classes = (IsAuthenticated, ) | ... | ... |
chat/views.py
... | ... | @@ -24,6 +24,8 @@ from log.models import Log |
24 | 24 | from log.mixins import LogMixin |
25 | 25 | import time |
26 | 26 | |
27 | +from fcm_django.models import FCMDevice | |
28 | + | |
27 | 29 | from categories.models import Category |
28 | 30 | from subjects.models import Subject |
29 | 31 | from users.models import User |
... | ... | @@ -369,6 +371,11 @@ class SendMessage(LoginRequiredMixin, LogMixin, generic.edit.CreateView): |
369 | 371 | |
370 | 372 | Group("user-%s" % user.id).send({'text': notification}) |
371 | 373 | |
374 | + device = FCMDevice.objects.filter(user = user).first() | |
375 | + | |
376 | + if not device is None: | |
377 | + device.send_message(title = "Message", body = self.object.text) | |
378 | + | |
372 | 379 | ChatVisualizations.objects.create(viewed = False, message = self.object, user = user) |
373 | 380 | |
374 | 381 | return super(SendMessage, self).form_valid(form) | ... | ... |