Fundraising in PDF.
[wolnelektury.git] / src / push / utils.py
1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
3 #
4 import firebase_admin
5 from firebase_admin import credentials, messaging
6 from django.conf import settings
7
8
9 cred = None
10 if hasattr(settings, 'FCM_PRIVATE_KEY_PATH'):
11     cred = credentials.Certificate(settings.FCM_PRIVATE_KEY_PATH)
12     firebase_admin.initialize_app(cred)
13
14 TOPIC = 'wolnelektury'
15
16
17 def send_fcm_push(title, body, image_url=None):
18     if cred is None:
19         return
20     # See documentation on defining a message payload.
21     data = {}
22     # data = {
23     #     'title': title,
24     #     'body': body,
25     # }
26     if image_url:
27         data['imageUrl'] = image_url
28     message = messaging.Message(
29         notification=messaging.Notification(
30             title=title,
31             body=body,
32         ),
33         data=data,
34         topic=TOPIC,
35     )
36     message_id = messaging.send(message)
37     return message_id