Notify iOS app to update its content - ios

How can an iOS app be notified that it needs to update its content?
I would like to have an app that listens for update alerts that trigger a download rather than doing a data request to be able to stay in sync with the server.
What is the correct way to have an iOS app that needs to be in sync with a server stay in sync while minimizing network activity?
Im aware of using a cache locally, NSURL Cache or Core data etc. and adhere to the timeout in the http header etc. but I would like to correctly setup a sync mechanism without having an update button that a user needs to press to get updates.
I have thought of implementing a dispatch_source on a socket but wont that keep the network active and drain battery?
Rather than request a token for the version of the data to sync from the server I would rather like the server to notify the device that it needs to update. Is this practical?
How does the OS handle listening to push notifications? Is it on POSIX level?

You really have two options here:
Client Side Polling: call the server and ask every so often. For 99% of apps this is fine as it's not that big a deal for someone to run the old version during the interval between polling.
Real-time Server Push: If for some reason you need to disable the old version as soon as you push a new version, you could do a server side push. You would not want to use APNS for this (user may deny APNS, delivery not guaranteed, etc). You would want to use a cheap push service like PubNub or Pusher for this to guarantee real-time delivery. Or code you own version using the same principles of these services, but really why re-invent the wheel?

There are two common, as far as I am aware, approaches to doing this on iOS;
Regular GetUpdates calls to the server at an interval (Fairly standard across any platform I think)
Apple's Push Notification Server(APNS) if anything changes on the server you can push a notification to the device.
At work we typically implement both options because there are a few issues with APNS;
They are not guaranteed to reach the device, if the device is offline the APNS service will store the Notification for a limited time in the APNS system upon which it is forwarded (if it hasn't expired)
If multiple Notifications are sent while the device is offline only the most recent notification is sent
If the device is offline for a long enough period of time all stored notifications will be discarded
the user has to allow Push Notifications to be sent to their device because it suppliers and identifier that you can use to determine the device

Related

Is there a way for the server to push data to an app without using push notifications?

I'm looking for a way to push data to an app similar to APNs but that doesn't use APNs. Is there a way to do this? The reason I want to do this is if the User disables notifications I want to still be able to send data to the app without needing user input such as a pull to refresh or what have you. I only want it to happen when the App is in the foreground.
Basically, I want it to use push notifications like normal, so if there is new data available the user is notified if the app is closed so the user can click on it and have the app open and display the new info. Then if the user disables push notifications through the settings the user will no longer receive notifications, but the app will still refresh if there is new data available.
I tried using GCM/FCM but that just piggybacks off of APNs so it won't work if the setting is turned off for notifications.
Is this even possible?
If the app is in the foreground, you have two options:
1) Poll - For example, every few seconds to you make a network call to poll for new data. This may be possible if you can guess a frequency at which you get new data. But as you can imagine, this is pretty impractical/wasteful if you have to do this very often.
2) Push - You can open a persistent HTTP connection (web socket) and push new data to the client from server. This is more difficult architecture (publish-subscribe) to implement than polling, but definitely worth the time if you implementing any sort of real time system (think new Facebook posts on the timeline, new tweets). For websockets, you can use a home grown solution (eg http://socket.io/) or you can use many available 3rd party solutions (like http://pubnub.com etc.)

Should I use Apple Push Notification or simply pull data in background for simple app?

I have an app that fetches data from server via json/http (actually, it synconizes data with Core Data). User needs to be notified when new data is available in 5-10 minutes. I am planning to have about 10 users (that as an internal enterprise app), so I can simply fetch data in background using NSURLSession.
But iOS may kill my app in case of low memory (or even user may kill it!), and no notifications would be delivered! So, I believe APN may be used. But this solution looks very complex for such a simple task and small number of users.
Ideally, I should have some service/daemon or (like IntentService on android), but I can't have it on iOS, so there are only 2 ways: APN or background app (which may be killed). What is the right way here?
Even if your app killed, your app can be opened in background and fetch data. This feature is called as background app refresh. After updating data, you can show user a local notification, which makes it easy by not using APNS. However, if you want to use APNS, you can use Amazon SNS to send push notifications. If i am not remembering wrong, it gives first million push notifications as free for every month. It is very easy to implement, though.

How to make an iOS app aware of data changes on the server without any user action?

Suppose I have an e-commerce ios app (like snapdeal, flipkart etc.) and I want to make my app notified when there is any change in the data stored on the server.
For instance, take an example of price change of any product in any category, so how can I update the price for that product without putting it to user's knowledge, whether the user is on that particular screen (currently seeing that product for which the price is changed) or on any other screen.
is it possible to do? if YES, how can I achieve that?
Thanks in advance!!
You have to use Apple Push Notification Service to do that.
It allows your server to send data to your app, that warns user or not (you choose), and your app is awake by this push a can work on background mode.
In order to allow you app to work in background, you have to enable Background fetch options
From Apple Background execution guide :
Apps that need to check for new content periodically can ask the system to wake them up so that they can initiate a fetch operation for that content. To support this mode, enable the Background fetch option from the Background modes section of the Capabilities tab in your Xcode project. (You can also enable this support by including the UIBackgroundModes key with the fetch value in your app’s Info.plist file.)
Edit about BaaS
As mentioned in comments, implementing the whole push notification system yourself can be difficult, especially if you are doing it for the first time.
That's why you can use a BaaS (Backend as a Service) that handle push notification for you like Parse.com, Firebase or whatever you want. It simplify a lot the process, but it remains APNS on the back, so it's important to understand how it works.
I'd recommend, like Mr. Blackus said in his answer, to use Apple Push Notification service. However, I will add one bit of information -
Don't use the service to send the actual data, only send a small packet of information telling your application to request more data from the server.
The process should be something like this:
Server asks Apple service to send Push notification to your app.
App receives push notification (but does not show any badge to the user).
App interprets the data sent from the server as a "refresh data" notification.
Fire POST or GET request upon determining type of notification received.
Update your data from the request response.
1.) Polling - app asks for list of products whose prices has changed. You can setup a timer, which triggers itself after every X seconds.
2.) Socket connection - between app and the server. App can generate a unique token (such as identifierForVendor), and use it in the socket conenction. Server can notifiy all the apps (using this token), to notify about the changed prices.
Approach 2 is better, as it is less expensive ( in terms of networking).
Hope this helps.

