I understand that in case i'm sending invalid tokens, apple forcefully closes the connection. I also have a feedback loop, but the failing tokens never appear on feedback loop since they are not even accepted by apple.

Is there any algorithm or method to know, why the device token is "invalid" ?

1 Answer
1

Assuming you got all your device tokens from Apple and didn't create dummy tokens, the most likely cause for invalid tokens is a DB that has both production tokens and sandbox tokens.

How to find which tokens are invalid :

You should use the enhanced APN format, in which you specify the message ID. After each message you send to Apple, you should try reading from the socket after setting the timeout to a large enough value. Apple will send you error responses for the invalid tokens, which specify the message ID. This will let you find the invalid tokens and remove them from your DB.
Note that you should only use this method (reading responses after each message with a long timeout) in order to clean your DB. After your DB is clean from invalid tokens you don't want to use it, since it will make the notifications delivery very slow.