Python client for FCM - Firebase Cloud Messaging (Android, iOS and Web)
npx @tessl/cli install tessl/pypi-pyfcm@2.1.0Python client for Firebase Cloud Messaging (FCM) that enables sending push notifications to Android, iOS, and Web applications. PyFCM provides a comprehensive interface to Google's FCM HTTP v1 API with support for single device messaging, topic-based messaging, conditional messaging, and platform-specific configurations.
pip install pyfcmfrom pyfcm import FCMNotificationError handling:
from pyfcm.errors import (
FCMError,
AuthenticationError,
FCMServerError,
FCMNotRegisteredError,
FCMSenderIdMismatchError,
InvalidDataError,
InternalPackageError,
RetryAfterException
)Package metadata:
from pyfcm import (
__version__,
__author__,
__title__,
__summary__,
__url__,
__email__,
__license__
)from pyfcm import FCMNotification
# Initialize with service account file
fcm = FCMNotification(
service_account_file="path/to/service-account.json",
project_id="your-project-id"
)
# Send notification to a single device
result = fcm.notify(
fcm_token="device_registration_token",
notification_title="Hello World",
notification_body="This is a test notification"
)
# Send data-only message
data_payload = {"key1": "value1", "key2": "value2"}
result = fcm.notify(
fcm_token="device_registration_token",
data_payload=data_payload
)
# Send to a topic
result = fcm.notify(
topic_name="news",
notification_title="Breaking News",
notification_body="Important update"
)PyFCM is built around the FCMNotification class which inherits from BaseAPI. The architecture includes:
The library handles OAuth2 authentication automatically, manages access token refresh, implements retry logic with exponential backoff, and provides thread-safe operations with connection pooling.
Send push notifications to individual devices, topics, or conditional topic expressions. Supports notification messages, data messages, and combined message types with platform-specific configurations for Android, iOS, and Web.
class FCMNotification:
def __init__(
self,
service_account_file: Optional[str] = None,
project_id: Optional[str] = None,
credentials: Optional[Credentials] = None,
proxy_dict: Optional[dict] = None,
env: Optional[str] = None,
json_encoder=None,
adapter=None
):
"""Initialize FCM client"""
def notify(
self,
fcm_token=None,
notification_title=None,
notification_body=None,
notification_image=None,
data_payload=None,
topic_name=None,
topic_condition=None,
android_config=None,
apns_config=None,
webpush_config=None,
fcm_options=None,
dry_run=False,
timeout=120
):
"""Send push notification"""Batch operations for sending notifications to multiple devices concurrently using asyncio and aiohttp for improved performance with large-scale messaging scenarios.
def async_notify_multiple_devices(
self,
params_list: Optional[list] = None,
timeout: int = 5
):
"""Send notifications to multiple devices asynchronously"""Comprehensive exception classes for different FCM error conditions including authentication errors, server errors, invalid tokens, and data validation failures.
class FCMError(Exception): ...
class AuthenticationError(FCMError): ...
class FCMServerError(FCMError): ...
class FCMNotRegisteredError(FCMError): ...
class FCMSenderIdMismatchError(FCMError): ...
class InvalidDataError(FCMError): ...
class InternalPackageError(FCMError): ...
class RetryAfterException(Exception): ...Access to package information and metadata constants for version checking and library identification.
__version__: str # Package version (e.g., "2.1.0")
__author__: str # Package author
__title__: str # Package title
__summary__: str # Package description
__url__: str # Project URL
__email__: str # Author email
__license__: str # License typefrom typing import Optional
from google.oauth2.credentials import Credentials