0

I have a chat application, developed on the Xamarin.Forms platform, in which users can chat with each other. I have managed c# code and UI both in the shared project.

I have been facing problem since long in iOS platform. When the iOS app is running on screen, having foreground mode then the app can successfully receive a message which has been send by another user. When the app is running in the background mode and someone sends a message, I want to notify the user by using local notification (No Push remote notification - Because I think as my app is already running in minimized mode there is no need to wake up the app by implementing Push notification). Even I have implemented local notification successfully but the problem is,

When the iOS application heads to the background mode, the main thread (task) is paused so, when some user sends a message the app is not able to execute the code (when app is minimized) so that it won’t be able to show the local notification. But when the application is brought back to the foreground the thread/task get resumed and then ie shows up the local notification and also the message.

I already have selected the "Background fetch" property under Background Modes in Info.plist. I have also added below the line in my FinishedLaunching method

UIApplication.SharedApplication.SetMinimumBackgroundFetchInterval(UIApplication.BackgroundFetchIntervalMinimum);

I have already worked and implemented code from below links, but didn’t worked for me.

I think my issue is relate to iOS Background processing, So, does anyone has idea what to do, to execute the code when app is already in minimized mode?

Vivek Patel
  • 25
  • 2
  • 6

1 Answers1

0

Generally speaking, backgrounding is very restricted on iOS. If your app is in a certain category (e.g. Navigation, Music) you'd get extended backgrounding capabilities, but I don't believe that chat apps do. More specifically, Background Fetch is not really suitable for your problem. It is called on an irregular basis to fetch contents to be cached within your app in order to make showing contents to your users faster. Background fetch intervals may vary from 15 min to several hours (not sure about the latter).

What you need is remote notifications.

Remote notifications (also known as push notifications) let you push small amounts of data to devices on which your app is installed, even when your app isn't running.

Remote notifications are brokered via a priviliged service (Apple Push Notification service - APNs) to Apple devices and delivered in a timely manner (seconds rather than minutes or hours). Usually you'd want to keep the payload as little as possible (just send the chat ID for example) and let the app fetch its data when it's notified.

Basic structure of Apple Remote notifications.

Speaking in terms of a chat application, your chat server would send the remote notification to the APNs whenever a user sends a message to the chat. The app would be notified, fetch additional data and then display the notification to the user. If the notification is tapped, the user would be taken to the chat window for the respective chat.

Please note that your app has to be registered with APNs, otherwise remote notifications won't work.

Paul Kertscher
  • 9,416
  • 5
  • 32
  • 57
  • Thanks for the answer. My app is also having the audio-video calling functionality, so I also want to play the ringing sound during incoming call request, when app is minimized. For the Background modes in info.plist, I have selected "Audio, Airplay and Picture in Picture", "Voice over IP", and "Background fetch" option. So is there any solution of my problem after selecting these options. App is already running in background so I want to execute the code when app is in background and display the local notification I don't require Push. For Android platform it is working with out any problem. – Vivek Patel Mar 26 '20 at 04:26