aiopyfcm


Nameaiopyfcm JSON
Version 0.2.1 PyPI version JSON
download
home_pagehttps://github.com/837477/aiopyfcm
Summaryaiopyfcm is Python Asynchronous FCM Push Controller
upload_time2024-10-25 08:56:07
maintainerNone
docs_urlNone
author837477
requires_pythonNone
licenseMIT License
keywords aiopyfcm is python asynchronous fcm push controller
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <br><br><br>
<p align="center">
  <a href="https://github.com/837477/aiopyfcm"><img src="https://github.com/user-attachments/assets/5825c842-8031-4ada-ab37-e2be74653e69"></a>
</p>
<p align="center">
    <em>From now on, you will be able to conveniently send FCM messages asynchronously through Python.</em>
</p>
<p align="center">
<a href="https://github.com/837477/aiopyfcm/blob/main/LICENSE" target="_blank">
    <img src="https://img.shields.io/pypi/l/aiopyfcm?color=FEC301" alt="License">
</a>
<a href="https://pypi.org/project/aiopyfcm" target="_blank">
    <img src="https://img.shields.io/pypi/v/aiopyfcm?color=FEC301" alt="Package version">
</a>
<a href="https://pypi.org/project/aiopyfcm" target="_blank">
    <img src="https://img.shields.io/pypi/pyversions/aiopyfcm?color=FEC301" alt="Supported Python versions">
</a>
</p>
<br><br><br>

---

**Github**: <a href="https://github.com/837477/aiopyfcm" target="_blank">https://github.com/837477/aiopyfcm </a><br>
**PyPi**: <a href="https://pypi.org/project/aiopyfcm" target="_blank">https://pypi.org/project/aiopyfcm </a>

---

You can now easily send asynchronous FCM messages!<br>
`aiopyfcm` offers the following features:

- Support for sending FCM messages with Python's Asyncio
- Compatibility with all types of messages supported by FCM
- A user-friendly message sending interface
- Simple handling of Firebase credentials (Json File / Json String / Dict)
- Automatic access token refresh
- Enhanced performance efficiency by maintaining asynchronous sessions based on your needs
- Comprehensive control over all exception scenarios in FCM

## Requirements

If you're planning to use FCM, you’ve probably already looked into it.<br>
As you may know, `google_application_credentials` is required to use FCM.<br>
**Please note that the existing Cloud Messaging API server key will be deprecated on June 20, 2024. It's advisable to obtain a `google_application_credentials` key moving forward.**

To authenticate a service account and allow it access to Firebase services, you'll need to generate a private key file in JSON format.

To generate a private key file for your service account: <br>
1. In the Firebase console, open Settings: <a href="https://console.firebase.google.com/project/_/settings/serviceaccounts/adminsdk?_gl=1*pput8o*_up*MQ..*_ga*MTQ0NTkyMjIzOC4xNzExMTMyOTM2*_ga_CW55HF8NVT*MTcxMTEzMjkzNi4xLjAuMTcxMTEzMjkzNi4wLjAuMA.." target="_blank">Service Accounts. </a>
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.<br>
https://firebase.google.com/docs/cloud-messaging/migrate-v1#provide-credentials-using-adc


## Installation

```console
$ pip install aiopyfcm
```

## Example

You can use it flexibly according to your development situation. (Check it in the `example.py` file)

### Authenticate Google Credentials
This sample shows how to authenticate with Google credentials.

```Python
import json
import aiopyfcm


def sample_authenticate():
    # Initialize the PyFCMAuthenticator object.
    authenticator = aiopyfcm.PyFCMAuthenticator()

    # If you want to authenticate with your Google credentials file path, use the following method.
    authenticator.init_credentials_from_file(
        credentials_file_path="<your_credentials_path>/credentials.json",
        auto_refresh=True  # Default is True
    )

    # If you want to Google credentials in a dictionary format, use the following method.
    credentials_dict = {
        "type": "service_account",
        "project_id": "<PROJECT_ID>",
        "private_key_id": "<PRIVATE_KEY_ID>",
        "private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
        "client_email": "<SERVICE_ACCOUNT_EMAIL>",
        "client_id": "<CLIENT_ID>",
        "auth_uri": "https://accounts.google.com/o/oauth2/auth",
        "token_uri": "https://oauth2.googleapis.com/token",
        "auth_provider_x509_cert_url": "<auth_provider_x509_cert_url>",
        "client_x509_cert_url": "<client_x509_cert_url>",
        "universe_domain": "googleapis.com"
    }
    authenticator.init_credentials(
        credentials=credentials_dict,
        auto_refresh=True  # Default is True
    )

    # If you want to Google credentials as a JSON string, use the following method.
    credentials_dumps = json.dumps(credentials_dict)
    authenticator.init_credentials(
        credentials=credentials_dumps,
        auto_refresh=True  # Default is True
    )

    return authenticator
```

