push_messaging.py
2.06 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
from firebase_admin import messaging
from firebase_admin import credentials
from firebase_admin import datetime
from firebase_admin import db
import firebase_admin
import json
import re
cred = credentials.Certificate('wello_firebase_SDKKey.json')
firebase_admin.initialize_app(cred, {'databaseURL': 'https://wello-topic.firebaseio.com/'})
ref = db.reference()
topics = list(ref.get())
pre_topic = list()
def send_to_topic(policy, topic):
message = messaging.Message(
android=messaging.AndroidConfig(
ttl=datetime.timedelta(seconds=3600),
priority='normal',
notification=messaging.AndroidNotification(
title='새 정책 알람',
body=policy,
icon='@drawable/mini',
color='#29ABE2',
sound='default'
),
),
data={
'score': '850',
'time': '2:45',
},
webpush=messaging.WebpushConfig(
notification=messaging.WebpushNotification(
title='웹 알림',
body='TEST',
icon='',
),
),
topic=topic,
)
response = messaging.send(message)
print('Successfully sent message:', response)
for topic in topics:
categorys = re.findall('c[0-9]+[_][0-9]+', topic)
for i in range(len(categorys) - 1, -1, -1):
if '1' == categorys[i].split('_')[-1]:
categorys.pop(i)
pre_topic.append(categorys)
print('new policy category : ', end = '')
policy = input()
policy_category = re.findall('c[0-9]+[_][0-9]+', policy)
policy_category_list = re.findall('c[0-9]+', policy)
push_index = list()
flag = True
for i, categorys in enumerate(pre_topic):
for category in categorys:
if category.split('_')[0] in policy_category_list:
if category not in policy_category:
flag = False
break
if flag: push_index.append(i)
flag = True
for push in push_index:
send_to_topic('[여성가족부] 다자녀 가구에 지원금을 드립니다.', topics[push])