FCM – send push notifications using Python

What is FCM ?

FCM – Firebase Cloud Messaging is a cross-platform  ( Android, iOS and Chrome ) messaging solution that lets you reliably deliver messages at no cost. FCM is best suited if you want to send push notification to your app which you built to run on Android and iOS. The advantage you get is you don’t have to separately deal with GCM (Google Cloud Messaging deprecated now) and Apple’s APNS. You hand over your notification message to FCM and FCM takes care of communicating with apple’s APNS and Android messaging servers to reliably deliver those messages.


Using FCM we can send message to single device or multiple devices.  There are two different types of messages, notification and data. Notification messages include JSON keys that are understood and interpreted by phone’s operating system. If you want to include customized app specific JSON keys use data message. You can combine both notification and data JSON objects in single message. You can also send messages with different priority.

Note : – You need to set priority  to high  if you want phone to wake up and show notification on screen

Sending message with Python

We can use PyFCM to send messages via FCM. PyFCM is good for synchronous ( blocking ) python. We will discuss non-blocking option in next paragraph.

Install PyFCM using following command

The following code will send a push notification to

So, the PyFCM API is the pretty straight forward to use.

Sending FCM push notification using Twisted

PyFCM discussed in above paragraph is good enough if you want to send messages in blocking fashion. If you have to send high number of concurrent messages then using Twisted is a good option.

Twisted Matrix
Twisted Matrix

Network operations performed using twisted library don’t block. Thus it’s a good choice when network concurrency is required by program. We can use txFCM library to send FCM messages using twisted

Install txFCM using following command

Following code send FCM message using txFCM

txFCM is built on top of PyFCM so all the API call that are available in PyFCM are also available in txFCM.

Published by


Hello, I am Godson at the helm of Lintel. It's nice meeting you !

16 thoughts on “FCM – send push notifications using Python”

  1. So I’m trying to use the PyFCM but I can’t get the registration_id. I don’t even know how I would get it.

    1. Hi Alex,

      Using following code-snippets, you can retrieve device token-id

      For android
      String refreshedToken = FirebaseInstanceId.getInstance().getToken().

      For Object-c
      NSString *refreshedToken = [[FIRInstanceID instanceID] token];

      For swift
      let token = FIRInstanceID.instanceID().token()!

      Following are detailed docs to setup FCM at client

      Android – https://firebase.google.com/docs/cloud-messaging/android/client

      iOS – https://firebase.google.com/docs/notifications/ios/console-device

      With help of fcm console and this token-id, you can send test-notifications to your device. If you want to send notifications from your server using pyFCM, you need to deliver this token-id to server. Ideally at time of authentication with your server, you can do it.

      1. Hi how can I add the code following registration_id to pyfcm ?

        I have some android apps, my push receiver build with ionic and the trigger build with pyfcm, what should I do with registration_id ? thanks.

        1. Hi Budhi, in article it is already mentioned how to use registeration_id aka Registration Token ( in fcm lingo ) –

          result = push_service.notify_single_device(registration_id=registration_id, message_title=message_title, message_body=message_body)
          print result

  2. I have webapp in web2py, it sends email to user at some point at same time i want to sendandroid notification fro that user. How can i do it???

  3. hi
    i get this error HTTPSConnectionPool(host=’fcm.googleapis.com’, port=443): Max retries exceeded with url: /fcm/send (Caused by ProxyError(‘Cannot connect to proxy.’, NewConnectionError(‘: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it’,)))

  4. I has a test server run linux, can I use c or c++ code to call some API to send message use FCM? Does there has a C or C++ API about FCM?

Leave a Reply

Your email address will not be published. Required fields are marked *