Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flutter should provide an abstraction for background execution #3671

Closed
eseidelGoogle opened this issue May 2, 2016 · 141 comments
Closed

Flutter should provide an abstraction for background execution #3671

eseidelGoogle opened this issue May 2, 2016 · 141 comments

Comments

@eseidelGoogle
Copy link
Contributor

@eseidelGoogle eseidelGoogle commented May 2, 2016

Customers would like to execute dart code as background processes on both iOS and Android, for things like syncing down data when wifi is available but the app is not open, etc.

@mpcomplete created a background process on Android to execute Dart code to update flutter. But I'm not aware of a parallel on iOS.

This issue tracks the development of APIs in plugins or otherwise. Engine support for these abstractions is tracked in #6192.

@abarth
Copy link
Contributor

@abarth abarth commented May 2, 2016

Should the code that runs in the background be written in Dart or in Java/Objective-C ?

@eseidelGoogle
Copy link
Contributor Author

@eseidelGoogle eseidelGoogle commented May 2, 2016

This came up in the context of wanting to update the local data model when connectivity was available. e.g. subway maps/times when you might be walking in/out of a subway. So I think the goal would be to have the executed code be Dart so you didn't have to have separate data-model-manipulation code for each platform.

@Hixie Hixie modified the milestone: Flutter 1.0 May 20, 2016
@mit-mit
Copy link
Member

@mit-mit mit-mit commented Aug 23, 2016

CC @kasperl; we have explored many ways of executing code in the background across our various targets, and perhaps some learnings could be drawn from that.

@sethladd
Copy link
Contributor

@sethladd sethladd commented Aug 23, 2016

Do we have some use cases from our users? The only one that I heard was "We want to run some code on the device, in response to a push message. We want to sync the data onto the device, even if the app isn't open."

@mit-mit
Copy link
Member

@mit-mit mit-mit commented Aug 24, 2016

It's typically pretty restricted what an app is actually allowed to do in the background, so in this case I think we can get inspiration for the use cases from there. For example, here is the iOS model:
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

@eseidelGoogle
Copy link
Contributor Author

@eseidelGoogle eseidelGoogle commented Jan 17, 2017

I believe this is the same bug as #6192, yes.

@mit-mit
Copy link
Member

@mit-mit mit-mit commented Jan 18, 2017

Adding leafy tag and milestone value from #6192

@mit-mit mit-mit modified the milestones: 2: Make Early Adopters happy, 4: Make Hixie proud Jan 18, 2017
@mit-mit
Copy link
Member

@mit-mit mit-mit commented Jan 18, 2017

@mravn-google @sigurdm something to think about!

@mit-mit
Copy link
Member

@mit-mit mit-mit commented Feb 2, 2017

Assigning to Mikkel for a very initial investigation to better understand feasibility and cost.

@mehmetf
Copy link
Member

@mehmetf mehmetf commented Feb 2, 2017

Here's an update from Leafy:

Due to limitations of data notifications (i.e. notifications that require app to respond in some way), leafy is leaning towards using display notifications (i.e. notifications that are pushed directly to user screen without app involvement). Support for these already exist in Flutter.

However, during conversations with other teams another use case has surfaced: If you want to support wearables or [home screen] widgets from your app, you would need to write the UI in native code since these surfaces are not yet supported by Flutter. However, it would be nice to be able to "wake up the app and ask for data" so that the model layer and business logic can be shared across the flutter app and the wearable app. Having never written one of these, I am not sure how existing native apps do this but it certainly seems like a valid use case for background tasks.

@sethladd
Copy link
Contributor

@sethladd sethladd commented Jul 19, 2017

Just talked to author of Flutter talk for Berlin DroidCon, and this was one of his top asks. "How to do this, the Flutter way"

@radvansky-tomas
Copy link

@radvansky-tomas radvansky-tomas commented Nov 2, 2018

