Flutter isolate not showing notifications when app killed

1,257

Yeah it can't be done in Flutter alone as the isolates are spawned by the parent process and they get killed when the app is terminated.

for Android use services which get spawned and persist after the app dies.

For iOS you can run a background thread which gets spawned after a minimum amount of time, but there's no guarantee as to when that will run. You can, at Apple's discretion, run apps as audio or location based, which will allow the background thread to run continuously.

Share:
1,257
Rob Brew
Author by

Rob Brew

Updated on December 13, 2022

Comments

  • Rob Brew
    Rob Brew over 1 year

    Flutter isolates using the flutter isolate package do not show notificaitons when the app has been killed.

    Tried using the isolate package with dart:isolate, that does not work.

    import 'dart:async';
    import 'dart:isolate';
    
    
    import 'package:flutter/material.dart';
    import 'package:flutter_isolate/flutter_isolate.dart';
    'package:flutter_local_notifications/flutter_local_notifications.dart';
    FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
    
    Future onSelectNotification(String payload) async {
      if (payload != null) {
        debugPrint('notification payload: ' + payload);
      }
    }
    
      void isolated(String args) async {
        Timer.periodic(new Duration(seconds: 10), (Timer t) => print('hi!'));
        Timer.periodic(new Duration(seconds: 10), (Timer y) async
        {
          var androidPlatformChannelSpecifics = AndroidNotificationDetails(
              'your channel id', 'your channel name', 'your channel description',
              importance: Importance.Max,
              priority: Priority.High,
              ticker: 'ticker');
          var iOSPlatformChannelSpecifics = IOSNotificationDetails();
          var platformChannelSpecifics = NotificationDetails(
              androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
          await flutterLocalNotificationsPlugin.show(
              0, 'plain title', 'plain body', platformChannelSpecifics,
              payload: 'item x');
        });
    
    }
      void main() async {
    
        // initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
        // If you have skipped STEP 3 then change app_icon to @mipmap/ic_launcher
    
        var initializationSettingsAndroid =
        new AndroidInitializationSettings('appicon');
        var initializationSettingsIOS = new IOSInitializationSettings();
        var initializationSettings = new InitializationSettings(
            initializationSettingsAndroid, initializationSettingsIOS);
        flutterLocalNotificationsPlugin.initialize(initializationSettings,
            onSelectNotification: onSelectNotification);
        isolated("hi");
      }
    

    The code above is what the system sees when the app has been killed as the state classes have terminated, and no longer exist. If we can get the code above running then the isolate will still work when the app has been killed by the user in Android and iOS and the state classes are no longer there.

    Thanks in advance.

    Rob.