soonmyeong2

make push alarm system by python file

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')
default_app = firebase_admin.initialize_app(cred)
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():
# [START send_to_topic]
# The topic name can be optionally prefixed with "/topics/".
topic = 'c1_1'
# See documentation on defining a message payload.
def send_to_topic(policy, topic):
message = messaging.Message(
title = 'wef',
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,
)
# Send a message to the devices subscribed to the provided topic.
response = messaging.send(message)
# Response is a message ID string.
print('Successfully sent message:', response)
send_to_topic()
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])
......