Skip to content

Commit 48bc9a2

Browse files
authored
Merge pull request #45 from codeguru42/44-firebase-cloud-messaging
44 firebase cloud messaging
2 parents 1c398be + bb88825 commit 48bc9a2

File tree

7 files changed

+933
-3
lines changed

7 files changed

+933
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
*.jpg
66

77
/http-client.private.env.json
8+
*-firebase-adminsdk-*.json

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: 22 additions & 1 deletion
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,16 +22,35 @@
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):
2631
print(f'Request: {self.request!r}')
2732

2833

2934
@app.task
30-
def process_image_task(image_filename):
35+
def process_image_task(image_filename, fcm_token):
36+
print(f'token: {fcm_token}')
3137
sgf_filename = f'{Path(image_filename).stem}.sgf'
3238
sgf_path = settings.SGF_DIR / sgf_filename
39+
print(f'Processing {image_filename}')
3340
with open(image_filename, 'rb') as image_file:
3441
with open(sgf_path, 'w') as sgf_file:
3542
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)

go_capture/views.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ def capture(request):
2828
@require_POST
2929
def capture_async(request):
3030
image_file = request.FILES['image']
31+
fcm_token = request.POST['fcm_registration_token']
32+
print(f'token: {fcm_token}')
3133
filename = Path(image_file.name)
3234
output_path = settings.IMAGES_DIR / filename
3335
with output_path.open('wb') as output_file:
3436
output_file.write(image_file.read())
35-
process_image_task.delay(str(output_path.absolute()))
37+
process_image_task.delay(str(output_path.absolute()), fcm_token)
3638
return HttpResponse(status=201)
3739

3840

poetry.lock

Lines changed: 898 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Django = "^4.1.3"
1111
environs = "^9.5.0"
1212
gunicorn = "^20.1.0"
1313
celery = "^5.2.7"
14+
firebase-admin = "^6.1.0"
1415

1516
[tool.poetry.dev-dependencies]
1617
pytest = "^7.2.0"

requests.http

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ Content-Type: image/jpeg
1616
POST {{protocol}}{{host}}:{{port}}/capture_async/
1717
Content-Type: multipart/form-data; boundary=WebAppBoundary
1818

19+
--WebAppBoundary
20+
Content-Disposition: form-data; name="fcm_registration_token"
21+
Content-Type: text/plain
22+
23+
foobar
24+
1925
--WebAppBoundary
2026
Content-Disposition: form-data; name="image"; filename="board.jpg"
2127
Content-Type: image/jpeg

0 commit comments

Comments
 (0)