[This post was originally posted as a Stackoverflow question. This is my content and my effort for solving this challenge.]
I have coded Push Notifications (APNS, Apple Push Notification Service) for an iPhone App with PHP. Everything is working fine while no app is deleted from a device. I use the Feedback Service from Apple to get the deleted devices. If I get devices I mark them as INACTIVE in my database to send no further push notification to those devices.
The problem is that Apple is slow. If you delete the app from your device a request is send to Apple and I collect the devices by using the Feedback Service. But Apple is not forwarding the inactivated devices instantly so I get the following error because I can’t send push notifications to an inactive device:
PHP Warning: fwrite(): SSL: Broken pipe in apns.php on line 155
My Feedback Service script runs every 5 minutes but the update from Apple comes every 10-15 minutes. If I send a push notification within the 10-15 minutes the above error appears because in the database the device is still marked as ACTIVE and on the Apple server the device is marked as INACTIVE.
There is no problem to connect to the Feedback Service or to the Push Service but I don’t always get devices from the Feedback Service.
Update, 2011-07-29 08:42: Solution
Answer from Apple.com Developer Forum:
Your server needs to detect disconnections and reconnect if necessary. Nothing is “instant” when networking is involved; there’s always some latency and code needs to take that into account. Also, consider using the enhanced binary interface so you can check the return response and know why the connection was dropped. The connection can also be dropped as a result of TCP keep-alive, which is outside of Apple’s control.
So I’m gonna set all devices to INACTIVE if the server detects a disconnection.
When Apple has any problem with a device they close the connection. You don’t get any error message or something like that. If you get an SSL error on a device you assume that this device is not active anymore. So you must
- turn it inactive and
- you must reconnect to the Apple server.
While you sending thousands of push notifications you maybe have hundreds of SSL errors (reconnects). I know, this system is not perfect. And I don’t know why Apple has it made so tricky.
You need to manage the IDs which give you an SSL error. You must skip that ID. Don’t try to resend the message to that ID. It won’t work. You also must run the feedback service with an interval of 5 to 15 minutes. But the feedback service is not instant. Like the Apple Developer Forum said: Nothing is “instant” when networking is involved. But that’s the main problem: the feedback service will be updated every (for example, I don’t know the exact time) 15 minutes. So on your side you run your Feedback Service script every 15 minutes. But within the 15 minutes, when the Feedback Service script is not running, people also can deinstall your App. So you send a push notification and you get an error because any user has deinstalled your app within the 15 minutes you must deal with the sending script because the Feedback Service script has no effect.