How to determine Push notification status programmatically? - iphone

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.

Related

Call status change web-service when my App is deleted

In my application , i works with web-services and when user log in into my app my app is sending a request with status 1 means loged in and when on log out button click sending a request with status 0.
Now problem is , when user removes app from devices , status in my server is remain 1(log in) , hence other user can see him available while his app is not in device. so is there any way by which i can send request when my app removes from device (i don't think it is possible) or is there any other way that i can do in my backend side ?
Thanks in advance.
It is not possible to call a web service when the user deleting the app from an ios device. There are three methods to came to know that whether your application is there in user's ios device. But there are few limitations also.
Activate Push notification: By doing this, device will get registered with Apple's push notification service. When user delete the app from device, the registration will be revoked from APNS server and through the APNS feedback service, you can get to know whether the application is existing. (Limitation: If the user did not agree with receiving push notifications, then the app will not be registered with APNS and you never came to know that whether application is existing or not)
Activate Location Based service: If your application enables location based service, then your application will get periodic location updates in a location delegate method. In this delegate, you can call a webservice and keep update the status of user even the application is in background. (Limitation: If user disables the location update, then your server will not get info about user status)
Periodically Call a Webservice From you app: This is possible only if your app is active. (Limitation: When you application pushed in to background, your application will be in suspended mode, so it will not possible to call webservice)
Sorry Unfortunately Apple not provide any method that user Uninstall app from user's device, There is no such method.
When user delete any application device does give the alert "Do you want to delete this application" with option "Delete" and "Cancel". You don't have to write any specific code for this.
I just assume that There is one method in which you can find out when user is about to delete your app. That is you need to implement push notification Apple server will inform you when you try to push to an uninstalled instance. Other than that there's no way to detect it but i am not sure its helpful or not.
You can't do this from within the app. You would want to do something like have a periodic task which runs on the backend, checking the last activity date of logged in users and setting them to 'not available' after some configured period of inactivity. This will probably require some changes to the backend to record last activity date and a change to the app so that while it's open it sends a periodic 'heartbeat' to the backend. You probably want to make the timeout quite big (say 15 minutes, big enough to not have a large impact on performance).

APNS Apple Push Notification token goes stale and stops working

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.

Push Notifications for iPhone - accepted methodolgy

I’m looking at the designing and developing a service for Push notifications, and am trying to understand the order of processing in the Apple App when it comes to Registering for Push Notifications and storage of the Token.
What I’m developing currently is a service that allows subscription to individual changes that happen to particular processing on our server.
When they happen, the phone user may receive a message of type “A”, “B”, or “C”.
The phone user has to “subscribe” to each of these types individually if they want them. Otherwise they can ignore it and not subscribe.
So User Fred, on Phone #4, starts our App, logs in, and then has the ability to turn on or off subscriptions to events that are sent as Push Notifications.
So I need to tie details from Fred’s login, to Phone #4’s device Token, with particular subscriptions.
So my particularly important questions are.
When the Phone connects to the APNS server to get its device token, is this automatic on app start? Or can this be initiated at a later step? Ie, after going through a loging screen on our app.
Can we (are we allowed to) store the device token on the phone in the App's data store?. Or, should the App be connecting to the APNS server every time the app is run?
How does the App know if it as already called the APNS server and retrieved a token, or as above, should it call the APNS server time the app is run?
Can we (are we allowed to) store the token in the App’s memory as it runs, so we can properly subscribe and unsubscribe for particular messages?
We need also to be able to list all the subscriptions that a particular user may have across all their devices so the user can remove old devices (if they change phones). Or can we rely upon data back from calls to the APNS – when we attempt to push a notification - to inform us that a device token is no longer valid?
Or is there some better way of tying this all together?
When the Phone connects to the APNS server to get its device token, is
this automatic on app start? Or can this be initiated at a later step?
Ie, after logging into our app.
After the app has started, the app gets the token by calling registerForRemoteNotificationTypes. This will prompt the user for permission, and call a callback with the device token if permission is granted.
Can we (are we allowed to) store the device token on the phone in the
Apps data store?. Or, should the App be connecting to the APNS server
every time the app is run?
You'll need to build an APN provider, which is a web server that calls apple to send the pushes. The thing to do with the token is post it to your server that uses the APN provider. The app doesn't connect to APNS, your provider does, and it does it when it has pushes to send.
Can we (are we allowed to) store the token in the App’s memory as it
runs, so we can properly subscribe and unsubscribe for particular
messages?
You can keep the token on the client, but you don't really need to. It's your web service that calls APN, so it needs to be kept aware of your users' subscription prefs.
We need also to be able to list all the subscriptions that a
particular user may have across all their devices so the user can
remove old devices (if they change phones). Or can we rely upon data
back from calls to the APNS – when we attempt to push a notification -
to inform us that a device token is no longer valid?
APN also provides a feedback service that you call in batch which returns the device tokens that are no longer valid. Not only can you use this service, but you must. Apple will get mad at apps that repeatedly send to no longer valid devices.
Or is there some better way of tying this all together?
Yes! Parse.com provides a nice wrapper on the client code, does the provider and feedback service, abstracts the idea of single devices to the idea of a "channel" which sounds like just what you need for multiple notification types A, B, C, provides a super-easy step by step setup, and loads of other useful cloud services for iOS. (I'm not affiliated, but a big fan).

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".

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.