In Part 1 of this series, we set up our Firebase project and downloaded the all-important Service Account JSON key.
Now, we put that key to work.
In this guide, we will write a simple Python script to authenticate with Firebase and send a push notification to a device (or a topic). This is the core logic you will eventually wrap in an API or a background worker.
Prerequisites
Python installed on your machine.
The
service-account.jsonfile from Part 1.
Step 1: Install the SDK
Google provides a powerful Python library called firebase-admin. It handles the complex authentication protocols (OAuth2) automatically, so you don't have to manually manage access tokens.
Open your terminal and install it:
pip install firebase-admin
Step 2: The Python Script
Create a new file called send_notification.py. Place your downloaded JSON key in the same folder and rename it to serviceAccountKey.json for simplicity.
Here is the complete code to send a message to a "Topic".
-
Why a Topic? Topics are the easiest way to test backend code without needing a specific device token (like an iPhone ID). Any device subscribed to the topic
'news'will receive this message.
Python
import firebase_admin
from firebase_admin import credentials, messaging
# 1. Initialize the SDK
# Point this to the path of the JSON key you downloaded in Part 1
cred = credentials.Certificate("serviceAccountKey.json")
firebase_admin.initialize_app(cred)
def send_to_topic():
# 2. Construct the message
# This object defines the notification content
message = messaging.Message(
notification=messaging.Notification(
title="Hello from Python! ",
body="This notification was triggered by a backend script."
),
topic="news", # Sending to the 'news' topic
)
# 3. Send the message
try:
response = messaging.send(message)
print(" Successfully sent message:", response)
except Exception as e:
print(" Error sending message:", e)
if __name__ == "__main__":
send_to_topic()
Step 3: Understanding the Code
Let's break down what is happening here:
credentials.Certificate(...): This loads your private key. It tells Firebase, "I am the owner of this project, let me in."messaging.Message(...): This is the payload. You can add data fields, images, and custom sounds here.topic="news": In a real app, you might usetoken="DEVICE_TOKEN"to target a specific user. For testing, topics are perfect because you don't need a real device ID handy.
Step 4: Run It
Execute the script in your terminal:
python send_notification.py
If everything is set up correctly, you will see a success message containing a unique message ID string (looks like projects/logicloop.../messages/...).
Common Errors to Watch For
FileNotFoundError: Ensure your JSON key file is in the exact same folder as your Python script, or provide the full absolute path.ValueError: The default Firebase app already exists: This happens if you try to initialize the app twice in the same runtime (common in Jupyter Notebooks).
What's Next?
You now have a working script that sends notifications. But right now, you have to run it manually.
In Part 3, we will automate this. We will move this code to the cloud using Google Cloud Functions and schedule it to run every morning automatically using Cloud Scheduler.

Top comments (0)