Background process in flutter

24 Views Asked by At

I have created a chat application using flutter and socket. When i am removing the app from background, my socket is also losing connection. i have implemented the background service its also not working on removed application. Any solution on flutter to check for receive message?

this is my background service in flutter

class MyBackgroundService{

  static Future<void> initializeService() async {
    final service = FlutterBackgroundService();
    const AndroidNotificationChannel channel = AndroidNotificationChannel(
      notificationChannelId, 'MY FOREGROUND SERVICE',
      description: 'This channel is used for important notifications.',
      importance: Importance.low,
    );

    final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

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

    await service.configure(androidConfiguration: AndroidConfiguration(
      // this will be executed when app is in foreground or background in separated isolate
      onStart: onStart,
      // auto start service
      autoStart: true,
      isForegroundMode: false,
      notificationChannelId: notificationChannelId, // this must match with notification channel you created above.
      initialNotificationTitle: 'AWESOME SERVICE',
      initialNotificationContent: 'Initializing',
      foregroundServiceNotificationId: notificationId,
    ), iosConfiguration: IosConfiguration(),);


  }

  static Future<void> onStart(ServiceInstance service) async {

    DartPluginRegistrant.ensureInitialized();

    final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

    Timer.periodic(const Duration(seconds: 30), (timer) async {
      
      if (service is AndroidServiceInstance) {
        if (await service.isForegroundService()) {
          
          flutterLocalNotificationsPlugin.show(
            notificationId,
            'COOL SERVICE',
            'Awesome ${hubConnection.state}',
            const NotificationDetails(
              android: AndroidNotificationDetails(
                notificationChannelId,
                'MY FOREGROUND SERVICE',
                icon: 'ic_bg_service_small',
                ongoing: true,
              ),
            ),
          );
        }else{
           try{

             final url = '[offlineMessageFetchURL]';

               final response = await http.get(Uri.parse(url));
               if (response.statusCode == 200) {
                 // If the server returns a 200 OK response, parse the JSON
                 // and update the _response state with the received data.
               } else {
                 // If the server did not return a 200 OK response,
                 // throw an exception.
                 throw Exception('Failed to load data');
               }
           }catch(e){
             debugPrint(e.toString());
           }
          /// Background Service
        }
      }
    });
  }
}

0

There are 0 best solutions below