How does a server notify a mobile application of a state change - ios

lets suppose I have a PHP web service, and I have an iphone app that communicates with it. If the iPhone want info from the web services, it makes an http call to the server ans retrieves information. But how do you do this the other way around, how does would the server notify the the application when some state has changed, and what is the most efficient way to do this?

What you are looking for is Push Notifications. You will need to create a push notification server that will send notifications to your users. http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html

Related

How to push data to ios application

I have a native ios app that is essentially a REST client. The backend is a java REST API.
There is a delegate service that the backend is subscribed to. The delegate service sends events to the backend service.
What is the best way to communicate these events back to the IOS app?
Is polling the only option or is there a cleaner and more efficient way to PUSH data back to the devices.
I have done a little reading about push notifications but I am not sure it fits my use case. I don't want to notify the user about the change, I want the data to be passed to the application and to be used to perform business logic (change the u.i)

How to share data between an iOS app and a macOS app?

I have an iOS app that plays music with the MusicKit API. I want to write a companion app for macOS that communicates with the iOS app.
For example, my iPhone is playing music. When a new track starts playing, a notification appears in the macOS app. I can click a button on macOS to skip to the next track on my phone.
What's the best way to communicate between devices like this?
I think there are two important use cases:
For static data like account details and preferences.
For real-time messages.
I could do everything via a remote server but I wonder if there's a better way.
The user needs to have your app installed on iOS and macOS and needs to separately grant permission to receive push notifications.
Keep in mind that Apple does not guarantee the real-time delivery or delivery at all of remote push notifications. Remote push notifications may also be discarded by the APNS if many are sent within a short timeframe. It is not recommended to use the APNS for user-initiated actions such as music track controlling, as the user expects an (immediate) result. A web socket or other permanent connection between your server and its clients should be used instead.
Playing music on one device, stopping on another:
Request permission to send remote push notifications to get the unique APNS token for each device.
Store the device tokens on your server.
Make the iOS client notify the server that the user started playing music.
Make the server send a remote push notification to the macOS client via the APNS.
On the macOS client handle the user interaction with the push notification and report the user action back to the server.
Make the server notify the iOS of the user interaction via a web socket connection, rather than a remote push notification to ensure that the client handles the user action.
Make the iOS client skip the track.
Synching user preferences:
Store preferences on server for every change.
Request preferences from server on every app launch or dynamically when the preferences change by triggering the request through a remote push notification by the server or via a web socket connection, depending on how real-time the synchronization should be.

Is this possible to keep polling http after the user inactive the iOS application?

I got a programme, it requires polling result from the HTTP server, but the HTTP Server won't connect to Apple push notification server. So, is this possible to keep polling after the user click the home bottom? Thanks.
In iOS7 Apple introduced the ability for apps to refresh their data when the connection is started up by another app. You need to implement application:performFetchWithCompletionHandler:

Notify iOS app to update its content

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

Is there no option other than Apple Push Notification for sending a notification from a server to a client?

Its not possible to intercept SMSs with iOS, and its not possible to poll a server or permanently maintain an outstanding HTTP request from the device to a server (unless the app has a valid background mode).
Therefore if there is a requirement to push a notification from a server to the client, is it correct that there is no option other than to us APNS?
If there is a requirement to push a notification from a server to the device and not have the user receive an alert (in the situation when the application is in the background), then it seems this just isn't possible at all?
Is this correct. Are there any creative workarounds that are legitimate?
You are correct - there is no option but APNS for receiving notifications once an App is in the background.
There is no way to intercept ShortMessages (in background or not). There also is no possible way to permanently poll a server when being in background (which is good because that would drain the battery as it does for Android apps).
It could be possible if you can class your application as VOIP and use that background processing mode.
If there is a requirement to push a notification from a server to the device and not have the user receive an alert (in the situation when the application is in the background), then it seems this just isn't possible at all?
If the user doesn't need to receive an immediate alert, can't you just poll the server when the application is started or comes to the foreground?

Resources