-
Notifications
You must be signed in to change notification settings - Fork 0
/
notification.py
54 lines (43 loc) · 1.51 KB
/
notification.py
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
# -*- coding: utf-8 -*-
import os
import time
import requests
import json
from datetime import datetime, timedelta
from pytz import timezone
from apscheduler.schedulers.background import BackgroundScheduler
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
django.setup()
from config.keys import firebase
from univ.models import Schedule
KST = timezone('Asia/Seoul')
def send_fcm_notification(ids, title, body):
url = 'https://fcm.googleapis.com/fcm/send'
headers = {
'Authorization': firebase.key,
'Content-Type': 'application/json; UTF-8',
}
content = {
'to': ids,
'notification': {'title': title, 'body': body},
"data": {"id": "id"}
}
requests.post(url, data=json.dumps(content), headers=headers)
def notify():
schedules = Schedule.objects.all()
for schedule in schedules:
liked_users = schedule.major.devices.all()
tokens = [user.token for user in liked_users]
day_before = timedelta(days=1)
now_hour = KST.localize(datetime.now().replace(minute=0, second=0, microsecond=0))
if now_hour == (schedule.end_date - day_before):
for token in tokens:
send_fcm_notification(token, f'{schedule.description} 마감 하루 전이에요!', schedule.description)
if __name__ == '__main__':
scheduler = BackgroundScheduler()
scheduler.configure(timezone=KST)
scheduler.start()
scheduler.add_job(notify, 'interval', hours=1)
while True:
time.sleep(1)