Flutter connection back online background event listener - flutter

I've been trying to learn Flutter by developing an Offline-first application using non-firebase backend API. The problem I am trying to solve derives from the following requirements:
Given User devise is Offline and he creates a record in the app, the record is stored on a local Database and is synced to backend once app is back online
If the app is not in the foreground, user should receive notifications once he is back online, given something changed on BE while he was offline.
Now, the problem is that reading through some packages that Flutter provides like background_fetch, I've stumbled upon the following limitation: "Background Fetch is a very simple plugin which will awaken an app in the background about every 15 minutes, providing a short period of background running-time."
The problem is that testing a similar IOS offline-first app I've noticed that notification comes through the moment internet is back online.
My question is the following: How can I achieve an immediate response/notification from my Flutter app once internet is back online, given that application is not working in the foreground. A fetch once every 15 minutes would work but is obviously is less than ideal, and given there are already apps on App Store/Play market that are notifying me immediately just keeps me awake at night. Is there a way to configure an event listener for connectivity change without relying on the 15 min limitation?

Related

Incoming call notification

We are developing an app with angular/ionic in which we use capacitor jitsi plugin for video calls. What we are now trying to do is to receive notifications (via firebase) like in whatsapp with the incoming call screen and two buttons to accept and decline. Any idea on how to do that?
Thanks
If you got the choice to change the notification service, instead or directly using firebase, you could use Onesignal which extends firebase and they already have a service named VOIP notifications which should kinda do your needs and here is the link:
https://documentation.onesignal.com/docs/voip-notifications
In case your are restricted with firebase or need to know how this could be done, bellow will be the way to achieve it..
As for android:
First as logic part, you need to add some code in the native layer since hybrid apps usually can't interact from JavaScript side to native side in case app was not launched, so in order to wake the application on a specific event like notification received or any other actions that phone system can hold..
Second, as technical part, you need to add broadcast receivers and the receivers role stand to interact as native code with system. example in the link below:
https://www.digitalocean.com/community/tutorials/android-broadcastreceiver-example-tutorial
also another video about foreground and background broadcast receiver service in the link below:
https://www.youtube.com/watch?v=rlzfcqDlovg
video code output in git:
https://github.com/borntocoderepos/callrecorder
in the Youtube video example, the user is launching a toast message on phone call if app was opened or closed (background or foreground) so you can launch your app with intent with passing data and capture the data on app start as Deep Links as capacitor (https://capacitorjs.com/docs/guides/deep-links) or Cordova (https://ionicframework.com/docs/native/deeplinks)..
And instead of listening to network or phone calls, you can listen to Notifications and for sure you need to do searches about your topic and or the notification service that you'll choose.
Now for the video and the tutorial not sure of the quality of code so make sure to do more researches about the way its done from different places (could be outdated code or bad code quality or even not complete service and will discuss about this point below).
In android there is policy about using background and foreground services so once you start a service you need to end it after your done so make sure after you receive the notification and launch your app to stop the listening since it would cost power usage and perhaps could be stopped by Playstore as harmful app.
Now considering IOS it should be the same concept so make searches about this topic, but for IOS, the listeners policy as I remember , the receivers should not be waked up for more than 15 mins, so also keep this in mind and make sure you stop the receivers directly after launching your Hybrid app.
Broadcast equivalent receiver for IOS:
http://www.andrewcbancroft.com/2014/10/08/fundamentals-of-nsnotificationcenter-in-swift/

Move flutter (dart) function to foreground service ANDROID

Im working on the app sends list of data to server one by one in foreground with progress indication via push notification to user. I need to send the data to server even the app is killed with the progress indication using the android service.
Is this possible? If not please suggest me any other solutions or techniques.

How to create proper notifications on iOS (FCM doesn't seem to be the way)?

According to FCM official documentation:
On iOS, if the user swipes away the application from app Switcher, it
must be manually reopened again for background messages to start
working again
See:
https://firebase.flutter.dev/docs/messaging/usage/
However, any app with notifications that I can think of (Whatsapp, Facebook messenger, Tinder etc) will still notify the user if there's a new message, even if the app was swiped away. A different behavior will be a very bad UX (as a sidenote, background messages don't work on FCM ->APN -> client if the physical iPhone was restarted, see https://github.com/FirebaseExtended/flutterfire/issues/7785)
So FCM isn't the right way for iOS. I thought that APN might be the right way, but to my surprise, all of the APN packages seem to be dead (for example see https://pub.dev/packages/flutter_apns - was last pushed 6 months ago and authors stopped responding to issues).
Flutter is a popular framework which exists for years now, so there must be a way to implement notifications properly. What is that way?

How to make periodic background fetches in Flutter iOS and Android App?

I am developing an app that should fetch an API periodically (every 15 or 30 minutes). I am using Flutter for the frontend development. The app should be available for both Android and iOS devices.
The background task includes a call to the backend. In the backend, a worker is determining certain data (this can take up to one minute) and sending it then back to the frontend. The task has to be (at least partially) initiated by the frontend, as an decryption key is passed to the backend.
I had the idea to use Firebase Cloud Messaging to push the requested data to the devices. Unfortunately, it seems to be necessary to have the Apple Development Program enrolled to use it. As the app is an university project, and I'm not even sure, if the FCM approach is the best one, I don't want to pay the 99€ for the enrollment.
Beside the FCM approach, I found some flutter packages like "background_fetch", to pull the data instead of pushing it, which wouldn't be a problem for me. But all packages I found have the problem, that they work differently on iOS and Android, and are also not very reliable in terms of background task management by the respective operating systems.
Is there any "simple" solution for this problem, like a flutter package allowing me to schedule periodic background tasks on both iOS and Android, which is mostly reliable in that the time periode is respected at least roughly and the background task is not killed while waiting for the response? Or will I have to implement an own server-side Push-Service to avoid the costs for the Apple Developer Program?
Thank you in advance!

IOS background work

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.