I’m trying to setup Firebase push notifications using fcm_django but I’m not able to receive any push notifications through FCM/GCM. I used signals(Post_save) to handle the sending of new notifications, here’s my notification’s view :
from django.shortcuts import render
from django.db.models.signals import post_save
from django.dispatch import receiver
from drf_yasg.utils import swagger_auto_schema
# fcm_django
from fcm_django.models import FCMDevice
from firebase_admin.messaging import Message, Notification
from .models import Notification as Notification_model
from account.models import PhoneOTP
# Create your views here.
@receiver(post_save, sender=Notification_model)
#@database_sync_to_async
def send_push_notification(sender, instance, **kwargs):
#print(f"here's the notification Id: {instance.pk}")
devices = FCMDevice.objects.filter(user=instance.target)
# Send push notifications to GCM Devices
for device in devices:
device.send_message(
message = Message(
notification=Notification(
title = instance.get_content_object_type(),
body = instance.verb
),
),
# this is optional
# app=settings.FCM_DJANGO_SETTINGS['DEFAULT_FIREBASE_APP']
)
here’s my fcm_django push settings:
CUSTOM_GOOGLE_APPLICATION_CREDENTIALS = '/home/taskitly/taskily/src/taskitly-notifications-firebase-adminsdk-sv7au-94373f2754.json'
class CustomFirebaseCredentials(credentials.ApplicationDefault):
def __init__(self, account_file_path: str):
super().__init__()
self._account_file_path = account_file_path
def _load_credential(self):
if not self._g_credential:
self._g_credential, self._project_id = load_credentials_from_file(self._account_file_path,
scopes=credentials._scopes)
# init default firebase app
# this loads the default google service account with GOOGLE_APPLICATION_CREDENTIALS env variable
FIREBASE_APP = initialize_app()
# init second firebase app for fcm-django
# the environment variable contains a path to the custom google service account JSON
custom_credentials = CustomFirebaseCredentials(CUSTOM_GOOGLE_APPLICATION_CREDENTIALS)
FIREBASE_MESSAGING_APP = initialize_app(custom_credentials, name='messaging')
FCM_DJANGO_SETTINGS = {
# an instance of firebase_admin.App to be used as default for all fcm-django requests
# default: None (the default Firebase app)
"DEFAULT_FIREBASE_APP": FIREBASE_MESSAGING_APP,
# default: _('FCM Django')
"APP_VERBOSE_NAME": "Taskitly",
# true if you want to have only one active device per registered user at a time
# default: False
"ONE_DEVICE_PER_USER": True,
# devices to which notifications cannot be sent,
# are deleted upon receiving error response from FCM
# default: False
"DELETE_INACTIVE_DEVICES": True,
}