skip to Main Content

Hi I’m trying to create a flutter app that show a notification even when the app is closed.

I managed to find some plugin related to that stuff flutter_background_service, but when I tried to replicate the code shown on the example, it shows some error related to the AndroidServiceInstance and setAsBackgroundService() saying that they’re not defined. How to fix this ? Is there any better way to achieve what I want ? thanks before.

Here’s my current code:

Future<void> initializeService() async {
  final service = FlutterBackgroundService();

  const AndroidNotificationChannel channel = AndroidNotificationChannel(
    notificationChannelId, // ID
    'MY FOREGROUND SERVICE', // Title
    description:
    'This channel is used for important notifications.', // Description
    importance: Importance.low, // importance must be at low or higher level
  );

  final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

  if (Platform.isIOS) {
    await flutterLocalNotificationsPlugin.initialize(
      const InitializationSettings(
        iOS: IOSInitializationSettings(),
      ),
    );
  }

  await flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<
      AndroidFlutterLocalNotificationsPlugin>()
      ?.createNotificationChannel(channel);

  await service.configure(
    androidConfiguration: AndroidConfiguration(
      onStart: onStart,
      autoStart: true,
      isForegroundMode: true,

      notificationChannelId: 'my_foreground',
      initialNotificationTitle: 'AWESOME SERVICE',
      initialNotificationContent: 'Initializing',
      foregroundServiceNotificationId: 888,
    ),

    iosConfiguration: IosConfiguration(
      autoStart: true,
      onForeground: onStart,
      onBackground: onIosBackground,
    ),
  );
  service.startService();
}

onStart(ServiceInstance service) async {
  DartPluginRegistrant.ensureInitialized();

  if (service is AndroidServiceInstance) {
    service.on('setAsForeground').listen((event) {
      service.setAsForegroundService();
    });
    service.on('setAsBackground').listen((event) {
      service.setAsBackgroundService();
    });
  }
  service.on('stopService').listen((event) {
    service.stopSelf();
  });
}

2

Answers


  1. try

    final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
    
    final AndroidInitializationSettings initializationSettingsAndroid = AndroidInitializationSettings('@mipmap/ic_launcher');
    
    final InitializationSettings initializationSettings = InitializationSettings(android: initializationSettingsAndroid);
    
    await flutterLocalNotificationsPlugin.initialize(initializationSettings,
              onSelectNotification: (payload) async =>
                  handleLocalNotification(payload, message: message));
          debugPrint('Got a message whilst in the foreground!');
    
    const AndroidNotificationChannel channel = AndroidNotificationChannel(
            'high_importance_channel', // id
            'High Importance Notifications', // titledescription
            importance: Importance.max,
          );
    
    await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);
    
    RemoteNotification notification = message.notification!;
    
    AndroidNotification android = message.notification!.android!; 
    
    Login or Signup to reply.
  2. i just realize after re-create the example app.

    you need to import another dependencies:

    import 'package:flutter_background_service_android/flutter_background_service_android.dart';
    

    i got same error AndroidServiceInstance and setAsBackgroundService() is not defined before add the additional package.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search