Skip to main content

Firebase Cloud Messaging Adapter

Project description

FCM Adapter

This is a simple adapter for the FCM (Firebase Cloud Messaging) service.

Installation

pip install fcm_adapter
poetry add fcm_adapter

Usage

from fcm_adapter import FCMAdapter
import json


async def main(token: str):
    with open('key.json') as f:
        key = json.load(f)

    fcm_adapter = FCMAdapter(key)

    await fcm_adapter.send_message(
        {
            "token": token,
            "notification": {"title": "Hello World"},
        }
    )

FCMAdapter initialization

Parameters

  • key: dict - Google key. You can get it from the Firebase Console. Documentation here.
  • client: Union[httpx.AsyncClient, None] = None - httpx.AsyncClient instance. If not provided, a new instance will be created.
  • send_message_url: Union[str, None] = None - FCM send message url. If not provided, the default value will be used.
  • validate_only: bool = False - Flag for testing the request without actually delivering the message. Works only with the send_message method.

send

Send a message to a device. See FCM documentation for more details.

Parameters

  • data: dict - Data to send to FCM service.

Returns

Example

from fcm_adapter import FCMAdapter


async def main(fcm_adapter: FCMAdapter, token: str):
    response = await fcm_adapter.send(
        {
            "validate_only": False,
            "message": {
                "token": token,
                "notification": {"title": "Hello World"},
            },
        }
    )

send_message

Send a message to a device. The function is a wrapper around the send method, but it expects to receive only inner message data.

If fcm_adapter.validate_only is set to True, the request to FCM will be sent with "validate_only": true.

Parameters

  • message: dict - Message data to send to FCM service.

Returns

Example

from fcm_adapter import FCMAdapter


async def main(fcm_adapter: FCMAdapter, token: str):
    response = await fcm_adapter.send_message(
        {
            "token": token,
            "notification": {"title": "Hello World"},
        }
    )

Development

First, install poetry.

Then, install dependencies:

poetry install

Formatting

poetry run black .

Testing

poetry run pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fcm_adapter-0.1.1.tar.gz (4.8 kB view hashes)

Uploaded Source

Built Distribution

fcm_adapter-0.1.1-py3-none-any.whl (5.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page