MaikuB / flutter_local_notifications

A Flutter plugin for displaying local notifications on Android, iOS, macOS and Linux
2.45k stars 1.39k forks source link

Notifications not running as Isolate #296

Closed rydal closed 5 years ago

rydal commented 5 years ago

Hi Maiku,

Thanks for such a useful plugin. I accept i might have something wrong here, i find the notificaitons run ok when the application is there but not as an isolate. When the user kills the app the notifications cease to be displayed by the isolate.

I expect the notifications to be shown but, whilst the FlutterIsolate continues to run the notifications are not being shown. I belive this might be related to the context but i'm not sure.

Code is below:

import 'dart:isolate';

import 'package:flutter/material.dart';
import 'package:local_notifications/local_notifications.dart';
import 'dart:async';

void main() {
      runApp(new MyApp());
}
class MyApp extends StatefulWidget {
  @override
  MyAppState createState() => new MyAppState();

}

class MyAppState extends State<MyApp> {

  String _imageUrl = 'https://flutter.io/images/catalog-widget-placeholder.png';
  String _text;
  bool loggingEnabled = false;

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text('Notification example'),
        ),
        body: new Center(
          child: new Column(
            children: <Widget>[

              new Text(_text ??
                  "Click a notification with a payload to see the results here")
            ],
          ),
        ),
      ),
    );
  }

  static void isolated(String args) {
    Timer.periodic(Duration(seconds: 5), (timer) async
    {
      await LocalNotifications.createNotification(
          id: 0,
          title: 'Multiple actions',
          content: '... and unique callbacks and/or payloads for each');
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Isolate.spawn(isolated,"hi");

  }
}

I tried moving the spawn thread to the main class. If you or anyone can advise on how to make the applications persist that would be great.

Thanks, Rob.

MaikuB commented 5 years ago

From my understanding, isolates like that do not work when the app isn't running. Invoking code whilst the app is terminated relies on native APIs. What I've seen others do is rely on the background_fetch plugin. It only supports headless execution (i.e. the ability to execute Dart code without the app running) on Android though. It's not something I've used but if this is sufficient for what you need to do then I would suggest you ask on, say, Stack Overflow given this is more of an issues list so the chances of the community see your request help is more slim.

rydal commented 5 years ago

Thanks for your reply. Just to confirm we are on the same page the notifications can't be run as an isolate? If you want i can provide more code. The example was a test case scenario.

My hope was that after the app was killed the isolate would persist to show notifications.

Thanks.

Rob.

MaikuB commented 5 years ago

Correct but I should clarify since it appears you may have not understood what I wrote. Flutter won't be able to run your code in an isolate when the app has been terminated at all without setup on the side of the native platform. This is why I suggested looking at the background_fetch plugin. A member of the Flutter team had written an article on what's involved as well but may be a bit out of date https://medium.com/flutter/executing-dart-in-the-background-with-flutter-plugins-and-geofencing-2b3e40a1a124