flutter_overlay_window icon indicating copy to clipboard operation
flutter_overlay_window copied to clipboard

Not able to receive message from flutter_background_service

Open ugirishm opened this issue 2 years ago • 3 comments

When background service is added with flutter_background_service, able to send data using FlutterBackgroundService.invoke, but not able to receive data using FlutterBackgroundService.on.

FlutterBackgroundService.on works fine when implemented in non-overlay screens.

ugirishm avatar Oct 16 '23 16:10 ugirishm

Hi @ugirishm you need to use shareData method to share data from variables to overlay and to get data on overlay you need to use overlayListener so that you can listen to the data and get it and update data on the overlay. I hope you'll find this helpfull.

share data example:

if (!(await FlutterOverlayWindow.isPermissionGranted())) return; if (await FlutterOverlayWindow.isActive()) return;

await FlutterOverlayWindow.showOverlay( enableDrag: true, flag: OverlayFlag.defaultFlag, visibility: NotificationVisibility.visibilityPublic, height: 300, width: WindowSize.matchParent, ); await Future.delayed(const Duration(milliseconds: 200)); await FlutterOverlayWindow.shareData('$order,$pNumber');

Listen Data Example: String firstTitle = ''; String secTitle = '';

@override void initState() { super.initState(); FlutterOverlayWindow.overlayListener.listen((event) { log("$event"); setState(() { // isGold = !isGold; print('true call event $event'); if (event.toString().contains(',')) { String data = event.toString();

      try {
        print('try to set data');
        firstTitle = data.split(',').first;
        secTitle = data.split(',')[1];
      } catch (e) {
        print('exception $e');
        firstTitle = '';
        secTitle = '';
      }
      print('after all result on overlay: $firstTitle     $secTitle');
    }
  });
});
setState(() {});
Future.delayed(const Duration(milliseconds: 400), () {
  setState(() {});
});

}

M-ImranNawaz avatar Oct 25 '23 07:10 M-ImranNawaz

@M-ImranNawaz How to do the opposite direction? Share data from the overlay. The above method doesn't work as the listen doesn't get any events. Thanks

YoavSl avatar May 07 '24 11:05 YoavSl

@M-ImranNawaz Thank you for the solution, I followed it and it worked

Ngochiendev avatar Jun 09 '24 03:06 Ngochiendev