views.py
2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from rest_framework.views import APIView
from django.http import HttpResponse
import requests
import json
import firebase_admin
from firebase_admin import credentials
from firebase_admin import messaging
from firebase_admin import datetime
from django.apps import apps
User = apps.get_model('user', 'User')
cred = credentials.Certificate('./detection/whatsup-ad0b7-firebase-adminsdk-6yhd1-2e4fcd728a.json')
default_app = firebase_admin.initialize_app(cred)
topic = 'detection'
"""
Notification View는 fire_broken 또는 unknown_person이라는 이벤트 발생 시, 스파크에서 호출하는 api를 가진 view입니다.
post함수를 이용하여 각 user의 알림용 device의 고유 토큰을 조회하여 알림을 보낼 수 있습니다.
"""
class NotificationView(APIView):
def post(self, request, *args, **kwargs):
if request.method == 'POST':
timestamp = request.POST.get('timestamp', False)
userId = request.POST.get('userId', False)
detectionType = request.POST.get('detectionType',False)
print("USER : ", userId)
print("timestamp : ", timestamp)
print("detectionType : ", detectionType)
user = User.objects.get(userId=userId)
bodyContent = ""
if detectionType == "fire_broken":
bodyContent = "불났어요 불났어요!! " + timestamp
elif detectionType == "unknown_person":
bodyContent = "침입자 발생!! " + timestamp
message = messaging.Message(
android=messaging.AndroidConfig(
ttl=datetime.timedelta(seconds=3600),
priority='normal',
notification=messaging.AndroidNotification(
title='삐뽀삐뽀',
body = bodyContent,
icon='',
color='#f45342',
sound='default'
),
),
data={
'timestamp': timestamp
},
webpush=messaging.WebpushConfig(
notification=messaging.WebpushNotification(
title='웹 알림',
body='여긴 어떨까',
icon='',
),
),
# topic=topic
token=user.userToken
)
response = messaging.send(message)
# Response is a message ID string.
print('Successfully sent message:', response)
return HttpResponse('notification_success')
return HttpResponse('/notification_failure')