I am working on a simple web server that sends push notification to my iPhone app.
I’ve tried using existing Python packages, but they all suffer from this problem:
When an invalid token is sent, Apple will send back a error message and close the socket. I need to send a large number of push notifications, but if one of them has an invalid token, the rest won’t be delivered to Apple.
So currently, my code looks something like this:
for m in messages:
socket.send(m)
But when the first of the messages has invalid token, so Apple closes the socket as soon as it receives the first message, socket still sends all the messages without any error.
I can’t afford to do a recv after each message with a timeout, since that will take too long. What can I do to know when the socket is closed and open a new one?
I learned that there is no way at TCP level that I can find out what packets were sent. I decided to just send all the messages and then wait (with timeout) for any error messages from Apple. If there is any error, repeat sending the ones that failed.
I decided to go with this, since getting an invalid token should be a rare event, and most of the time all the notifications will go through.