# exponent-server-sdk-python-async
This repo is made for the async version of the [exponent-server-sdk-python](https://github.com/expo-community/expo-server-sdk-python) made by [Bahadır Araz](https://github.com/bahadiraraz).
## Installation
```
pip install exponent-server-sdk-async
```
## Usage
Use to send push notifications to Exponent Experiences from a Python server.
[Full documentation](https://docs.expo.dev/push-notifications/sending-notifications/#http2-api) on the API is available if you want to dive into the details.
```python
import asyncio
import os
import httpx
from loguru import logger
from dotenv import load_dotenv
from exponent_server_sdk_async import (
AsyncPushClient,
PushMessage,
DeviceNotRegisteredError,
PushTicketError,
PushServerError
)
# Logger setup
logger.add("debug.log", rotation="1 week", compression="zip")
load_dotenv()
logger.debug("Using Expo Token: {}", os.getenv('EXPO_TOKEN'))
async def send_push_message_to_multiple(tokens, message, extra, title, navigate_to=None):
async with httpx.AsyncClient(headers={
"Authorization": f"Bearer {os.getenv('EXPO_TOKEN')}",
"Accept": "application/json",
"Content-Type": "application/json",
}) as session:
push_client = AsyncPushClient(session=session)
push_messages = [
PushMessage(
to=token,
body=message,
data={'extra': extra, 'navigateTo': navigate_to},
badge=1,
title=title,
sound="default"
) for token in tokens
]
try:
push_tickets = await push_client.publish_multiple(push_messages)
for push_ticket in push_tickets:
if push_ticket.is_success():
logger.info("Notification sent successfully to: {}", push_ticket.push_message.to)
else:
logger.warning("Failed to send notification to: {}, Error: {}", push_ticket.push_message.to, push_ticket.message)
except PushServerError as exc:
logger.error("Push server error: {}", exc)
raise
except DeviceNotRegisteredError as exc:
logger.error("Device not registered error: {}", exc)
raise
except PushTicketError as exc:
logger.error("Push ticket error: {}", exc)
raise
async def main():
tokens = [
"ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]",
"ExponentPushToken[yyyyyyyyyyyyyyyyyyyyyy]",
"ExponentPushToken[zzzzzzzzzzzzzzzzzzzzzz]"
]
extra_parameters = {"key": "value"}
# Send a simple push message to multiple recipients
await send_push_message_to_multiple(tokens, "Hello Multi-User", extra_parameters, "Greeting")
# Navigate to home data for multiple recipients
await send_push_message_to_multiple(tokens, "Navigate to home", extra_parameters, "Home", "/(tabs)/(home)")
if __name__ == "__main__":
asyncio.run(main())
```
Raw data
{
"_id": null,
"home_page": null,
"name": "exponent-server-sdk-async",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.12",
"maintainer_email": null,
"keywords": "expo, sdk, async, push notifications",
"author": null,
"author_email": "=?utf-8?q?Bahad=C4=B1r_Araz?= <bahadiraraz@protonmail.com>, Expo Team <exponent.team@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/42/ed/8c3120e29553bf1d669ab9c04511011c92924bcc7721dcd337c7d809f7c1/exponent_server_sdk_async-2.1.7.tar.gz",
"platform": null,
"description": "# exponent-server-sdk-python-async\n\nThis repo is made for the async version of the [exponent-server-sdk-python](https://github.com/expo-community/expo-server-sdk-python) made by [Bahad\u0131r Araz](https://github.com/bahadiraraz).\n\n## Installation\n\n```\npip install exponent-server-sdk-async\n```\n\n## Usage\nUse to send push notifications to Exponent Experiences from a Python server.\n[Full documentation](https://docs.expo.dev/push-notifications/sending-notifications/#http2-api) on the API is available if you want to dive into the details.\n```python\nimport asyncio\nimport os\nimport httpx\nfrom loguru import logger\nfrom dotenv import load_dotenv\nfrom exponent_server_sdk_async import (\n AsyncPushClient,\n PushMessage,\n DeviceNotRegisteredError,\n PushTicketError,\n PushServerError\n)\n\n# Logger setup\nlogger.add(\"debug.log\", rotation=\"1 week\", compression=\"zip\")\nload_dotenv()\nlogger.debug(\"Using Expo Token: {}\", os.getenv('EXPO_TOKEN'))\n\n\nasync def send_push_message_to_multiple(tokens, message, extra, title, navigate_to=None):\n async with httpx.AsyncClient(headers={\n \"Authorization\": f\"Bearer {os.getenv('EXPO_TOKEN')}\",\n \"Accept\": \"application/json\",\n \"Content-Type\": \"application/json\",\n }) as session:\n push_client = AsyncPushClient(session=session)\n push_messages = [\n PushMessage(\n to=token,\n body=message,\n data={'extra': extra, 'navigateTo': navigate_to},\n badge=1,\n title=title,\n sound=\"default\"\n ) for token in tokens\n ]\n try:\n push_tickets = await push_client.publish_multiple(push_messages)\n for push_ticket in push_tickets:\n if push_ticket.is_success():\n logger.info(\"Notification sent successfully to: {}\", push_ticket.push_message.to)\n else:\n logger.warning(\"Failed to send notification to: {}, Error: {}\", push_ticket.push_message.to, push_ticket.message)\n except PushServerError as exc:\n logger.error(\"Push server error: {}\", exc)\n raise\n except DeviceNotRegisteredError as exc:\n logger.error(\"Device not registered error: {}\", exc)\n raise\n except PushTicketError as exc:\n logger.error(\"Push ticket error: {}\", exc)\n raise\n\n\nasync def main():\n tokens = [\n \"ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]\",\n \"ExponentPushToken[yyyyyyyyyyyyyyyyyyyyyy]\",\n \"ExponentPushToken[zzzzzzzzzzzzzzzzzzzzzz]\"\n ]\n extra_parameters = {\"key\": \"value\"}\n\n # Send a simple push message to multiple recipients\n await send_push_message_to_multiple(tokens, \"Hello Multi-User\", extra_parameters, \"Greeting\")\n\n # Navigate to home data for multiple recipients\n await send_push_message_to_multiple(tokens, \"Navigate to home\", extra_parameters, \"Home\", \"/(tabs)/(home)\")\n\n\nif __name__ == \"__main__\":\n asyncio.run(main())\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Expo Server SDK for Python async",
"version": "2.1.7",
"project_urls": {
"Homepage": "https://github.com/Learnity-AI/expo-server-sdk-python-async"
},
"split_keywords": [
"expo",
" sdk",
" async",
" push notifications"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "b7467e9227c3ee902c5feb71267f32d2a27f33c971f00bb5b6f52442c0eb99d3",
"md5": "9851da2ef9ed7f5853f32b2d5b384e9a",
"sha256": "23f33cb49d53bc925b3538b2eda4b9ab4655bd825481a1fd4effd6b9d022f2a0"
},
"downloads": -1,
"filename": "exponent_server_sdk_async-2.1.7-py3-none-any.whl",
"has_sig": false,
"md5_digest": "9851da2ef9ed7f5853f32b2d5b384e9a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.12",
"size": 8472,
"upload_time": "2024-08-13T12:24:56",
"upload_time_iso_8601": "2024-08-13T12:24:56.714744Z",
"url": "https://files.pythonhosted.org/packages/b7/46/7e9227c3ee902c5feb71267f32d2a27f33c971f00bb5b6f52442c0eb99d3/exponent_server_sdk_async-2.1.7-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "42ed8c3120e29553bf1d669ab9c04511011c92924bcc7721dcd337c7d809f7c1",
"md5": "ed836257cb4094255c8075402047d5c8",
"sha256": "6e8e213009b4146336336a51a54e089072afffc8c5b01233b00c8030c97e9e94"
},
"downloads": -1,
"filename": "exponent_server_sdk_async-2.1.7.tar.gz",
"has_sig": false,
"md5_digest": "ed836257cb4094255c8075402047d5c8",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.12",
"size": 6787,
"upload_time": "2024-08-13T12:24:58",
"upload_time_iso_8601": "2024-08-13T12:24:58.665155Z",
"url": "https://files.pythonhosted.org/packages/42/ed/8c3120e29553bf1d669ab9c04511011c92924bcc7721dcd337c7d809f7c1/exponent_server_sdk_async-2.1.7.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-13 12:24:58",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Learnity-AI",
"github_project": "expo-server-sdk-python-async",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "exponent-server-sdk-async"
}