APNS Apple Push Notification token goes stale and stops working - iphone

Does anyone know what could cause a Apple Push Notification token to go stale? After some time the Push token no longer works for a given device, while other tokens work. If I delete that account for that device and create a new account for that device and receive a new Push token from Apple, then the new Push token works fine.
Edit 1 - This post claims that push notification connections fail after 200 notifications and the connection has to be restarted. Has anyone else had this experience?
Could the Push token go stale because Apple saw the Push connection for that device being brought up and down too often in a given period of time? If so, is there some way to programmatically know when a Push token has gone stale?

Your application should register for push notifications every time it is launched. This way you will get the current device token every time it is launched, and if it changes (you can store the old copy in the app in order to know that), you can send it to your server.
You can see a related answer with quotes from the APNS guide here.

Related

Push notification data when app is not running?

When the app is not running and user receives, say 5 push notifications, are those push notifications saved somewhere? Or is that data gone? I need to access all 5 push notifications when the app runs the next time.
To clarify, I already understand that you can access the push notification that caused the app to run. What I'm asking is to get all push notifications since the app got terminated.
The APNS service will only retain the most recent pushed message to a device - assuming there is still at least one other app installed AND the user allows push notifications for it, then this one stored message will be delivered the next time the user has an Internet connection.
For better information on the quality of service that Apple has implemented for the APNS service, see my other answer here:
Clarification on Apple APNS

Push notificaton without device token

My application is in Appstore now and ı cant find out device tokens.is it possible to send push notification without device token
Simple answer: No, sorry.
Longer answer: No, you'll need to submit a new binary which collects the device tokens and submits them to your server.
You really should have sorted all of this out during testing with the sandbox before releasing to the AppStore!
The device will send your server a token to indicate that it wants to receive notifications. It is up to your server to store these tokens and send the appropriate messages to the devices as and when required.
Your server should also respond to people requesting to not have notifications any further and also for tokens that have become "inactive".

How to determine Push notification status programmatically?

In my app, for first time push notification registration, I call didRegisterForRemoteNotificationsWithDeviceToken and save the device token in persistence as well as update my server list for device token. Now afterwards if somebody turns the push notification settings off from iPhone Settings how can I determine it from my app so that I can remove the device token from server as well. I know APNS provides a feedback list, but other than that is there a way to determine it in App programmatically? Thanks for any help!
I believe you do not want to manage tokens this way.
Your app should always be asking Apple for an APNs token. You should always then send that token to your own server, likely associating the token with your user (if you have one). You do this because the token could change, so you want to make sure you always have up-to-date tokens.
The Feedback service will tell you (actually, you poll it at some interval of your choosing) which tokens have become invalid. At this point, you remove the tokens from your server-side database. To be clear, you need a server-side process that polls Apple's feedback service and then updates your server-side database.
You will not receive feedback about invalid tokens until you try to send a notification using the token. The notification will (I believe) be accepted by Apple when you send it, but when Apple discovers it's for an invalid token, the message is dropped, and the token is added to your feedback.
Now, if the user of your app accepts push notifications when your app first asks about it, but later turns off notifications via the Settings app for your app, you will not get any feedback about it. What happens, near as I can tell, is that any notification you send to that device will be sent to the device, but the OS drops it, honoring the user's ultimate choice in the Settings app for your app and notifications.
Finally, there is an API you can call in your app to get a bitmask of which kinds of notifications are enabled for your app on the device. Here's a method I wrote for this purpose; adjust as needed:
+(BOOL)acceptsPushNotifications
{
UIRemoteNotificationType mask = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
return (mask & UIRemoteNotificationTypeAlert) == UIRemoteNotificationTypeAlert;
}
But I would not recommend using this to decide if you app should tell your server to delete the token from your database. That's not how the whole APNs system is intended to work... I believe.

Why registering for push notifications every time a user launch an app?

In the Apple documentation you can find the following sentence :
An application should register every time it launches and give its provider the current token. It calls registerForRemoteNotificationTypes: to kick off the registration process.
So when I implemented the push notification in my app I had to register the device, and I did what they said in that documentation: registering every time a user launch my app.
The token that I receive from the APNS is always the same for a given user.
My question is: why do I need to register everytime if the APNS gives me always the same token?
I read somewhere than a token can change if a user swipe his iPhone or the app. Is it the only case?
Thank you !
The token that I receive from the APNS is always the same for a given user.
Except it isn't, basically because there's nothing you can hang onto as being "a user" in the iPhone setup. The device token is always the same for each app for each device. So different apps on the same device get different tokens. The same app on two different devices gets two different tokens.
The crucial thing to note, and this is mentioned in the APNS guide, is that a user may back up their apps, settings, everything. Then they can drop their phone down the toilet. When they get their replacement phone, they can take their backup and restore it onto their new phone. Bingo - same app, same user, different device, and different token.
As far as your app is concerned, nothing has changed since the last time it ran - it doesn't know that it's actually running on a different device now. The only way it knows is because it asks for the 'current' device token, and hey presto it's a different token to last time.
You can choose to cache the token and check it against the token you just received (e.g. save it in your NSUserDefaults) - that way you don't have to communicate it back to the server unless it has changed since the last run, but you absolutely do have to check, otherwise your users will come complaining that they don't get push notifications any more since they replaced their phone.

Testing Apple Push Notifications Feedback - no items received

How to test feedback.sandbox.push.apple.com? Everything goes right, but I receive empty list.
How to make it consider the device token as inactive?
I installed the application to iPhone using Xcode, received some push notifications, then removed it from iPhone and send some more notifications. But even on the next day feedback.sandbox.push.apple.com returns just empty set.
Issues with Using the Feedback Service
If you remove your app from your device and then send a push notification to it, you would expect to have the device token rejected, and the invalidated device token should appear on the feedback service. However, if this was the last push-enabled app on the device, it will not show up in the feedback service. This is because deleting the last app tears down the persistent connection to the push service before the notice of the deletion can be sent.
You can work around this by leaving at least one push-enabled app on the device in order to keep the persistent connection up. Just install any free push-enabled app from the App Store and you should then be able to delete your app and see it appear in the feedback service.
source:
http://developer.apple.com/library/ios/technotes/tn2010/tn2265.html#TNTAG34
I far as I found, Apple push notification feedback service doesn't work properly on sandbox mode. You should try it on ad-hoc or production mode.