Is it possible to use server-side events to notify an iOS user despite my app not running, without using Apple Push Notification Service?

I am considering using EventSource (server-sent events) to send notifications to my iOS App users, instead of APNS. This is great when app is active, but is it possible to ensure these users receive my messages when my app is not currently running on their device? Or is the only way to do this APNS?
If you need your events to arrive in real-time (like a messaging system), then APNS is probably your best bet. APNS is the only way to send realtime messages from a server to your app. However, it is wise to know that there are downsides to push notifications.
If you don't care about real-time events (not a messaging system), or if it's okay if you get the events a bit delayed, you can use background fetching to periodically poll your server for new events.
Objc.io has a great article discussing some of these: http://www.objc.io/issue-5/multitasking.html

APNS? Or Polling? Which is more reliable?

I'm working on a small messaging app, similar to IM or SMS text messages. I'm obviously planning on using Apple's Push Notification Service (APNS) for notifying users of new messages when the app is not running. The question is, what about when it is running? Should I rely on APNS to notify the app when to refresh the list of messages that the user has received? Or, should I do periodic polling to grab any new messages?
My app will be making use of RestKit 0.20 as its web service client library. Polling should be easy enough to configure there, but I'm concerned about the additional overhead of creating the connections, as well as the increased bandwidth utilization that polling will create.
Is APNS reliable enough that I can use it to trigger a UI refresh when a new message is received? Or should I handle it manually within the app itself via polling?
APNS is very reliable, so you can get away with using it to refresh the application when new messages come in. However, it isn't 100%, so I'd plan on keeping a Refresh button close by to allow the user to poll as they wish.

Resources