-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
97 lines (82 loc) · 2.85 KB
/
main.py
File metadata and controls
97 lines (82 loc) · 2.85 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
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
# Konfiguration
auth_file = "path/to/serviceAccountKey.json"
key_file = "path/to/key.pem"
cert_file = "path/to/cert.pem"
port = 8000
# Bibliotheken
import json
from http.server import HTTPServer, BaseHTTPRequestHandler
import firebase_admin
from firebase_admin import credentials, messaging
import ssl
def send_notification(id, notification_title, notification_body, notification_image=''):
message = messaging.Message(
notification=messaging.Notification(
title=notification_title,
body=notification_body,
image=notification_image,
),
token=id,
)
response = messaging.send(message)
return response
def send(self):
content_length = int(self.headers['Content-Length'])
body = self.rfile.read(content_length)
data = json.loads(body.decode("utf-8"))
token = data["token"]
title = data["title"]
body = data["body"]
if "img_url" in data:
image = data["img_url"]
else:
image = ""
message_id = send_notification(token, title, body, image)
self.send_response(200)
self.end_headers()
self.wfile.write(b'{"id":"')
self.wfile.write(bytes(message_id, 'utf-8'))
self.wfile.write(b'"}')
def register(self):
content_length = int(self.headers['Content-Length'])
body = self.rfile.read(content_length)
data = json.loads(body.decode("utf-8"))
with open('clients.txt') as json_file:
clients = json.load(json_file)
clients[data["client_name"]] = {
'token': data["token"]
}
with open('clients.txt', 'w') as outfile:
json.dump(clients, outfile)
self.send_response(200)
self.end_headers()
self.wfile.write(b'saved')
if __name__ == '__main__':
cred = credentials.Certificate(auth_file)
firebase_admin.initialize_app(cred)
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == "/clients":
self.send_response(200)
self.end_headers()
with open('clients.txt') as json_file:
clients = json.load(json_file)
self.wfile.write(bytes(json.dumps(clients), 'utf-8'))
else:
self.send_response(405)
self.end_headers()
self.wfile.write(b'not allowed')
def do_POST(self):
if self.path == "/send":
send(self)
elif self.path == "/register":
register(self)
else:
self.send_response(405)
self.end_headers()
httpd = HTTPServer(('localhost', port), SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket,
keyfile=key_file,
certfile=cert_file,
server_side=True)
httpd.serve_forever()