Skip to content

Commit

Permalink
[deploy] AioPyFCM Version 0.2.0
Browse files Browse the repository at this point in the history
[deploy] AioPyFCM Version 0.2.0
  • Loading branch information
837477 authored Oct 25, 2024
2 parents 0a47bb9 + f7163e3 commit c655dbc
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 164 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
- main

env:
TAG: 0.1.0
TAG: 0.2.0

jobs:
pypi-publish:
Expand Down
161 changes: 91 additions & 70 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<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, easily send FCM (Firebase Cloud Messages) through "aiopyfcm"</em>
<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">
Expand All @@ -20,32 +20,32 @@

---

**Documnets**: <a href="https://github.com/837477/aiopyfcm" target="_blank">https://github.com/837477/aiopyfcm </a>
**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` 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 / 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.
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 are planning to use FCM now, I think you have already studied FCM.<br>
As you know, `google_application_credentials` is required to use FCM.<br>
**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.**
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 authorize it to access Firebase services, you must generate a private key file in JSON format.
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>
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.

Expand All @@ -61,74 +61,93 @@ $ pip install aiopyfcm

## Example

You can use it flexibly according to your development situation.
* You can check it in the `example.py` file.
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 asyncio
import aiopyfcm


async def credentials_sample_1():
"""
This example demonstrates an authorization method using a JSON file.
"""

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

# Initialize the credentials from the JSON file. (Be required)
aiopyfcm_module.init_credentials_from_file(
# 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
)

return aiopyfcm_module


async def credentials_sample_2():
"""
This example demonstrates how to authenticate by converting the contents of a JSON file into a <Dict> or <String>.
"""
# If you want to Google credentials in a dictionary format, use the following method.
credentials_dict = {
"type": "service_account",
"project_id": "837477-Sample",
"private_key_id": "XXXX...",
"project_id": "<PROJECT_ID>",
"private_key_id": "<PRIVATE_KEY_ID>",
"private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
"client_email": "firebase-adminsdk-XXXX...",
"client_id": "XXXX...",
"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": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-XXXX...",
"auth_provider_x509_cert_url": "<auth_provider_x509_cert_url>",
"client_x509_cert_url": "<client_x509_cert_url>",
"universe_domain": "googleapis.com"
}
credentials_dumps = json.dumps(credentials_dict)

# Initialize the AioPyFCM object.
aiopyfcm_module = aiopyfcm.AioPyFCM()
authenticator.init_credentials(
credentials=credentials_dict,
auto_refresh=True # Default is True
)

# Initialize the credentials from the <Dict> or <JSON string>. (Be required)
aiopyfcm_module.init_credentials(
credentials=credentials_dict or credentials_dumps,
# 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 aiopyfcm_module
return authenticator
```

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

async def send_stateful_sample():
"""
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.
"""
```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 is used when you want to maintain an asynchronous session of aiohttp.<br>
You can use resources efficiently by not opening a session every time you send.

```Python
import asyncio
import aiopyfcm

async def send_stateful():
# Get the PyFCMAuthenticator object.
authenticator = sample_authenticate()

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

# Create a message object.
message = {
"token": "<FCM_TOKEN>",
"notification": {
"title": "Sample Title",
"body": "Sample Body",
Expand All @@ -137,24 +156,26 @@ async def send_stateful_sample():
}

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


async def send_stateless_sample():
async def send_stateless():
"""
This sample uses the AsyncPyFCM object by declaring it.
This method does not maintain the aiohttp asynchronous session,
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.
aiopyfcm_module = await credentials_sample_1()
async_pyfcm = aiopyfcm.AioPyFCM(authenticator)

# Create a message object.
message = {
Expand All @@ -168,22 +189,22 @@ async def send_stateless_sample():

# Send the message. (Stateless)
responses = await asyncio.gather(
aiopyfcm_module.send(message),
aiopyfcm_module.send(message),
aiopyfcm_module.send(message),
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. Use your best judgment, and feel free to propose changes to this document in a pull request.
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 `dev` branch.
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 😀
Expand Down
4 changes: 3 additions & 1 deletion aiopyfcm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

from .message import Message
from .authenticator import PyFCMAuthenticator
from .aiopyfcm import AioPyFCM
from .errors import (
InvalidCredentialsError,
Expand All @@ -21,14 +22,15 @@
__TITLE__ = 'aiopyfcm'
__SUMMARY__ = 'aiopyfcm is Python Asynchronous FCM Push Controller'
__URL__ = 'https://github.com/837477/aiopyfcm'
__VERSION__ = "0.1.0"
__VERSION__ = "0.2.0"
__AUTHOR__ = '837477'
__EMAIL__ = '[email protected]'
__LICENSE__ = 'MIT License'


__all__ = [
'Message',
'PyFCMAuthenticator',
'AioPyFCM',
'InvalidCredentialsError',
'AioPyFCMError',
Expand Down
Loading

0 comments on commit c655dbc

Please sign in to comment.