### Refresh Access Token
This sample shows how to refresh the Google Access Token.

```Python
def sample_refresh_access_token():
    # If you set the auto_refresh value to False during the init_credentials process,
    # you will need to manually refresh the access_token.
    authenticator = sample_authenticate()
    authenticator.update_auto_refresh(False)

    # [Refresh the access token]
    # The validity period of a Google AccessToken is approximately 1 hour,
    # so you need to manually refresh it before it expires.
    authenticator.refresh_credentials()

    # If you set the auto_refresh flag to True,
    # the PyFCMAuthenticator will automatically refresh your access_token every 30 minutes.
    authenticator.update_auto_refresh(True)
```

### Send Message
This sample shows how to send a message to FCM.

```Python
import asyncio
import aiopyfcm

async def send_stateful():
    """ 
    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.
    """
    # Get the PyFCMAuthenticator object.
    authenticator = sample_authenticate()

    # Initialize the AioPyFCM object.
    async_pyfcm = aiopyfcm.AioPyFCM(authenticator)

    # Create a message object.
    message = {
        "token": "<FCM_TOKEN>",
        "notification": {
            "title": "Sample Title",
            "body": "Sample Body",
            "image": "https://example.com/sample.jpg"
        }
    }

    # Send the message. (Stateful - Recommended)
    async with async_pyfcm as pyfcm:
        responses = await asyncio.gather(
            pyfcm.send(message),
            pyfcm.send(message),
            pyfcm.send(message),
        )
        print(responses)


async def send_stateless():
    """
    This sample does not maintain the aiohttp asynchronous session,
    so it connects the session every time you send.
    """

    # Get the PyFCMAuthenticator object.
    authenticator = sample_authenticate()

    # Initialize the AioPyFCM object.
    async_pyfcm = aiopyfcm.AioPyFCM(authenticator)

    # Create a message object.
    message = {
        "token": "<FCM_TOKEN>",
        "notification": {
            "title": "Sample Title",
            "body": "Sample Body",
            "image": "https://example.com/sample.jpg"
        }
    }

    # Send the message. (Stateless)
    responses = await asyncio.gather(
        async_pyfcm.send(message),
        async_pyfcm.send(message),
        async_pyfcm.send(message),
    )
    print(responses)
```

