-
Notifications
You must be signed in to change notification settings - Fork 0
/
Firebase.py
171 lines (146 loc) · 4.82 KB
/
Firebase.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from oauth2client.service_account import ServiceAccountCredentials
from werkzeug.utils import redirect
from model.User import User
import firebase_admin
from firebase_admin import credentials, db, messaging
pathService_acount_json = 'covidtracing-749e0-firebase-adminsdk-2ub07-05c9957b06.json'
cred = credentials.Certificate(
pathService_acount_json)
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://covidtracing-749e0.firebaseio.com'
})
ref = db
def get_token():
"""Retrieve a valid access token that can be used to authorize requests.
:return: Access token.
"""
credentials = ServiceAccountCredentials.from_json_keyfile_name(
pathService_acount_json)
access_token_info = credentials.get_access_token()
return access_token_info.access_token
def addGeneralNotiToFB(contentNoti,title):
print(ref.reference('notification').push().set({
'title':title,
'content':contentNoti
}))
def getUsers(ref):
objUsers = ref.reference('user').get()
listUsers = []
for userID in objUsers:
phone = objUsers[userID]['phone']
isExposed = objUsers[userID]['expose']
key = objUsers[userID]['key']
listUsers.append(User(userID, phone, key, isExposed))
return listUsers
def deleteUser(ID):
ref.reference('user').child(ID).delete()
def update(ID):
ref.reference('user').child(ID).update(
{
'expose': 'F0'
}
)
def pushNotification( historyLocation, keyApp,title):
# The topic name can be optionally prefixed with "/topics/".
topic = 'covidtracing'
if historyLocation == "":
historyLocation = "Test"
if keyApp == "":
keyApp = "Test Key"
if title=="":
title = "test title"
# See documentation on defining a message payload.
messages = [
# Gửi tin nhắn lịch trình đi lại tại đây, gửi kèm Key người bi F0
messaging.Message(
data={
'title':title,
'key': keyApp,
'historyLocation': historyLocation,
},
topic=topic,
),
# ...
# Gửi thêm tin nhắn lịch trình đi lại tại đây luôn
messaging.Message(
notification=messaging.Notification('Thông báo ca nhiễm F0', historyLocation),
topic=topic,
),
]
# The topic name can be optionally prefixed with "/topics/".
# See documentation on defining a message payload.
# message = messaging.Message(
# data={
# 'score': '850',
# 'time': '2:45',
# },
# topic=topic,
# )
# Send a message to the devices subscribed to the provided topic.
response = messaging.send_all(messages)
# Response is a message ID string.
print('Successfully sent message:', response)
def pushGeneralNotification(contentNoti,title):
topic = 'covidtracing'
if contentNoti=="":
contentNoti = "Content Test Noti"
if title=="":
title = "Test title"
messages = [
# Gửi tin nhắn lịch trình đi lại tại đây, gửi kèm Key người bi F0
messaging.Message(
data={
'generalNotification': contentNoti,
'title':title,
},
topic=topic,
),
# ...
# Gửi thêm tin nhắn lịch trình đi lại tại đây luôn
messaging.Message(
notification=messaging.Notification('Thông báo: '+title, contentNoti),
topic=topic,
),
]
response = messaging.send_all(messages)
# Response is a message ID string.
print('Successfully sent message:', response)
from flask import Flask, request, render_template, url_for
app = Flask(__name__)
@app.route('/generalnotification',methods=['POST'])
def handleNotification():
notification = ""
title = ""
title = request.form['titleNoti']
notification = request.form['historyLocation']
pushGeneralNotification(notification,title)
addGeneralNotiToFB(notification,title)
print(notification)
return redirect('/')
@app.route('/deleteuser',methods=['GET'])
def delete():
IDUser = request.args.get('idremove')
deleteUser(IDUser)
return redirect('/')
@app.route('/handle', methods=['GET','POST'])
def handleRequest():
IDUser = ""
historyLocation = ""
keyApp = ""
title = ""
historyLocation = request.form['historyLocation']
title = request.form['titleNoti']
print(historyLocation)
IDUser = request.args.get('id')
keyApp = request.args.get('key')
print(keyApp)
update(IDUser)
# print(get_token())
# do somthing
pushNotification(historyLocation, keyApp,title)
return redirect('/')
@app.route('/')
def homePage():
return render_template('index.html', users=getUsers(ref))
if __name__ == '__main__':
app.run()