soonmyeong2

make push alarm system by python file

1 from firebase_admin import messaging 1 from firebase_admin import messaging
2 from firebase_admin import credentials 2 from firebase_admin import credentials
3 +from firebase_admin import datetime
4 +from firebase_admin import db
3 import firebase_admin 5 import firebase_admin
6 +import json
7 +import re
8 +
4 9
5 cred = credentials.Certificate('wello_firebase_SDKKey.json') 10 cred = credentials.Certificate('wello_firebase_SDKKey.json')
6 -default_app = firebase_admin.initialize_app(cred) 11 +firebase_admin.initialize_app(cred, {'databaseURL': 'https://wello-topic.firebaseio.com/'})
12 +ref = db.reference()
13 +topics = list(ref.get())
14 +pre_topic = list()
7 15
8 -def send_to_topic():
9 - # [START send_to_topic]
10 - # The topic name can be optionally prefixed with "/topics/".
11 - topic = 'c1_1'
12 16
13 - # See documentation on defining a message payload. 17 +def send_to_topic(policy, topic):
14 message = messaging.Message( 18 message = messaging.Message(
15 - title = 'wef', 19 + android=messaging.AndroidConfig(
20 + ttl=datetime.timedelta(seconds=3600),
21 + priority='normal',
22 + notification=messaging.AndroidNotification(
23 + title='새 정책 알람',
24 + body=policy,
25 + icon='@drawable/mini',
26 + color='#29ABE2',
27 + sound='default'
28 + ),
29 + ),
16 data={ 30 data={
17 'score': '850', 31 'score': '850',
18 'time': '2:45', 32 'time': '2:45',
19 }, 33 },
34 + webpush=messaging.WebpushConfig(
35 + notification=messaging.WebpushNotification(
36 + title='웹 알림',
37 + body='TEST',
38 + icon='',
39 + ),
40 + ),
20 topic=topic, 41 topic=topic,
21 ) 42 )
22 -
23 - # Send a message to the devices subscribed to the provided topic.
24 response = messaging.send(message) 43 response = messaging.send(message)
25 - # Response is a message ID string.
26 -
27 print('Successfully sent message:', response) 44 print('Successfully sent message:', response)
28 45
29 -send_to_topic() 46 +
47 +for topic in topics:
48 + categorys = re.findall('c[0-9]+[_][0-9]+', topic)
49 + for i in range(len(categorys) - 1, -1, -1):
50 + if '1' == categorys[i].split('_')[-1]:
51 + categorys.pop(i)
52 + pre_topic.append(categorys)
53 +
54 +
55 +print('new policy category : ', end = '')
56 +policy = input()
57 +policy_category = re.findall('c[0-9]+[_][0-9]+', policy)
58 +policy_category_list = re.findall('c[0-9]+', policy)
59 +push_index = list()
60 +flag = True
61 +
62 +
63 +for i, categorys in enumerate(pre_topic):
64 + for category in categorys:
65 + if category.split('_')[0] in policy_category_list:
66 + if category not in policy_category:
67 + flag = False
68 + break
69 + if flag: push_index.append(i)
70 + flag = True
71 +
72 +
73 +for push in push_index:
74 + send_to_topic('[가정복지부] 기성이를 보유하신 가정에 지원금을 드립니다.', topics[push])
......