push_messaging.py 2.06 KB
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])