Home > database >  How to show local notifications when app is closed in Flutter on Android device
How to show local notifications when app is closed in Flutter on Android device

Time:08-02

I have some troubles with showing sheduled local notifications in Flutter on Android device. It works when app is opened, but when app is closed notifications don't show. There is NotificationManager class that shows notifications:

class NotificationManager {

  static final NotificationManager _instance = NotificationManager._internal();
  final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

  NotificationManager._internal();

  factory NotificationManager() {
    return _instance;
  }

  Future<void> init() async {
    final AndroidInitializationSettings androidSettings =
        AndroidInitializationSettings("@drawable/ic_stat_ic_launcher");

    final InitializationSettings initializationSettings = InitializationSettings(
      android: androidSettings,
    );

    initializeTimeZones();
    await flutterLocalNotificationsPlugin.initialize(initializationSettings);
  }

  Future<void> showNotification(int id, String title, String body, int seconds) async {
    await flutterLocalNotificationsPlugin.zonedSchedule(
        id,
        title,
        body,
        TZDateTime.now(local).add(Duration(seconds: seconds)),
        _defaultDetails(),
        uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime,
        androidAllowWhileIdle: true
    );
  }

  NotificationDetails _defaultDetails() {
    return const NotificationDetails(
      android: AndroidNotificationDetails(
        'main_channel',
        "Main Channel",
        channelDescription: "Main Channel",
        importance: Importance.max,
        priority: Priority.max,
        icon: "@drawable/ic_stat_ic_launcher"
      )
    );
  }
  
}

There is how I use it in the main.dart:

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  NotificationManager().init();
  NotificationManager().showNotification(1, "Aboba", "Feed aboba", 10);
  runApp(Application());
}

So if I don't close app, it works. When I close app, notification doesn't show. How can I fix that?

P.S. There is AndroidManifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

CodePudding user response:

So, I finally found solution to this problem. Just use Workmanager package:

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  Workmanager().initialize(callback);
  Workmanager().registerOneOffTask("uniqueName", "taskName");
  runApp(Application());
}

void callback() {
  Workmanager().executeTask((taskName, inputData) {
    NotificationManager().init();
    NotificationManager().showNotification(
        id: 1,
        title: "Aboba",
        body: "Feed aboba",
        delaySeconds: 100);
    return Future.value(true);
  });
}

CodePudding user response:

Have you take a look at this package: https://pub.dev/packages/flutter_background_service

This package allow you to run code in isolates both in background and foreground.

  • Related