Navin Singh

Navin Singh

Setting up platform independent push notifications with Django and Firebase

In this article we'll see how we can use Firebase for sending push notifications across Android, iOS and Web apps with minimum setup and technical overhead.

Firebase is a mobile and web application development platform that provides a host of features to help build an application, over the years it has evolved only to be better and faster, it can compete head to head with AWS Mobile Hub probably better.

You can use Firebase's features selectively depending on what your application needs. In this article we'll only be working with Push notifications, which is Firebase cloud messaging.

Things we'll need:

Step 1

Setting up Firebase account and app
  • Create an account with Google cloud by simply using your existing google credentials, dont worry its free
  • Navigate to and go to console
  • Click on Add project as shown in the image below
  • Fill in the app name and select your desired location (select location preferably closest to your major user base)

You will be greeted with instructions on how to integrate with various platforms.

Getting the FCM_SERVER_KEY:

  • Click the settings cog on project overview in sidebar on firebase console
  • Navigate to cloud messaging tab

This is all we need to do for now we'll come back to this once we setup our django project.

Step 2

Setup a Django project or use an existing one

Install FCM-Django project from pip

pip install fcm-django

Setup fcm-django in your

 INSTALLED_APPS = ( ... "fcm_django" ) 
    "FCM_SERVER_KEY": "[your api key]",
    "ONE_DEVICE_PER_USER": True/False,

Run: python migrate to setup the database models, this is all the configuration we need in Django.

Step 3

Testing if it all works

Open a new shell: python shell

from fcm_django.models import FCMDevice
from django.contrib.auth.models import User

device = FCMDevice() // instantiate fcmdevice object
device.device_id = "Device ID"
device.registration_id = "Device registration id"
device.type = "Android" // simple string field doesnt matter what you pass = "Can be anything"
device.user = User.objects.all().first()

device.send_message(title="Title", body="Message", data={"test": "test"})

If your configurations are correct the send_message method should return a success message else there might be something you missed in configration.

###How it all works explained:

Fcm Django:

  • Creates new models to store User's device information nothing more than the type of the device and device ID which is obtained from respective apps either on ios or android
  • When we need to send notification to a particular user group, use django filters like FCMDevice.objects.filter(user__in=[user_ids...]) this will return respective devices for the users in list, if selected users do not have a device associated it will return an empty queryset which is fine, it wont break any code.
  • Once you have devices list all you have to do is devices.send_message(your message details) method params can be found either in the documentation or you can refer the core FCMDevice model
  • FCM-django then makes a connection with Firebase with your server keys and sends out the push notification

Disclaimer: FCM Django is just one of the packages that help make connection with Firebase there are plenty others, choose depending on what you need in your application, for a simple project that needs only push notification this is perfect

Devil's advocate

This article is similar to the FCM-django's readme file, why'd you waste my time? Answer to that is, I've used this extensively in production environments in multiple ocassions. I have re-written the package to suit my needs, I know it inside out, using this article I can help many others who are in similar dilemma, shoot your questions in comments.

What's next:

A few questions left unanswered here:

  • How do I know if it works with mobile apps
  • You haven't shown it work in any application

The point of this article was to setup the firebase with django, I will be covering the app part of it in another article, unitl then sit tight, try and figure things out on your if you can, that gives immense pleasure i bet you.

Hope this helps. Happy coding!