Any update on this...I really thought that this is part of flutter :( ..this is than pretty much same issues than other web hybrid solutions have...

@SirWindfield
Copy link

@SirWindfield SirWindfield commented Nov 2, 2018

@radvansky-tomas I don't know why people just don't get that this is not within flutters responsibility. Even if you have to implement background execution natively you still have the advantage of one single code base. Writing one flutter plugin that does your background work takes merely one or two days of work. I had the same issue, I needed an android service that would be capable of playing music for me. I just wrote it in kotlin, implemented the communication layer using flutter and later on did the same for iOS.
It's entirely possible to just use the systems native background systems.
Instead of whining around here that X is not implemented,you can literally implement it without any problems whatsoever using the ecosystem provided by the team.

@radvansky-tomas
Copy link

@radvansky-tomas radvansky-tomas commented Nov 2, 2018

@SirWindfield Unfortunately you dont know what are you talking about. This is essential part of architecture, not plugin itself.

Flutter is presented as cross-platform solution producing native code, ie there is expectation that it can run code in its language (Dart) everytime when OS allows it.

So, if app is running after remote notification is received, and you can place breakpoint in existing native code, you should be able to call your flutter/dart code where are your services, models and finally UI.

To make it in your case...lets say that your music is playing in background and then you finish your playlist...after 10mins is your app shutdown. Then you call siri, or use headphones to start playback again of your app, whatever entry point you chose...native code is executed, but not its UI, your flutter code is not initialised and your lost as I am with my notifications.

So no its not possible, I can write native plugin catching notification for me, but then I need access to flutter business logic to process data, in my case crypto decrypt and releasing local notification with decrypted content

@SirWindfield
Copy link

@SirWindfield SirWindfield commented Nov 2, 2018

We'll, I've tested it on my phone extensively and it works without problems whatsoever.
It might be that my phone never killed the app though. My bad then.

@ethael
Copy link

@ethael ethael commented Nov 2, 2018

@christocracy any update regarding background_fetch plugin Chris?

@christocracy
Copy link

@christocracy christocracy commented Nov 2, 2018

@ethael Here's the empty repo for you to watch for updates.
https://github.com/transistorsoft/flutter_background_fetch

@bkonyi
Copy link
Contributor

@bkonyi bkonyi commented Nov 2, 2018

@radvansky-tomas you can handle background events in Dart code by creating a background isolate as described in this Medium post on background execution. You'll likely need to write some native code for each platform to suit your particular needs unless there's a plugin that already exists which does what you want (unlikely, since I'm unaware of many plugins which support background execution at the moment).

@radvansky-tomas
Copy link

@radvansky-tomas radvansky-tomas commented Nov 2, 2018

@bkonyi Thank you for reply, I started to change firebase_messaging plugin to support background execution...but this issue is more about some generic way to do it. I mean it should be part of template = runner and its dart side where devs can execute such headless code, some rules, pattern standard as now is all over some blogs, suggestions etc.

@radvansky-tomas
Copy link

@radvansky-tomas radvansky-tomas commented Nov 4, 2018

Some initial work done: https://github.com/radvansky-tomas/plugins/tree/master/packages/firebase_messaging

Following some guides from https://github.com/bkonyi/FlutterGeofencing

I should finish it by end of this week, I am only working on iOS bits for now. Example project is using local notification plugin to show that message was received in background (silent notification) and then release new local one, to manifest success.

@bkonyi
Copy link
Contributor

@bkonyi bkonyi commented Nov 5, 2018

Some initial work done: https://github.com/radvansky-tomas/plugins/tree/master/packages/firebase_messaging

Following some guides from https://github.com/bkonyi/FlutterGeofencing

I should finish it by end of this week, I am only working on iOS bits for now. Example project is using local notification plugin to show that message was received in background (silent notification) and then release new local one, to manifest success.

@goderbauer and I were actually looking to add this support, but we would be more than happy to help you out with design and reviews if you plan on making a pull request.

@radvansky-tomas
Copy link

@radvansky-tomas radvansky-tomas commented Nov 7, 2018

So I pretty much finished that part, so now I can receive silent push notifications and my callback written in dart is executed while app is in background.

However, I cannot access any other plugin there. Callback has to be root/static method and really dont get how to call anything from there.

So I tried to make simple test, to use flutter local notification plugin to send local notification after silent one is received, but plugin seems to be not accessible (not registered?)

I found similar posts here: flutter/engine#6396

Is there any other necessary step, which i have to do to load those plugins ?

My root level callback:
void callback(FirebaseMessage m, MessageEvent e) async { print('Message $m Event: $e'); final SendPort send = IsolateNameServer.lookupPortByName('messaging_send_port'); send?.send(e.toString()); }

Question is not how to access all dart code + plugins from that callback...query firestore, etc

@christocracy
Copy link

@christocracy christocracy commented Nov 9, 2018

My Background Fetch implementation is finally released.
background_fetch

@seniorbee
Copy link

@seniorbee seniorbee commented Nov 13, 2018

@christocracy You're right, but I was hoping one of the largest corporations in the world would have the resources to develop a fully featured product and not require its users to spend tens of thousands of dollars to make it usable enough.

That's so true. If you get people's time you should give something back to them. If not then don't make these fancy marketing titles!

@eseidelGoogle
Copy link
Contributor Author

@eseidelGoogle eseidelGoogle commented Nov 13, 2018

It seems like this issue is still causing folks woes? It's closed and has a lot of comments, so it's unlikely we're going to make many concrete project changes from this issue. :( (We'll need to file new ones to make more progress I expect.)

I would be very interested in us documenting the concrete changes desired here. Sounds like more background examples are desired? (e.g. #23794)

In any case, we'd love to have individual issues filed with individual asks. As others have noted above, Flutter is a large and complex project, and although we have a large (and rapidly expanding) team here at Google, we still just don't have enough engineers working fulltime on Flutter to answer all issues simultaneously. Flutter is 100% open source and all of our issues are on GitHub. Patches are always welcome of course. :)

For any with issues/changes desired, please file new issues to let all contributors know how we can help further: https://github.com/flutter/flutter/issues/new

Thanks!

@ethael
Copy link

@ethael ethael commented Nov 13, 2018

@eseidel challenge accepted: #24278

@camillo777
Copy link

@camillo777 camillo777 commented Oct 1, 2019

I was hoping to avoid writing device specific code and having a Flutter Dart API implementation maybe based on events that would hide device and os specifications and specific implementation.

@Krunal79-flutter
Copy link

@Krunal79-flutter Krunal79-flutter commented Oct 19, 2019

already solveda at here

@rdev-software
Copy link

@rdev-software rdev-software commented Oct 19, 2019

@Krunal79-flutter how exactly, that approach would still have same issues. Thing is that in your scenario is flutter already initialised and its not proper background access then. Issue what I had and have, is when app is actually dead and silent notification (or app extension) will wake up your app and you need perform "dart" logic while flutter is not running yet (ie no UI present).

@tomoerlemans010
Copy link

@tomoerlemans010 tomoerlemans010 commented May 26, 2020

This really ought to be a much higher priority issue for whoever is managing the Flutter team. Implementing background functionality is a huge pain for the average Flutter developer and it breaks down often an in non-trivial ways. And I am sure that a lot of app developers are currently struggling with this.

@a-siva
Copy link
Contributor

@a-siva a-siva commented May 26, 2020

@tomoerlemans010 please see the comment in #3671 (comment).

This particular issue is closed, please add comments to other open issues related to background execution or create a new one with the issues that you are facing.

@VrundaAgs

This comment was marked as off-topic.

@zanderso

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Milestone 3
Plugins - Rich Ecosystem (collin, kro...
Linked pull requests

Successfully merging a pull request may close this issue.

None yet