-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update documentation with publish_multiple example #61
Comments
Here's something if anyone wants to take use of. I think the trick here to match push tickets to tokens is to keep a parallel list of the same order, since the docs mention:
def push_expo_notifications(user, title, body):
messages = []
token_ids = []
for device in user.devices.filter(active=True):
messages.append(
PushMessage(
to=device.expo_token,
title=title,
body=body
)
)
token_ids.append(device.id)
try:
responses = PushClient().publish_multiple(messages)
except PushServerError as exc:
# Encountered some likely formatting/validation error.
raise
except (ConnectionError) as exc:
# Encountered some Connection or HTTP error.
raise
for index, response in enumerate(responses):
try:
response.validate_response()
except DeviceNotRegisteredError:
# Mark the push token as inactive
UserDevice.objects.filter(id=token_ids[index]).update(active=False)
# Or use the token from push_message - NOTE that `to` field may be a list of recipients according to the API
UserDevice.objects.filter(expo_token=response.push_message.to).update(active=False)
except PushTicketError as exc:
# Encountered some other per-notification error.
raise |
@sergioisidoro doesn't this just use the order to get the device token? Isn't the device token already present in each |
I think you're right. I checked that publish returned push tickets, and completely missed that the push ticket already contained Ironically, it uses the exact same method I'm using here, using the index of the response to match the push message to the push receipt 😅 expo-server-sdk-python/exponent_server_sdk/__init__.py Lines 393 to 396 in f7f1317
I'll update the example, and my own code. Thanks @YPCrumble ! Just an important caveat, that with the PRs that enable multiple senders, the
|
I think your insight that the responses are in the same order is what I needed for my use case. If I publish ten notifications, and only one fails for whatever reason, I want to retry that ticket only. |
The
README.md
is pretty clear on how to handle the errors when sending a single push notification, however, most of the times we will be sending the same push notification to a bunch of devices, so in that case, I think it would be greatly valuable an example of how to handle that case.The text was updated successfully, but these errors were encountered: