-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
68e7425
commit 699a8d7
Showing
3 changed files
with
84 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,85 @@ | ||
from helpers import is_package_installed | ||
import logging | ||
|
||
if is_package_installed('fcm_django'): | ||
from fcm_django.models import FCMDevice | ||
from firebase_admin.messaging import Message, Notification | ||
|
||
def notify_user(user, data, title=None, message=None, silent=True): | ||
''' | ||
Sends an FCM notification to a user | ||
If the message is silent, title and message are included in the data dictionary | ||
''' | ||
if not user: | ||
return | ||
from fcm_django.models import FCMDevice | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
if not data: | ||
data = {} | ||
if not message and not title: | ||
silent = True | ||
if title and silent: | ||
data['title'] = title | ||
if message and silent: | ||
data['message'] = message | ||
logger = logging.getLogger(__name__) | ||
|
||
device = FCMDevice.objects.filter(user=user).first() | ||
if device is None: | ||
return | ||
if silent: | ||
result = device.send_message(data=data) | ||
else: | ||
result = device.send_message(title=title, body=message, data=data) | ||
|
||
class NotificationEvent: | ||
class Event: | ||
def __init__(self, title, prefix): | ||
self.title = title | ||
self.prefix = prefix | ||
|
||
OTHER = Event(_("Otro"), "other") | ||
NEWS_ADDED = Event(_("Nueva noticia"), "news") | ||
|
||
def broadcast_notification(users=None, data=None, title=None, body=None, silent=True): | ||
|
||
def notify_user(user, data, event=NotificationEvent.OTHER, title=None, body=None, image=None, silent=True): | ||
''' | ||
Sends an FCM notification to a user | ||
If the message is silent, title and message are included in the data dictionary | ||
''' | ||
logger.info("Sending FCM notification...") | ||
|
||
if not user: | ||
return | ||
|
||
data = data or {} | ||
data["event"] = str(event.title) | ||
|
||
if not body and not title: | ||
silent = True | ||
if title and silent: | ||
data['title'] = title | ||
if body and silent: | ||
data['message'] = body | ||
|
||
device = FCMDevice.objects.filter(user=user).first() | ||
if device is None: | ||
return | ||
if silent: | ||
result = device.send_message( | ||
Message(data=data) | ||
) | ||
else: | ||
result = device.send_message( | ||
Message(notification=Notification(title=title, body=body, image=image), data=data) | ||
) | ||
logger.info(f"FCM Notification sent: {result}") | ||
|
||
|
||
def broadcast_notification(node_shortname=None, data=None, event=NotificationEvent.OTHER, title=None, body=None, image=None, silent=False): | ||
''' | ||
Sends an FCM notification to a user | ||
Sends an FCM notification broadcast to all devices subscribed to topic | ||
If the message is silent, title and message are included in the data dictionary | ||
''' | ||
devices = FCMDevice.objects.all() | ||
if users is not None: | ||
devices.filter(user__in=users) | ||
logger.info("Sending FCM broadcast...") | ||
|
||
data = data or {} | ||
data["event"] = str(event.title) | ||
|
||
if not data: | ||
data = {} | ||
if not body and not title: | ||
silent = True | ||
if title and silent: | ||
data['title'] = title | ||
if body and silent: | ||
data['message'] = body | ||
|
||
topic = node_shortname + "_" + event.prefix | ||
|
||
if silent: | ||
result = devices.send_message(data=data) | ||
result = FCMDevice.send_topic_message( | ||
Message(data=data), | ||
topic | ||
) | ||
else: | ||
result = devices.send_message(title=title, body=body, data=data) | ||
result = FCMDevice.send_topic_message( | ||
Message(notification=Notification(title=title, body=body, image=image), data=data), | ||
topic | ||
) | ||
|
||
print(result) | ||
logger.info(f"FCM Broadcast sent: {result}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters