Metadata-Version: 2.4
Name: pullkin
Version: 1.0.0
Summary: Asynchronous Python client for receiving FCM/GCM push notifications directly over Google's MCS protocol
Home-page: https://github.com/WhiteApfel/pullkin
Author: WhiteApfel
Author-email: white@pfel.ru
License: MPL-2.0
Project-URL: Homepage, https://github.com/WhiteApfel/pullkin
Project-URL: Documentation, https://pullkin.readthedocs.io/en/latest/
Project-URL: Repository, https://github.com/WhiteApfel/pullkin
Project-URL: Issues, https://github.com/WhiteApfel/pullkin/issues
Project-URL: Changelog, https://pullkin.readthedocs.io/en/latest/guide/migration/
Keywords: fcm gcm push notification receive receiving firebase cloud messaging mcs mtalk asyncio android
Classifier: Development Status :: 5 - Production/Stable
Classifier: Framework :: AsyncIO
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Topic :: Communications
Classifier: Topic :: Software Development :: Libraries
Classifier: Typing :: Typed
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: betterproto<2.0,>=1.2.5
Requires-Dist: cryptography<47.0,>=42.0.5
Requires-Dist: http-ece<2.0,>=1.2.0
Requires-Dist: httpx<1.0,>=0.27.0
Requires-Dist: loguru<1.0,>=0.7.2
Requires-Dist: pydantic<3.0,>=2.6.2
Dynamic: author
Dynamic: author-email
Dynamic: classifier
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: keywords
Dynamic: license
Dynamic: license-file
Dynamic: project-url
Dynamic: requires-dist
Dynamic: requires-python
Dynamic: summary

# Pullkin

[![Tests](https://github.com/WhiteApfel/Pullkin/actions/workflows/test_branch.yml/badge.svg)](https://github.com/WhiteApfel/Pullkin/actions/workflows/test_branch.yml)
[![Docs](https://readthedocs.org/projects/pullkin/badge/?version=latest)](https://pullkin.readthedocs.io/en/latest/)
[![PyPI - Version](https://img.shields.io/pypi/v/pullkin)](https://pypi.org/project/pullkin/)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pullkin)](https://pypi.org/project/pullkin/)
[![PyPI - Downloads](https://img.shields.io/pypi/dm/pullkin)](https://pypi.org/project/pullkin/)
[![License](https://img.shields.io/github/license/whiteapfel/pullkin)](https://github.com/WhiteApfel/Pullkin/blob/master/LICENSE)

**Asynchronous Python client for receiving Firebase Cloud Messaging (FCM / GCM) push notifications directly over Google's MCS protocol.**

Like Alexander Pushkin — except instead of pushing notifications, **Pullkin** pulls them in.

Pullkin emulates an Android/Chrome MCS client without requiring a headless browser. It is a clean asynchronous redesign inspired by [push_receiver](https://github.com/Francesco149/push_receiver) and [push-receiver](https://github.com/MatthieuLemoine/push-receiver).

---

## Features

- **Fully `asyncio`-native** — built on `asyncio` TLS streams (`mtalk.google.com:5228`) and `httpx.AsyncClient`.
- **Multi-app support** — attach multiple independent `sender_id` connections on one `Pullkin` instance.
- **Resilient by default** — configurable MCS heartbeats, ACK timeouts, exponential-backoff reconnects, and `persistent_id` deduplication.
- **Encrypted & unencrypted payloads** — handles Web Push `http-ece` payloads and direct Android GCM/FCM data messages seamlessly.
- **Typed Pydantic models** — validate and auto-convert incoming notifications directly via handler type annotations (`Message`, `NotificationData`, generics, `Optional`/unions).
- **Flexible execution modes** — blocking `run()`, non-blocking background `start()`, or pull-based `listen_coroutine()`.

## Who Is Pullkin For?

- **End-to-end push testing in CI**: Verify that your backend actually delivers real Firebase notifications without maintaining emulators or Appium farms.
- **Headless automation & OTP/2FA bots**: Receive confirmation codes or status pushes from mobile-first services on a Linux server without Chromium or Electron.
- **Multi-app push gateways & monitoring**: Listen to multiple Firebase `sender_id`s from a single Python process and measure real delivery latency.
- **When NOT to use Pullkin**: If you only want to *send* push notifications from a backend, use [`firebase-admin`](https://pypi.org/project/firebase-admin/). Pullkin is specifically for **receiving** pushes as a client device.

## Documentation

Full documentation (and LLM-friendly [`llms.txt`](https://pullkin.readthedocs.io/en/latest/llms.txt)) is hosted on **[Read the Docs](https://pullkin.readthedocs.io/en/latest/)**:

- [Installation](https://pullkin.readthedocs.io/en/latest/guide/install/)
- [Quickstart](https://pullkin.readthedocs.io/en/latest/guide/quickstart/)
- [Registration & Tokens](https://pullkin.readthedocs.io/en/latest/guide/registration/)
- [Listening & Lifecycle](https://pullkin.readthedocs.io/en/latest/guide/listening/)
- [Handlers & Typed Models](https://pullkin.readthedocs.io/en/latest/guide/handlers/)
- [Migration Guide & Breaking Changelog](https://pullkin.readthedocs.io/en/latest/guide/migration/)
- [API Reference](https://pullkin.readthedocs.io/en/latest/API/modules/)

## Installation

```bash
pip install pullkin
```

Requires **Python 3.10–3.15**.

## Quickstart

```python
import asyncio
from pathlib import Path

from pullkin import Pullkin
from pullkin.models import AppCredentials, Message
from pullkin.proto.mcs_proto import DataMessageStanza

SENDER_ID = "123456789012"
APP_ID = "1:123456789012:android:abcdef1234567890"
API_KEY = "AIzaSyExampleApiKey"
FIREBASE_PROJECT_ID = "my-firebase-project"
ANDROID_CERT = "da39a3ee5e6b4b0d3255bfef95601890afd80709"
APP_NAME = "com.example.app"
CREDS_FILE = Path("credentials.json")

pullkin = Pullkin()


@pullkin.on_notification()
async def handle_notification(message: Message, data_message: DataMessageStanza):
    print("Notification:", message.notification)
    print("Data:", message.data)


@pullkin.on_persistent_id()
async def save_persistent_id(sender_id: str, persistent_id: str):
    # Persist IDs to avoid re-receiving already processed messages after restart
    print(f"[{sender_id}] persistent_id={persistent_id}")


async def get_credentials() -> AppCredentials:
    if CREDS_FILE.exists():
        return AppCredentials.model_validate_json(CREDS_FILE.read_text(encoding="utf-8"))

    credentials = await pullkin.register(
        sender_id=SENDER_ID,
        app_id=APP_ID,
        api_key=API_KEY,
        firebase_project_id=FIREBASE_PROJECT_ID,
        android_cert=ANDROID_CERT,
        app_name=APP_NAME,
    )
    CREDS_FILE.write_text(credentials.model_dump_json(indent=2), encoding="utf-8")
    return credentials


async def main():
    credentials = await get_credentials()
    print("Device token:", credentials.token)

    await pullkin.add_app(sender_id=SENDER_ID, credentials=credentials)
    try:
        await pullkin.run()
    finally:
        await pullkin.stop()


if __name__ == "__main__":
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        pass
```

> **Note on tokens:** Always read the device registration token via `credentials.token` (not `credentials.fcm.token`). By default, `register(..., web_push=False)` skips Web Push registration and returns an Android GCM token, so `credentials.fcm` is `None`. See [Registration & Tokens](https://pullkin.readthedocs.io/en/latest/guide/registration/) for details.
