how to run dart code when receive notification on Terminated state? - flutter

I have to post some data to Firebase when user receive notification when app is on killed/Terminated state,
I tried flutter_background_service but that not solved my problem.
I don't want to run my app on background, I just want to run my app on background when I receive notification and after sends data to Firebase close the background thread.

I faced this problem a long time ago. I solved it by using Firebase native instead of Firebase flutter, and pass all my data to/from native using native bridge.
You might be able to use both (flutter for most of the stuff, native for the background stuff), but be prepared for dependency hell and some bizarre behavior on init etc.

Basically when your app is in the background or not running then if firebase notification triggers then the FirebaseMessaging.onBackgroundMessage() method gets called.
So if you want to do something then you can put your code like
/// this method will be called when you receive a message from firebase
/// while your app is not running
/// put this line on your main method
FirebaseMessaging.onBackgroundMessage(_messageHandler);
/// this method should be a top level function
Future<void> _messageHandler(RemoteMessage message) async {
// do what you want
}

Related

Run methods even when the application has been closed

I have a method that works like a timer. When a certain time is reached, the app is capable of sending a push notification to the user. The problem with this is that I have only gotten this method to work when the app is running in the background or when the user is running the app.
My question is if there is any way to keep this method running even when the user has completely close the application, regardless of whether it is on Android or iOS.
if your needs to work in background then use Flutter Background Service and Put that function onStart method,
it will works while your app in Background Mode and it's not running,
https://pub.dev/packages/flutter_background_service
flutter_background_service: ^2.4.6

Flutter Fetch API in background when App is terminated

I'm want to fetch API every 15min and check if anything changes. Then if it does send push notification to inform the user about changes. Now my problem is that I cannot get to run the check on IOS. Does anyone have a solution for this?
There is a tool: https://pub.dev/packages/background_fetch, but it's not the perfect Dart solution for iOS to schedule background tasks as android when the app terminates, so try using Alarm Manager and
Flutter WorkManager
Also, you can try native side implementation with BGTaskScheduler

Flutter background Api fetch

Actually I was trying to implement background process (fetch data from api) even the app is terminated in my flutter project how can I achieve in both Android & iOS platforms.
Have you ever wanted to execute Dart code in the background—even if your app wasn’t the currently active app?
The mechanism for this feature involves setting up an isolate.
Isolates are Dart’s model for multithreading, though an isolate
differs from a conventional thread in that it doesn’t share memory
with the main program. You’ll set up your isolate for background
execution using callbacks and a callback dispatcher.
Example
but if you want do that when app terminated, you will have to wakeup app through Workmanager or some another approach like sending firebase push notification or wakeup app in scheduled time with flutter alarm manager or something like that

how to open calling screen when App is killed , I am using agora.io

I am using agora_rtc_engine for video calling.
I have tested the Firebase background message but the documentation mentions:
Since the handler runs in its own isolate outside your applications
context, it is not possible to update application state or execute any
UI impacting logic. You can however perform logic such as HTTP
requests, IO operations (updating local storage), communicate with
other plugins etc.
Source https://firebase.flutter.dev/docs/messaging/usage/
How I can display incoming call screen when Firebase notification arrived in the background when App is killed?
I also use callkeep. It shows phone's default calling screen but when I click on answer call it starts the call. I am not able to navigate to specific screen.
Thanks.
Here is something that you can try - using the CallKeepPerformAnswerCallAction event you can bring your app to foreground using bringtoforeground

Receiving call same as WhatsApp in Flutter

I am implementing a video call feature in Flutter with the help of Firebase Cloud Messaging,
it's working fine in foreground.
my doubt is
while app is in background I need to get a receiving call same like as WhatsApp in both Android and IOS .... is it possible in flutter?
yes. it is possible by the combination of push notification and flutter_callkeep to show the upcoming call screen.
Or if you want to run the background process you need to refer to Method Channels, and here is a post about creating a service for running app in the background. also background_fetch is a useful package for background process.