Skip to main content

Python Asynchronous FCM Push Controller

Project description




From now on, easily send FCM (Firebase Cloud Messages) through Python asyncio!

License Package version Supported Python versions





Documnets: https://github.com/837477/async-pyfcm


You can now easily send asynchronous FCM messages.
AsyncPyfcm has the following features:

  • Support for sending Python Asyncio FCM messages
  • Supports all types of messages handled by FCM
  • Very convenient message sending interface
  • Easily handle Firebase credentials (Json File / Json String / Json Object[Dict])
  • Supports automatic access token refresh.
  • Increase performance efficiency by maintaining asynchronous sessions depending on the situation.
  • All exception situations in FCM can be controlled.

Requirements

If you are planning to use FCM now, I think you have already studied FCM.
As you know, google_application_credentials is required to use FCM.
The existing Cloud Messaging API server key will be deprecated on June 20, 2024, so it is recommended to obtain a google_application_credentials key in the future.

To authenticate a service account and authorize it to access Firebase services, you must generate a private key file in JSON format.

To generate a private key file for your service account:

  1. In the Firebase console, open Settings > Service Accounts.
  2. Click Generate New Private Key, then confirm by clicking Generate Key.
  3. Securely store the JSON file containing the key.

For a more detailed explanation, please refer to the following official document.
https://firebase.google.com/docs/cloud-messaging/migrate-v1#provide-credentials-using-adc

Installation

$ pip install async-pyfcm

Example

AsyncPyFCM Initialization

from async_pyfcm import AsyncPyFCM

# AsyncPyFCM requires one required parameter and one optional parameter.

# [param] google_application_credentials:
# Private key issued from Firebase's project service account
# (Json File Path / Json String inside File / Json Object(dict) inside File)

# [param] token_auto_refresh:
# Google API's Access Token expires after a certain period of time.
# Decide whether to automatically refresh the Access Token.

# True (Default): The Access Token is checked immediately before sending the message, and is automatically renewed 30 minutes before expiration.
# False: Access Token is not refreshed automatically.
#   - In this case, the AsyncPyFCM object must be created again.
#   - Suitable for short-term use.

# Case 1: Json File Path
async_pyfcm = AsyncPyFCM(
    google_application_credentials="google-application-credentials.json",
    token_auto_refresh=True
)

# Case 2: Json String inside File
async_pyfcm = AsyncPyFCM(
    google_application_credentials='{"type": "service_account", ...}',
    token_auto_refresh=True
)

# Case 3: Json Object(dict) inside File
async_pyfcm = AsyncPyFCM(
    google_application_credentials={
        "type": "service_account",
        ...
    },
    token_auto_refresh=True
)

You can use it flexibly according to your development situation.

Asynchronous FCM message sending

  • You can check it in the example.py file.
from async_pyfcm import AsyncPyFCM

async def stateful_session():
    # This sample is used when you want to maintain an asynchronous session of aiohttp.
    # You can use resources efficiently by not opening a session every time you send.
    async with AsyncPyFCM(google_application_credentials="path/to/your/credentials.json") as async_fcm:
        responses = await asyncio.gather(
            async_fcm.send(message),
            async_fcm.send(message),
            async_fcm.send(message),
        )
        return responses

async def stateless_session():
    # This sample uses the AsyncPyFCM object by declaring it.
    # This method does not maintain the aiohttp asynchronous session, so it connects the session every time you send.
    async_pyfcm = AsyncPyFCM(
        google_application_credentials="path/to/your/credentials.json"
    )
    responses = await asyncio.gather(
        async_pyfcm.send(message),
        async_pyfcm.send(message),
        async_pyfcm.send(message),
    )
    return responses

Message Resource

  • You can check it in the message.py file.
# All message formats provided by FCM documents are supported.
class Message(TypedDict):
    """
    FCM Message Resource
    https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages
    """
    name: str
    data: dict[str, str]
    notification: Notification
    android: AndroidConfig
    webpush: WebpushConfig
    apns: ApnsConfig
    fcm_options: FcmOptions
    token: str
    topic: str
    condition: str

Contributing

The following is a set of guidelines for contributing to async-pyfcm. These are mostly guidelines, not rules. Use your best judgment, and feel free to propose changes to this document in a pull request.

  1. Please create a branch in this format, <Issue Number>-<Some name>
  2. Open a terminal and navigate to your project path. And enter this. git config --global commit.template .gitmessage.txt
  3. You can use the template, with git commit through vi. Not git commit -m
  4. If you want to merge your work, please pull request to the dev branch.
  5. Enjoy contributing!

If you have any other opinions, please feel free to suggest 😀

License

This project is licensed under the terms of the MIT license.

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-pyfcm-0.1.4.tar.gz (9.8 kB view hashes)

Uploaded Source

Built Distribution

async_pyfcm-0.1.4-py3-none-any.whl (9.1 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