This client is for interacting with the Async Notification Service. It provides methods for managing device tokens and sending notifications to users, platforms, or specific tokens.
To install the required dependencies, run:
pip install -r requirements.txtEnsure that NOTIFICATION_API_URL and NOTIFICATION_API_KEY are defined in a private.py file:
NOTIFICATION_API_URL = "https://your-notification-service-url"
NOTIFICATION_API_KEY = "your-api-key"from notification_service_client import NotificationServiceClient
client = NotificationServiceClient()Registers a new device token.
Parameters:
token(str): The device token.platform(Platform): The platform of the device ("ios","android","web").session_id(Optional[str]): The session ID.user_id(Optional[str]): The user ID.username(Optional[str]): The username.first_name(Optional[str]): The first name.last_name(Optional[str]): The last name.expires_at(Optional[Union[datetime, str]]): The expiration time of the token.
Returns: JsonDict
Caveat: Ensure expires_at is either a datetime object or an ISO-8601 string.
Example:
response = client.register_token(
token="your_device_token",
platform="ios",
session_id="your_session_id",
user_id="your_user_id",
username="your_username",
first_name="YourFirstName",
last_name="YourLastName",
expires_at=datetime.now() + timedelta(days=30)
)
print(response)Updates an existing device token.
Parameters:
old_token(str): The old device token.new_token(str): The new device token.user_id(Optional[str]): The user ID.
Returns: JsonDict
Example:
response = client.update_token(
old_token="your_old_device_token",
new_token="your_new_device_token",
user_id="your_user_id"
)
print(response)Removes a specific device token.
Parameters:
token(str): The device token to be removed.
Returns: JsonDict
Example:
response = client.delete_token(token="your_device_token")
print(response)Removes all tokens for a specific user.
Parameters:
user_id(str): The user ID.
Returns: JsonDict
Example:
response = client.delete_user_tokens(user_id="your_user_id")
print(response)Removes the token associated with a specific session ID.
Parameters:
session_id(str): The session ID.
Returns: JsonDict
Example:
response = client.delete_session_token(session_id="your_session_id")
print(response)Broadcasts a notification to all registered devices.
Parameters:
title(str): The title of the notification.body(str): The body of the notification.data(Optional[JsonDict]): Additional data to send with the notification.
Returns: JsonDict
Example:
response = client.broadcast_notification(
title="Important Update",
body="We have an important update for you.",
data={"key": "value"}
)
print(response)Sends a notification to specific users.
Parameters:
user_ids(List[str]): A list of user IDs.title(str): The title of the notification.body(str): The body of the notification.data(Optional[JsonDict]): Additional data to send with the notification.
Returns: JsonDict
Caveat: user_ids must be a list of strings.
Example:
response = client.send_to_users(
user_ids=["user_id_1", "user_id_2"],
title="Personalized Message",
body="Hello, we have a special offer for you.",
data={"offer": "special_discount"}
)
print(response)Sends a notification to specific platforms.
Parameters:
platforms(List[Platform]): A list of platforms ("ios","android","web").title(str): The title of the notification.body(str): The body of the notification.data(Optional[JsonDict]): Additional data to send with the notification.
Returns: JsonDict
Caveat: platforms must be a list of strings.
Example:
response = client.send_to_platforms(
platforms=["ios", "android"],
title="Platform Specific Update",
body="This update is for iOS and Android users.",
data={"update": "version_1.2.3"}
)
print(response)Sends a notification to specific device tokens.
Parameters:
tokens(List[str]): A list of device tokens.title(str): The title of the notification.body(str): The body of the notification.data(Optional[JsonDict]): Additional data to send with the notification.
Returns: JsonDict
Caveat: tokens must be a list of strings.
Example:
response = client.send_to_tokens(
tokens=["device_token_1", "device_token_2"],
title="Direct Message",
body="This is a direct message to your device.",
data={"info": "additional_data"}
)
print(response)Checks the API health status.
Returns: JsonDict
Example:
response = client.health_check()
print(response)