You identify such invalid device tokens using the extended message format to send Apple notifications and try to read from the socket to get error responses. An invalid device token will cause Apple to respond to the error before closing it. The difficult part allows you to read the error response before closing the socket.
This Technical Note provides a good explanation.
Eran source share