exponent-server-sdk-python
This repo is maintained by Expo's awesome community :heart_eyes:! So, if you have problems with the code in this repository, please feel free to open an issue, and make a PR. Thanks!
Installation
pip install exponent_server_sdk
Usage
Use to send push notifications to Exponent Experiences from a Python server.
Full documentation on the API is available if you want to dive into the details.
Here's an example on how to use this with retries and reporting via pyrollbar.
from exponent_server_sdk import (
DeviceNotRegisteredError,
PushClient,
PushMessage,
PushServerError,
PushTicketError,
)
import os
import requests
from requests.exceptions import ConnectionError, HTTPError
session = requests.Session()
session.headers.update(
{
"Authorization": f"Bearer {os.getenv('EXPO_TOKEN')}",
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"content-type": "application/json",
}
)
def send_push_message(token, message, extra=None):
try:
response = PushClient(session=session).publish(
PushMessage(to=token,
body=message,
data=extra))
except PushServerError as exc:
rollbar.report_exc_info(
extra_data={
'token': token,
'message': message,
'extra': extra,
'errors': exc.errors,
'response_data': exc.response_data,
})
raise
except (ConnectionError, HTTPError) as exc:
rollbar.report_exc_info(
extra_data={'token': token, 'message': message, 'extra': extra})
raise self.retry(exc=exc)
try:
response.validate_response()
except DeviceNotRegisteredError:
from notifications.models import PushToken
PushToken.objects.filter(token=token).update(active=False)
except PushTicketError as exc:
rollbar.report_exc_info(
extra_data={
'token': token,
'message': message,
'extra': extra,
'push_response': exc.push_response._asdict(),
})
raise self.retry(exc=exc)