Skip to content

Commit bb88825

Browse files
committed
Send message via FCM
1 parent 6d7217b commit bb88825

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

go_capture/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,5 @@
129129

130130
SGF_DIR = BASE_DIR / 'sgf'
131131
SGF_DIR.mkdir(exist_ok=True)
132+
133+
FIREBASE_CREDENTIALS_FILE = BASE_DIR / env.str('FIREBASE_CREDENTIALS_FILE')

go_capture/tasks.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import os
22
from pathlib import Path
33

4+
import firebase_admin
45
from celery import Celery
56
from django.conf import settings
7+
from firebase_admin import credentials, messaging
68

79
from go_capture.sgf.process_image import process_image
810

@@ -20,6 +22,9 @@
2022
# Load task modules from all registered Django apps.
2123
app.autodiscover_tasks()
2224

25+
cred = credentials.Certificate(settings.FIREBASE_CREDENTIALS_FILE)
26+
firebase_admin.initialize_app(cred)
27+
2328

2429
@app.task(bind=True)
2530
def debug_task(self):
@@ -31,6 +36,21 @@ def process_image_task(image_filename, fcm_token):
3136
print(f'token: {fcm_token}')
3237
sgf_filename = f'{Path(image_filename).stem}.sgf'
3338
sgf_path = settings.SGF_DIR / sgf_filename
39+
print(f'Processing {image_filename}')
3440
with open(image_filename, 'rb') as image_file:
3541
with open(sgf_path, 'w') as sgf_file:
3642
process_image(image_file, sgf_file)
43+
44+
# See documentation on defining a message payload.
45+
message = messaging.Message(
46+
data={
47+
'sgf_file': str(sgf_path),
48+
},
49+
token=fcm_token,
50+
)
51+
52+
# Send a message to the device corresponding to the provided
53+
# registration token.
54+
response = messaging.send(message)
55+
# Response is a message ID string.
56+
print('Successfully sent message:', response)

0 commit comments

Comments
 (0)