From now on, easily send FCM (Firebase Cloud Messages) through "aiopyfcm"
Documnets: https://github.com/837477/aiopyfcm
You can now easily send asynchronous FCM messages.
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.
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:
- In the Firebase console, open Settings > Service Accounts.
- Click Generate New Private Key, then confirm by clicking Generate Key.
- 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
$ pip install aiopyfcm
You can use it flexibly according to your development situation.
- You can check it in the
example.py
file.
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()
# Initialize the credentials from the JSON file. (Be required)
aiopyfcm_module.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>.
"""
credentials_dict = {
"type": "service_account",
"project_id": "837477-Sample",
"private_key_id": "XXXX...",
"private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
"client_email": "firebase-adminsdk-XXXX...",
"client_id": "XXXX...",
"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...",
"universe_domain": "googleapis.com"
}
credentials_dumps = json.dumps(credentials_dict)
# Initialize the AioPyFCM object.
aiopyfcm_module = aiopyfcm.AioPyFCM()
# Initialize the credentials from the <Dict> or <JSON string>. (Be required)
aiopyfcm_module.init_credentials(
credentials=credentials_dict or credentials_dumps,
auto_refresh=True # Default is True
)
return aiopyfcm_module
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.
"""
# Initialize the AioPyFCM object.
aiopyfcm_module = await credentials_sample_1()
# Create a message object.
message = {
"notification": {
"title": "Sample Title",
"body": "Sample Body",
"image": "https://example.com/sample.jpg"
}
}
# Send the message. (Stateful - Recommended)
async with aiopyfcm_module as async_fcm:
responses = await asyncio.gather(
async_fcm.send(message),
async_fcm.send(message),
async_fcm.send(message),
)
print(responses)
async def send_stateless_sample():
"""
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.
"""
# Initialize the AioPyFCM object.
aiopyfcm_module = await credentials_sample_1()
# 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(
aiopyfcm_module.send(message),
aiopyfcm_module.send(message),
aiopyfcm_module.send(message),
)
print(responses)
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.
- Please create a branch in this format,
<Issue Number>-<Some name>
- Open a terminal and navigate to your project path. And enter this.
git config --global commit.template .gitmessage.txt
- You can use the template, with
git commit
through vi. Notgit commit -m
- If you want to merge your work, please pull request to the
dev
branch. - Enjoy contributing!
If you have any other opinions, please feel free to suggest 😀
This project is licensed under the terms of the MIT license.