I'm building app that uses flutter_pllayout and adhara_socket_io
App Scenario
When app is launched, it connect to a socket and keep receiving audio urls at random times, then it plays this url, it works fine on android
Problem
but in iOS when user switch to home or any other app (when my app is in background) it seems to close the connection of the socket and continue when app become in foreground, is there any way to keep the socket connection open ?
Background tasks on iOS are very limited. Only works if you play audio constantly with the correct Info.plist details.
If you want recurrent (cron job task) client side, use: BGTaskScheduler (for iOS), one other option is using a cron job triggered push notification task (you can even user Firebase Functions for that) server side.
Official docs: BFTaskScheduler, Background Behavior iOS, Enabling Background Audio
and no, sockets connections can't keep opened all the time (not even on Android on latest versions of their OS). Again, use cron job tasks to achieve a similar behavior.
Officials docs: Android Background limitations
Related
Here is the use case:
the user launches the app and grants permission, the app connects to the server
at some point in the future, the server sends a request to the app
the app
regardless of whether it is running or not in the foreground or background, wakes up to run this code
Here are some options I have explored which I am unsure about:
server sent events
websockets
push notifications
Is there a reliable and safe way to do this on android, whether it's Kotlin or Flutter? Can you provide examples or documentation?
Thanks!
Turns out that the best way to do it is with push notifications!
Websockets and server sent events are better suited for other use cases. Persistent connections to a server are resource consuming, and both Android and iOS have mechanisms in place to terminate apps running in the background to save memory etc. So we would probably lose the connection and not be able to receive anything from the server if we minimized the app or locked our phone screen.
Push notifications are basically built for this exact use case because, even though they work differently on Android and iOS, they are built to receive messages from a server regardless of whether the app is in the foreground, background, or not even currently running.
I used Firebase Cloud Messaging to build my app since it's primarily an Android app. It worked like a charm.
Is it possible to use connectivity plugin in background process?
I don't know how to call dart code in background there is little about this in docs.
I need this functionality to sync offline data with backend whenever there is a connection wether app is in foreground or background and app is in killed state like the way Whatsapp syncs messages.
You can use existing plugins for background execution (on android & ios usually in a roughly 15 minute interval)
https://pub.dev/packages/background_fetch
There is also a article dedicated to using isolates for background executions
https://flutter.dev/docs/development/packages-and-plugins/background-processes
https://medium.com/flutter/executing-dart-in-the-background-with-flutter-plugins-and-geofencing-2b3e40a1a124
I am building an ios application that requires internet connection via wi-fi in order to talk to web service. Now before anything, i want my application to run a background process that checks internet availability and when connected talk to web service.
Any body knows how to implement this in background process?
I already use this solution to check whether there is an internet connection or not
Easiest way to detect Internet connection on iOS?
but I want to know how to run this process in the background in order to work even the application is closed.
You can have your checks done when in foreground. Its not allowed to for an app to execute code in background continuously(although you can execute some code when you go to background initially). If you are not planning to publish this app in app store and its a enterprise solution, then you should explore ways to stay in background by running a music file (without sound) to get CPU Cycles. It worked for me..
I have an iPhone application like facebook for iPhone. My application must connect my server and read all message every two hours regularly. I have a thread to read all message but when the application is terminated the thread cannot work. Can the thread run undependently from main delegate or how can I find solution for this problem?
You cannot have your app do stuff in the background. There is an API to finish tasks like uploading a photo but even that will be killed after around 10 minutes.
But the Apple Push Notification Service seems like the most appropriate solution for your problem. Your server notifies the device that there is something new happening and you fetch the actual messages when the user opens the app.
edit: As of iOS 7 Apple implemented a feature where you can schedule running tasks to fetch data in the background. Those tasks are not guaranteed to run at any specific times. See the release notes for iOS 7 and the linked methods below:
Apps that regularly update their content by contacting a server can
register with the system and be launched periodically to retrieve that
content in the background. To register, include the UIBackgroundModes
key with the fetch value in your app’s Info.plist file. Then, when
your app is launched, call the setMinimumBackgroundFetchInterval:
method to determine how often it receives update messages. Finally,
you must also implement the
application:performFetchWithCompletionHandler: method in your app
delegate.
There is no solution.
Apple does not permit applications to run in the background unless they are of a specific type such as location or audio or voip or newstand (your app can continue to run for about 10 minutes after it was active if it uses shouldBeginBackgroundTaskWithExpirationHandler).
There is no workaround, many many other people have wondered how to do the same thing as yourself before, but there is no legitimate way. Your app cannot schedule any sort of periodic call home activity.
The only way your app can run once its gone into a suspended or terminated state is for the user to launch it, either explicitly or in reponse to a local notification or remote push notification.
what iOS feature does skype app use where it can remain on top of other apps with a drop down like bar even if you run other apps?
Skype uses the iOS 4 multitasking APIs, specifically VoIP multitasking in order to keep the call active while the rest of the app is suspended.
When the app is sent to the background, Skype informs the system that it would like to keep its network connection alive and that the audio subsystem should remain active.
The system continues to look after the network connection, passing received data back to Skype in order for it to process the audio.
This is one of the three main forms of multitasking in iOS, the others being: audio streaming, like Pandora and location services, like Tom Tom navigation.
It does this via the background execution APIs available starting in iOS 4. More information about how it works can be found in the Implementing a VoIP Application subsection on this page in the iOS Application Programming Guide.