This repo is maintained by Expo's awesome community 😍! So, if you have problems with the code in this repository, please feel free to open an issue, and make a PR. Thanks!
pip install exponent_server_sdk
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.
fromexponent_server_sdkimport (
DeviceNotRegisteredError,
PushClient,
PushMessage,
PushServerError,
PushTicketError,
)
importosimportrequestsfromrequests.exceptionsimportConnectionError, HTTPError# Optionally providing an access token within a session if you have enabled push securitysession=requests.Session()
session.headers.update(
{
"Authorization": f"Bearer {os.getenv('EXPO_TOKEN')}",
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"content-type": "application/json",
}
)
# Basic arguments. You should extend this function with the push features you# want to use, or simply pass in a `PushMessage` object.defsend_push_message(token, message, extra=None):
try:
response=PushClient(session=session).publish(
PushMessage(to=token,
body=message,
data=extra))
exceptPushServerErrorasexc:
# Encountered some likely formatting/validation error.rollbar.report_exc_info(
extra_data={
'token': token,
'message': message,
'extra': extra,
'errors': exc.errors,
'response_data': exc.response_data,
})
raiseexcept (ConnectionError, HTTPError) asexc:
# Encountered some Connection or HTTP error - retry a few times in# case it is transient.rollbar.report_exc_info(
extra_data={'token': token, 'message': message, 'extra': extra})
raiseself.retry(exc=exc)
try:
# We got a response back, but we don't know whether it's an error yet.# This call raises errors so we can handle them with normal exception# flows.response.validate_response()
exceptDeviceNotRegisteredError:
# Mark the push token as inactivefromnotifications.modelsimportPushTokenPushToken.objects.filter(token=token).update(active=False)
exceptPushTicketErrorasexc:
# Encountered some other per-notification error.rollbar.report_exc_info(
extra_data={
'token': token,
'message': message,
'extra': extra,
'push_response': exc.push_response._asdict(),
})
raiseself.retry(exc=exc)