Yaron Levi
Yaron Levi

Reputation: 13079

iOS - How to properly use Background Fetch and Remote Notifications in client server chat app?

I am building a simple client-server chat application (think WhatsApp,Telegram...).
I am trying to figure out how to use the available background API's to keep the client data updated, specifically, how to handle a case were a chat room has one or more new messages in it and the app is in the background state.

The app will target iOS 7 and up so I have these API's regrading background execution:

beginBackgroundTaskWithExpirationHandler doesn't really fits here because I will need to call it manualy when the user exists the app.

Background Fetch doesn't seem to fit here as well because it will constantly be invoked when not needed and will drain the battery and stress the back-end.

So I am left with Remote Notifications, which are essentially silent push notifications. The use case I had in mind is:

  1. While app in background some new messages were generated in a chat room.

  2. The back-end send a Remote Notification to the client.

  3. The silent push is handled and the client fetches in background any new data in the chat room.
  4. When fetch is over, a local notification is fired.

  5. User opens the app with fresh data in chat room.

The problem here is that now, the Remote Notifications are my only mechanism for getting new message while in background, and Push Notifications are said to be best-effort style and you should not use them for your main means of communication.

Another approach will be to leave out all the background downloading, and simply ask for new data each time the user opens the app but this will harm the user experience.

So to sum it up:

  1. Can I rely on remote notifications as my main means of communications when in background (and maybe in foreground, inside a chat room also) ?
  2. Should I use the other background API's in this case ?

I know the question is quite broad in scope and lack focus, but it's mainly for the purpose of not reinventing the wheel. I assume that the scenario I described is quite common and just want to know what is the best way yo tackle it before writing code/spending time on a strategy that is not fitting to begin with.

Upvotes: 0

Views: 1406

Answers (1)

dsmDev
dsmDev

Reputation: 361

Unless you want the app to be directly notified of new content, rather than regularly checking for it, I think that Background Fetch will work for this. It won't actually be constantly invoked, and you have some control over the frequency. If you set the fetch interval to minimum the system will check as often as it deems possible (depending on what kind of good "background citizen" your app is). As far as the power consumption, if you use Fetch well and call the completion handler at the right times (as soon as the task is done, or when your 30 seconds is almost up), the system will give your app opportunities to fetch data and it will manage the use of Background Fetch with battery life in mind. And it likely won't stress the back end nearly as much as sending out a lot of pushes. Fetch is very useful for apps like yours.

Upvotes: 1

Related Questions