-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
69 lines (50 loc) · 2 KB
/
main.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import time
import requests
import json
from oauth2client.service_account import ServiceAccountCredentials
import schedule
import threading
hours1 = ['00:00', '00:00', '00:00', '00:00']
hours2 = ['00:00', '00:00', '00:00', '00:00']
PROJECT_ID = 'moca-7c36d'
SCOPES = ['https://www.googleapis.com/auth/firebase.messaging']
BASE_URL = 'https://fcm.googleapis.com'
FCM_ENDPOINT = 'v1/projects/' + PROJECT_ID + '/messages:send'
FCM_URL = BASE_URL + '/' + FCM_ENDPOINT
def _get_access_token():
credentials = ServiceAccountCredentials.from_json_keyfile_name(
'service-account.json', SCOPES)
access_token_info = credentials.get_access_token()
return access_token_info.access_token
def _send_fcm_message(fcm_message):
headers = {
'Authorization': 'Bearer ' + _get_access_token(),
'Content-Type': 'application/json; UTF-8',
}
resp = requests.post(FCM_URL, data=json.dumps(fcm_message), headers=headers)
if resp.status_code == 200:
print('Message sent to Firebase for delivery, response:')
print(resp.text)
else:
print('Unable to send message to Firebase')
print(resp.text)
def _build_common_message(topic):
return {
'message': {
'topic': topic,
'notification': {
'title': 'FCM Notification',
'body': 'Notification from FCM'
},
},
}
def schdeule_notification(topic, hours):
schedule.every().days.at(hours[0]).do(_send_fcm_message, _build_common_message(topic))
schedule.every().days.at(hours[1]).do(_send_fcm_message, _build_common_message(topic))
schedule.every().days.at(hours[2]).do(_send_fcm_message, _build_common_message(topic))
schedule.every().days.at(hours[3]).do(_send_fcm_message, _build_common_message(topic))
while 1:
schedule.run_pending()
time.sleep(1)
threading.Thread(target=schdeule_notification, args=("time1", hours1)).start()
threading.Thread(target=schdeule_notification, args=("time2", hours2)).start()