Skip to main content

Async Firebase Client - a Python asyncio client to interact with Firebase Cloud Messaging in an easy way.

Project description

async-firebase is a lightweight asynchronous client to interact with Firebase Cloud Messaging for sending push notification to Android and iOS devices

PyPI download total PyPI download month PyPI version fury.io PyPI license PyPI pyversions GitHub Workflow Status for CI Codacy coverage

  • Free software: MIT license
  • Requires: Python 3.6+

Features

  • Extremely lightweight and does not rely on firebase-admin which is hefty
  • Send push notifications to Android and iOS devices
  • Set TTL (time to live) for notifications
  • Set priority for notifications
  • Set collapse-key for notifications
  • Dry-run mode for testing purpose

Installation

$ pip install async-firebase

Getting started

To send push notification to Android:

import asyncio

from async_firebase import AsyncFirebaseClient


async def main():
    client = AsyncFirebaseClient()
    client.creds_from_service_account_file("secret-store/mobile-app-79225efac4bb.json")

    # or using dictionary object
    # client.creds_from_service_account_info({...}})

    device_token = "..."

    android_config = client.build_android_config(
        priority="high"
        ttl=2419200,
        collapse_key="push",
        data={"discount": "15%", "key_1": "value_1", "timestamp": "2021-02-24T12:00:15"},
        title="Store Changes"
        body="Recent store changes",
    )
    response = await client.push(device_token=device_token, android=android_config)

    print(response)

if __name__ == "__main__":
    asyncio.run(main())

To send push notification to iOS:

import asyncio

from async_firebase import AsyncFirebaseClient


async def main():
    client = AsyncFirebaseClient()
    client.creds_from_service_account_file("secret-store/mobile-app-79225efac4bb.json")

    # or using dictionary object
    # client.creds_from_service_account_info({...}})

    device_token = "..."

    apns_config = client.build_apns_config(
        priority="normal",
        ttl=2419200,
        apns_topic="store-updated",
        collapse_key="push",
        title="Store Changes"
        alert="Recent store changes",
        badge=1,
        category="test-category",
        custom_data={"discount": "15%", "key_1": "value_1", "timestamp": "2021-02-24T12:00:15"}
    )
    response = await client.push(device_token=device_token, apns=apns_config)

    print(response)

if __name__ == "__main__":
    asyncio.run(main())

This prints:

{"name": "projects/mobile-app/messages/0:2367799010922733%7606eb557606ebff"}

To manual construct message:

from async_firebase.messages import APNSConfig, APNSPayload, ApsAlert, Aps
from async_firebase import AsyncFirebaseClient


async def main():
    apns_config = APNSConfig(**{
        "headers": {
            "apns-expiration": str(int(datetime.utcnow().timestamp()) + 7200),
            "apns-priority": "10",
            "apns-topic": "test-topic",
            "apns-collapse-id": "something",
        },
        "payload": APNSPayload(**{
            "aps": Aps(**{
                "alert": ApsAlert(title="some-title", body="alert-message"),
                "badge": 0,
                "sound": "default",
                "content_available": True,
                "category": "some-category",
                "mutable_content": False,
                "custom_data": {
                    "link": "https://link-to-somewhere.com",
                    "ticket_id": "YXZ-655512
                },
            })
        })
    })

    device_token = "..."

    client = AsyncFirebaseClient()
    client.creds_from_service_account_info({...})
    response = await client.push(device_token=device_token, apns=apns_config)


if __name__ == "__main__":
    asyncio.run(main())

License

async-firebase is offered under the MIT license.

Source code

The latest developer version is available in a GitHub repository: https://github.com/healthjoy/async-firebase

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

async-firebase-1.2.0.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

async_firebase-1.2.0-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file async-firebase-1.2.0.tar.gz.

File metadata

  • Download URL: async-firebase-1.2.0.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.2 Linux/5.4.0-1041-azure

File hashes

Hashes for async-firebase-1.2.0.tar.gz
Algorithm Hash digest
SHA256 09a4d71db420bb5a5af6b5abaa56ae05e26f94c0cfba9df9342619ee20ce648b
MD5 87e2420f812497d0489758713eec27b9
BLAKE2b-256 4926c5d966532d02d19650c300c4e8ee31595e7b813e0e2fe4a3b3eb104e0c59

See more details on using hashes here.

File details

Details for the file async_firebase-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: async_firebase-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.2 Linux/5.4.0-1041-azure

File hashes

Hashes for async_firebase-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6768b27acb319497754b7b60d84085781d95652f1e1e29412ce3d8db3f30e6dd
MD5 682359f9085d73a3ea0b1e85f1b990f0
BLAKE2b-256 ab2d75dbc86a0dd06bbc2d67709db319da1f7ffa152987e143678c524aeb5fe3

See more details on using hashes here.

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