## Contributing
The following is a set of guidelines for contributing to `aiopyfcm`. These are mostly guidelines, not rules.<br>
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 `develop` 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.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/837477/aiopyfcm",
    "name": "aiopyfcm",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "aiopyfcm is Python Asynchronous FCM Push Controller",
    "author": "837477",
    "author_email": "8374770@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/61/7c/887a95e2ff468b083fb8c6558c1f8cbf01525df0f288e14c4755abfc36de/aiopyfcm-0.2.1.tar.gz",
    "platform": "any",
    "description": "<br><br><br>\n<p align=\"center\">\n  <a href=\"https://github.com/837477/aiopyfcm\"><img src=\"https://github.com/user-attachments/assets/5825c842-8031-4ada-ab37-e2be74653e69\"></a>\n</p>\n<p align=\"center\">\n    <em>From now on, you will be able to conveniently send FCM messages asynchronously through Python.</em>\n</p>\n<p align=\"center\">\n<a href=\"https://github.com/837477/aiopyfcm/blob/main/LICENSE\" target=\"_blank\">\n    <img src=\"https://img.shields.io/pypi/l/aiopyfcm?color=FEC301\" alt=\"License\">\n</a>\n<a href=\"https://pypi.org/project/aiopyfcm\" target=\"_blank\">\n    <img src=\"https://img.shields.io/pypi/v/aiopyfcm?color=FEC301\" alt=\"Package version\">\n</a>\n<a href=\"https://pypi.org/project/aiopyfcm\" target=\"_blank\">\n    <img src=\"https://img.shields.io/pypi/pyversions/aiopyfcm?color=FEC301\" alt=\"Supported Python versions\">\n</a>\n</p>\n<br><br><br>\n\n---\n\n**Github**: <a href=\"https://github.com/837477/aiopyfcm\" target=\"_blank\">https://github.com/837477/aiopyfcm </a><br>\n**PyPi**: <a href=\"https://pypi.org/project/aiopyfcm\" target=\"_blank\">https://pypi.org/project/aiopyfcm </a>\n\n---\n\nYou can now easily send asynchronous FCM messages!<br>\n`aiopyfcm` offers the following features:\n\n- Support for sending FCM messages with Python's Asyncio\n- Compatibility with all types of messages supported by FCM\n- A user-friendly message sending interface\n- Simple handling of Firebase credentials (Json File / Json String / Dict)\n- Automatic access token refresh\n- Enhanced performance efficiency by maintaining asynchronous sessions based on your needs\n- Comprehensive control over all exception scenarios in FCM\n\n## Requirements\n\nIf you're planning to use FCM, you\u2019ve probably already looked into it.<br>\nAs you may know, `google_application_credentials` is required to use FCM.<br>\n**Please note that the existing Cloud Messaging API server key will be deprecated on June 20, 2024. It's advisable to obtain a `google_application_credentials` key moving forward.**\n\nTo authenticate a service account and allow it access to Firebase services, you'll need to generate a private key file in JSON format.\n\nTo generate a private key file for your service account: <br>\n1. In the Firebase console, open Settings: <a href=\"https://console.firebase.google.com/project/_/settings/serviceaccounts/adminsdk?_gl=1*pput8o*_up*MQ..*_ga*MTQ0NTkyMjIzOC4xNzExMTMyOTM2*_ga_CW55HF8NVT*MTcxMTEzMjkzNi4xLjAuMTcxMTEzMjkzNi4wLjAuMA..\" target=\"_blank\">Service Accounts. </a>\n2. Click Generate New Private Key, then confirm by clicking Generate Key.\n3. Securely store the JSON file containing the key.\n\nFor a more detailed explanation, please refer to the following official document.<br>\nhttps://firebase.google.com/docs/cloud-messaging/migrate-v1#provide-credentials-using-adc\n\n\n## Installation\n\n```console\n$ pip install aiopyfcm\n```\n\n## Example\n\nYou can use it flexibly according to your development situation. (Check it in the `example.py` file)\n\n### Authenticate Google Credentials\nThis sample shows how to authenticate with Google credentials.\n\n```Python\nimport json\nimport aiopyfcm\n\n\ndef sample_authenticate():\n    # Initialize the PyFCMAuthenticator object.\n    authenticator = aiopyfcm.PyFCMAuthenticator()\n\n    # If you want to authenticate with your Google credentials file path, use the following method.\n    authenticator.init_credentials_from_file(\n        credentials_file_path=\"<your_credentials_path>/credentials.json\",\n        auto_refresh=True  # Default is True\n    )\n\n    # If you want to Google credentials in a dictionary format, use the following method.\n    credentials_dict = {\n        \"type\": \"service_account\",\n        \"project_id\": \"<PROJECT_ID>\",\n        \"private_key_id\": \"<PRIVATE_KEY_ID>\",\n        \"private_key\": \"-----BEGIN PRIVATE KEY-----\\n...\\n-----END PRIVATE KEY-----\\n\",\n        \"client_email\": \"<SERVICE_ACCOUNT_EMAIL>\",\n        \"client_id\": \"<CLIENT_ID>\",\n        \"auth_uri\": \"https://accounts.google.com/o/oauth2/auth\",\n        \"token_uri\": \"https://oauth2.googleapis.com/token\",\n        \"auth_provider_x509_cert_url\": \"<auth_provider_x509_cert_url>\",\n        \"client_x509_cert_url\": \"<client_x509_cert_url>\",\n        \"universe_domain\": \"googleapis.com\"\n    }\n    authenticator.init_credentials(\n        credentials=credentials_dict,\n        auto_refresh=True  # Default is True\n    )\n\n    # If you want to Google credentials as a JSON string, use the following method.\n    credentials_dumps = json.dumps(credentials_dict)\n    authenticator.init_credentials(\n        credentials=credentials_dumps,\n        auto_refresh=True  # Default is True\n    )\n\n    return authenticator\n```\n\n### Refresh Access Token\nThis sample shows how to refresh the Google Access Token.\n\n```Python\ndef sample_refresh_access_token():\n    # If you set the auto_refresh value to False during the init_credentials process,\n    # you will need to manually refresh the access_token.\n    authenticator = sample_authenticate()\n    authenticator.update_auto_refresh(False)\n\n    # [Refresh the access token]\n    # The validity period of a Google AccessToken is approximately 1 hour,\n    # so you need to manually refresh it before it expires.\n    authenticator.refresh_credentials()\n\n    # If you set the auto_refresh flag to True,\n    # the PyFCMAuthenticator will automatically refresh your access_token every 30 minutes.\n    authenticator.update_auto_refresh(True)\n```\n\n### Send Message\nThis sample shows how to send a message to FCM.\n\n```Python\nimport asyncio\nimport aiopyfcm\n\nasync def send_stateful():\n    \"\"\" \n    This sample is used when you want to maintain an asynchronous session of aiohttp.\n    You can use resources efficiently by not opening a session every time you send.\n    \"\"\"\n    # Get the PyFCMAuthenticator object.\n    authenticator = sample_authenticate()\n\n    # Initialize the AioPyFCM object.\n    async_pyfcm = aiopyfcm.AioPyFCM(authenticator)\n\n    # Create a message object.\n    message = {\n        \"token\": \"<FCM_TOKEN>\",\n        \"notification\": {\n            \"title\": \"Sample Title\",\n            \"body\": \"Sample Body\",\n            \"image\": \"https://example.com/sample.jpg\"\n        }\n    }\n\n    # Send the message. (Stateful - Recommended)\n    async with async_pyfcm as pyfcm:\n        responses = await asyncio.gather(\n            pyfcm.send(message),\n            pyfcm.send(message),\n            pyfcm.send(message),\n        )\n        print(responses)\n\n\nasync def send_stateless():\n    \"\"\"\n    This sample does not maintain the aiohttp asynchronous session,\n    so it connects the session every time you send.\n    \"\"\"\n\n    # Get the PyFCMAuthenticator object.\n    authenticator = sample_authenticate()\n\n    # Initialize the AioPyFCM object.\n    async_pyfcm = aiopyfcm.AioPyFCM(authenticator)\n\n    # Create a message object.\n    message = {\n        \"token\": \"<FCM_TOKEN>\",\n        \"notification\": {\n            \"title\": \"Sample Title\",\n            \"body\": \"Sample Body\",\n            \"image\": \"https://example.com/sample.jpg\"\n        }\n    }\n\n    # Send the message. (Stateless)\n    responses = await asyncio.gather(\n        async_pyfcm.send(message),\n        async_pyfcm.send(message),\n        async_pyfcm.send(message),\n    )\n    print(responses)\n```\n\n## Contributing\nThe following is a set of guidelines for contributing to `aiopyfcm`. These are mostly guidelines, not rules.<br>\nUse your best judgment, and feel free to propose changes to this document in a pull request.\n\n1. Please create a branch in this format, **`<Issue Number>-<Some name>`**\n2. Open a terminal and navigate to your project path. And enter this.\n   **`git config --global commit.template .gitmessage.txt`**\n3. You can use the template, with `git commit` through vi. **Not** `git commit -m`\n4. If you want to merge your work, please pull request to the `develop` branch.\n5. Enjoy contributing!\n\nIf you have any other opinions, please feel free to suggest \ud83d\ude00\n\n## License\n\nThis project is licensed under the terms of the MIT license.\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "aiopyfcm is Python Asynchronous FCM Push Controller",
    "version": "0.2.1",
    "project_urls": {
        "Homepage": "https://github.com/837477/aiopyfcm"
    },
    "split_keywords": [
        "aiopyfcm",
        "is",
        "python",
        "asynchronous",
        "fcm",
        "push",
        "controller"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7f697c2a91ca2b03cbc9176e31beda9eb7c7d28ea0cfc6ef31ceeb287935fc41",
                "md5": "2e54bfcc700bc1fcb578efa66037f04e",
                "sha256": "6827e72d94f9f9320da953a02afdb5214aee12c9c76afd14af27ae9a8b31b17c"
            },
            "downloads": -1,
            "filename": "aiopyfcm-0.2.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2e54bfcc700bc1fcb578efa66037f04e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 9236,
            "upload_time": "2024-10-25T08:56:06",
            "upload_time_iso_8601": "2024-10-25T08:56:06.823041Z",
            "url": "https://files.pythonhosted.org/packages/7f/69/7c2a91ca2b03cbc9176e31beda9eb7c7d28ea0cfc6ef31ceeb287935fc41/aiopyfcm-0.2.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "617c887a95e2ff468b083fb8c6558c1f8cbf01525df0f288e14c4755abfc36de",
                "md5": "c70b7b93ed55262cff5015dc4ad74266",
                "sha256": "abc367e81c7d16bd88a7368b84c504a5b08cd7b2872b796ef71277096d40bcfb"
            },
            "downloads": -1,
            "filename": "aiopyfcm-0.2.1.tar.gz",
            "has_sig": false,
            "md5_digest": "c70b7b93ed55262cff5015dc4ad74266",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 11051,
            "upload_time": "2024-10-25T08:56:07",
            "upload_time_iso_8601": "2024-10-25T08:56:07.650518Z",
            "url": "https://files.pythonhosted.org/packages/61/7c/887a95e2ff468b083fb8c6558c1f8cbf01525df0f288e14c4755abfc36de/aiopyfcm-0.2.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-25 08:56:07",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "837477",
    "github_project": "aiopyfcm",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "aiopyfcm"
}
        
Elapsed time: 0.53910s