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