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 · 134 comments

Comments

@eseidelGoogle
Contributor

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

This comment has been minimized.

Contributor

abarth commented May 2, 2016

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

@eseidelGoogle

This comment has been minimized.

Contributor

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

This comment has been minimized.

Member

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

This comment has been minimized.

Contributor

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

This comment has been minimized.

Member

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

This comment has been minimized.

Contributor

eseidelGoogle commented Jan 17, 2017

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

@mit-mit

This comment has been minimized.

Member

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

This comment has been minimized.

Member

mit-mit commented Jan 18, 2017

@mravn-google @sigurdm something to think about!

@mit-mit

This comment has been minimized.

Member

mit-mit commented Feb 2, 2017

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

@mehmetf

This comment has been minimized.

Contributor

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.

@Pushan-Gupta

This comment has been minimized.

Pushan-Gupta commented Oct 7, 2018

Do we have a clear direction from docs? I saw Release Preview 2 saying that it has support for background execution. Some examples would be good to go.

@christocracy

This comment has been minimized.

christocracy commented Oct 7, 2018

@Pushan-Gupta background-execution is a plugin-specific thing.

It depends what you want to do. Track location? Receive push notifications? Monitor geofences? Detect BT devices? Beacons? Play music?

There’s no “one ring to rule them all support for background execution”

@Pushan-Gupta

This comment has been minimized.

Pushan-Gupta commented Oct 7, 2018

@christocracy Sorry I didn't know that. I was aiming for a specific thing. Say I want to make some posts request from the app. But the connection is off. As soon as the device comes back online, no matter if the app is open or not, the queue of those posts requests must be processed.

Example: Whatsapp enqueues messages before sending them to the server. If the device is offline it stores the queue and services it as soon as it comes online.

@pauldemarco pauldemarco referenced this issue Oct 9, 2018

Closed

Protobufs #68

@xtelinco

This comment has been minimized.

xtelinco commented Oct 10, 2018

BTW I have a PR going, discussing plugin registration for background Isolates. If anyone has any thoughts...

flutter/engine#6396

@rostopira

This comment has been minimized.

rostopira commented Oct 15, 2018

What's the point of FlutterHeadlessDartRunner currently?
I've implemented background execution on Android currently (no plugin just FlutterNativeView(ctx, true)), and that's was somewhat easy to do.
But on iOS it's just crashing

func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) {
    let headless = FlutterHeadlessDartRunner.init()
    headless.run(withEntrypoint: "main") // just EXC_BAD_ACCESS here
}
@MohdLucky

This comment has been minimized.

MohdLucky commented Oct 16, 2018

Hey,
Please help in reminder app in flutter ios

@Pushan-Gupta

This comment has been minimized.

Pushan-Gupta commented Oct 17, 2018

@MohdLucky And the help you need is..?

@ethael

This comment has been minimized.

ethael commented Oct 17, 2018

@christocracy do you have update on implementation state of background_fetch plugin please?

@MohdLucky

This comment has been minimized.

MohdLucky commented Oct 17, 2018

not update on implementation state of background_fetch plugin , please provide some sample code for
running background notification.

@radvansky-tomas

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

ethael commented Nov 2, 2018

@christocracy any update regarding background_fetch plugin Chris?

@christocracy

This comment has been minimized.

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

This comment has been minimized.

Contributor

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

Contributor

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

This comment has been minimized.

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

This comment has been minimized.

christocracy commented Nov 9, 2018

My Background Fetch implementation is finally released.
background_fetch

@JuniorBee

This comment has been minimized.

JuniorBee 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

This comment has been minimized.

Contributor

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

This comment has been minimized.

ethael commented Nov 13, 2018

@eseidel challenge accepted: #24278

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment