Django push notifications- FCM,PyFcm,txFcm??

Nancy Khullar
3 min readAug 10, 2020

--

-Nancy Khullar

What are Push Notifcations?

“Push notifications is a mechanism for a mobile/web application to convey certain data or messages from one edge to another.”

Hold on, so technical right. So, just scroll down your notification bar and all that you can see is what is called as push notifications!! Whether it be your zomato offers, dominos treat or your social media popups.

Why am I understanding this?

Today’s blog is just to get a bird’s eye view of how it works to send realtime notifications in just few clicks!

So, Imagine you have to send a notification from device to device, android to ios, or ios to web,or from one device to multiple devices!! It seems to be a tough job. But it isn’t. Before getting into “How to implement these?”, Let’s talk about which one to use!!

I assume you all have your django setup ready. If not, just hurry up and get it installed. And set up your environment (firebase).

TYPES- FCM, PyFcm or txFcm??

FCM- “ Firebase Cloud Messaging”,a cross-platform messaging solution that lets you reliably send messages at no cost. It provides it’s own model for sending notifications (FCMDevice Model )and overriding it is a tedious task!

PyFCM- “Python client for FCM”. It includes all functionality as fcm and in plus, adding your custom model is an easy task here! But, PyFCM is good for synchronous ( blocking ) python.

txFCM- “Twisted Python client for FCM”. It includes all functionalities covered in PyFCM and If you have to send high number of concurrent messages then using Twisted is a good option and it removes your blocking problem too!

So, done with the theory,Let’s catch you to the implementation!

FCMDEVICE Model: Used as it is when using FCM, can be written custom with other 2 types.

Fields-

<> registration_id (required — is FCM token of a device)
<> name (optional)
<> active (default: true)
<> user (optional)
<> device_id (optional — can be used to uniquely identify devices)
<> type (‘android’, ‘web’, ‘ios’)

Step1 : Installation:

"For FCM"  - pip install fcm-django
"For PyFcm"- pip install pyfcm
"For txFcm"- pip install txfcm

Step2 : Edit your settings.py:

For FCM:
FCM_DJANGO_SETTINGS = {
"FCM_SERVER_KEY": "[your api key]"
}
For others:
FCM_SERVER_KEY="[your api key]"
#you can get this api key: https://console.firebase.google.com/project/<project-name>/settings/cloudmessagingNote: While using FCM add the following to your installed apps. INSTALLED_APPS = (
...
"fcm_django"
)

Step3 : Let’s import notification classes from each of the libraries!

"For FCM"-   from fcm_django.models import FCMDevice
"For PyFcm"- from pyfcm import FCMNotification
"For txFcm"- from txfcm import TXFCMNotification
from twisted.internet import reactor
from settings import FCM_SERVER_KEY

Step4 : Almost there, so let’s define our NOTIFICATION FUNCTION

But, before doing that. Keep a look at this- “send_notification” function has two parts. First, Notification which you can refer for sending the title or the message and the other, Data message ehich includes the additional information that you wish to send along with the title that can help you with further pages.

for example- You recieved a notification from zomato saying “ your order is out for delivery”(i.e,notification part) and when you tap on that popup, you find the info related to order.(i.e,data part)

Moving on, let’s define a common send_notification that can be called at different times(“order placed”,”cancelled order”, “out for delivery”,etc) for each type!

  1. FCM
def send_notification(user_ids,title, message, data):
try:
device = FCMDevice.objects.filter(user__in=user_ids).first()
result = device.send_message(title=title,body=message,
data=data,sound=True)
return result
except:
pass

2. PyFcm:

def send_notification(user_objects,title, message, data):
try:
push_service = FCMNotification(api_key=FCM_SERVER_KEY)
fcm_token = []
for obj in user_objects:
fcm_token.append(obj.registration_id)
return push_service.notify_multiple_devices(
registration_ids=fcm_token,message_title=title,
message_body=message, data_message=data)
except:
pass

3. txFCM:

def send_notification(user_objects,title, message, data):
try:
push_service = TXFCMNotification(api_key=FCM_SERVER_KEY)
fcm_token = []
for obj in user_objects:
fcm_token.append(obj.registration_id)
return push_service.notify_multiple_devices(
registration_ids=fcm_token,message_title=title,
message_body=message, data_message=data)
except:
pass

Step5 : Let’s call this function and we are done!

#defining data message (Extra details)
data = {
"name": "HORN OK PLEASE!",
"days": 3,
"country": "United States"
}

calling function : Here, user_ids to be send in FCM and user objects otherwise. These are id’s of user who will recieve notification

send_notification(user_ids=["1","2","3"],
title="It's now or never: Horn Ok is back!",
message="Book now to get 50% off!",
data=data)

Here you go,you got notification! And, we are done!

--

--

Responses (4)