X-SLAYER / flutter_overlay_window

Flutter plugin for displaying flutter app over other apps on the screen
https://pub.dev/packages/flutter_overlay_window
MIT License
110 stars 118 forks source link

Open Overlay from background when app is kill state #110

Open Soheldeveloper opened 7 months ago

Soheldeveloper commented 7 months ago

I want to open Overlay from background when app is in kill state. Specially when firebase ping comes in background. So is is possible in this library or not ?

When I am trying to do this, I face following error.

**E/flutter (30813): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(error, Not allowed to start service Intent { flg=0x30000000 cmp=my.package.name/flutter.overlay.window.flutter_overlay_window.OverlayService (has extras) }: app is in background uid UidRecord{25fa346 u0a887 RCVR idle procs:1 seq(0,0,0)}, null, java.lang.IllegalStateException: Not allowed to start service Intent { flg=0x30000000 cmp=com.vivacom.mhealth/flutter.overlay.window.flutter_overlay_window.OverlayService (has extras) }: app is in background uid UidRecord{25fa346 u0a887 RCVR idle procs:1 seq(0,0,0)}

E/flutter (30813): at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1536) E/flutter (30813): at android.app.ContextImpl.startService(ContextImpl.java:1492) E/flutter (30813): at android.content.ContextWrapper.startService(ContextWrapper.java:650) E/flutter (30813): at flutter.overlay.window.flutter_overlay_window.FlutterOverlayWindowPlugin.onMethodCall(FlutterOverlayWi E/flutter (30813): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:258) E/flutter (30813): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295) E/flutter (30813): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-en E/flutter (30813): at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12) E/flutter (30813): at android.os.Handler.handleCallback(Handler.java:790) E/flutter (30813): at android.os.Handler.dispatchMessage(Handler.java:99) E/flutter (30813): at android.os.Looper.loop(Looper.java:164) E/flutter (30813): at android.app.ActivityThread.main(ActivityThread.java:6626)

E/flutter (30813): at java.lang.reflect.Method.invoke(Native Method) E/flutter (30813): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) E/flutter (30813): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811) E/flutter (30813): ) E/flutter (30813): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:652) E/flutter (30813): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:310) E/flutter (30813): E/flutter (30813): #2 FlutterOverlayWindow.showOverlay (package:flutter_overlay_window/src/overlay_window.dart:54) E/flutter (30813): E/flutter (30813): #3 _showOverLay (package:caafiya/main_flavor/main_shaafi.dart:84) E/flutter (30813): E/flutter (30813):**

Following is my code for opening overlay in main.dart

_showOverLay() async {
  //var context = MainApp.navigationKey.currentState!.context;
  if (await overlay_window.FlutterOverlayWindow.isActive()) return;
  await overlay_window.FlutterOverlayWindow.showOverlay(
    enableDrag: true,
    overlayTitle: "X-SLAYER",
    overlayContent: 'Overlay Enabled',
    flag: overlay_window.OverlayFlag.defaultFlag,
    visibility: overlay_window.NotificationVisibility.visibilityPublic,
    positionGravity: overlay_window.PositionGravity.auto,
    height: 200,
    width: overlay_window.WindowSize.matchParent,
    startPosition: const overlay_window.OverlayPosition(0, 0),
  );
}

@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage? message) async {
  debugPrint('Received Background Notification ######### $message');
  //await Firebase.initializeApp();
  if (message != null && message.data.isNotEmpty) {
    String title = message.data['title'];
    String body = message.data['body'];
    String type = message.data['type'];
    String id = message.data['id'];
    // String caseId = message.data['case_id'];
    //  String type = message.data['type'];
    // String nav = message.data['nave'] ?? '';
    final notificationPayload = NotificationPayload(type: type, id: id);
    if (Platform.isAndroid) {
      NotificationDetails notificationDetails = const NotificationDetails(
        android: AndroidNotificationDetails(
          'shaafi',
          'shaafi_channel',
          color: Colors.white,
          importance: Importance.high,
          priority: Priority.high,
          playSound: true,
        ),
      );

      if (type.isNotEmpty && id.isNotEmpty) {
        if (type == 'video') {
          _showOverLay();
        } else {
          FlutterLocalNotificationsPlugin().show(
              Random().nextInt(100), title, body, notificationDetails,
              payload: notificationPayload.toJsonString());
        }
      } else {
        FlutterLocalNotificationsPlugin().show(
            Random().nextInt(100), title, body, notificationDetails,
            payload: notificationPayload.toJsonString());
      }
    }
  }
}
chinmay4github1987 commented 7 months ago

Same issue,

  1. I am calling a BroadcastReceiver to make a phone call from outside of the app from a flutterengineplugin on data receive i am triggering the flutter_overlay_window for first time its working.
  2. Giving error on 2nd call the window is not loading giving error W/TextureView(19304): A TextureView or a subclass can only be used with hardware acceleration enabled. W/FlutterView(19304): Tried to send viewport metrics from Android to Flutter but this FlutterView was not attached to a FlutterEngine. W/FlutterView(19304): Tried to send viewport metrics from Android to Flutter but this FlutterView was not attached to a FlutterEngine. W/FlutterView(19304): Tried to send viewport metrics from Android to Flutter but this FlutterView was not attached to a FlutterEngine. D/OverLay (19304): Destroying the overlay window service
chinmay4github1987 commented 7 months ago

When app is going to sleep mode and after wake mode its crashing E/MethodChannel#x-slayer/overlay_channel( 2243): Failed to handle method call E/MethodChannel#x-slayer/overlay_channel( 2243): java.lang.IllegalStateException: Not allowed to start service Intent { flg=0x30000000 cmp=com.qspiders.flutter_project/flutter.overlay.window.flutter_overlay_window.OverlayService (has extras) }: app is in background uid UidRecord{8681324 u0a168 RCVR bg:+36m31s630ms idle change:uncached procs:1 seq(0,0,0)} E/MethodChannel#x-slayer/overlay_channel( 2243): at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1616) E/MethodChannel#x-slayer/overlay_channel( 2243): at android.app.ContextImpl.startService(ContextImpl.java:1571) E/MethodChannel#x-slayer/overlay_channel( 2243): at android.content.ContextWrapper.startService(ContextWrapper.java:669) E/MethodChannel#x-slayer/overlay_channel( 2243): at flutter.overlay.window.flutter_overlay_window.FlutterOverlayWindowPlugin.onMethodCall(FlutterOverlayWindowPlugin.java:110) E/MethodChannel#x-slayer/overlay_channel( 2243): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267) E/MethodChannel#x-slayer/overlay_channel( 2243): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295) E/MethodChannel#x-slayer/overlay_channel( 2243): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322) E/MethodChannel#x-slayer/overlay_channel( 2243): at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12) E/MethodChannel#x-slayer/overlay_channel( 2243): at android.os.Handler.handleCallback(Handler.java:883) E/MethodChannel#x-slayer/overlay_channel( 2243): at android.os.Handler.dispatchMessage(Handler.java:100) E/MethodChannel#x-slayer/overlay_channel( 2243): at android.os.Looper.loop(Looper.java:214) E/MethodChannel#x-slayer/overlay_channel( 2243): at android.app.ActivityThread.main(ActivityThread.java:7356) E/MethodChannel#x-slayer/overlay_channel( 2243): at java.lang.reflect.Method.invoke(Native Method) E/MethodChannel#x-slayer/overlay_channel( 2243): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:491) E/MethodChannel#x-slayer/overlay_channel( 2243): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:940)

abneradekz commented 6 months ago

Same issue:

E/AndroidRuntime(18128): FATAL EXCEPTION: main E/AndroidRuntime(18128): Process: br.com.neodelivery.driver, PID: 18128 E/AndroidRuntime(18128): java.lang.RuntimeException: Unable to create service flutter.overlay.window.flutter_overlay_window.OverlayService: java.lang.NullPointerException: Attempt to invoke virtual method 'io.flutter.embedding.engine.dart.DartExecutor io.flutter.embedding.engine.FlutterEngine.getDartExecutor()' on a null object reference E/AndroidRuntime(18128): at android.app.ActivityThread.handleCreateService(ActivityThread.java:4664) E/AndroidRuntime(18128): at android.app.ActivityThread.-$$Nest$mhandleCreateService(Unknown Source:0) E/AndroidRuntime(18128): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2264) E/AndroidRuntime(18128): at android.os.Handler.dispatchMessage(Handler.java:106) E/AndroidRuntime(18128): at android.os.Looper.loopOnce(Looper.java:205) E/AndroidRuntime(18128): at android.os.Looper.loop(Looper.java:294) E/AndroidRuntime(18128): at android.app.ActivityThread.main(ActivityThread.java:8177) E/AndroidRuntime(18128): at java.lang.reflect.Method.invoke(Native Method) E/AndroidRuntime(18128): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552) E/AndroidRuntime(18128): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971) E/AndroidRuntime(18128): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'io.flutter.embedding.engine.dart.DartExecutor io.flutter.embedding.engine.FlutterEngine.getDartExecutor()' on a null object reference E/AndroidRuntime(18128): at flutter.overlay.window.flutter_overlay_window.OverlayService.(OverlayService.java:58) E/AndroidRuntime(18128): at java.lang.Class.newInstance(Native Method) E/AndroidRuntime(18128): at android.app.AppComponentFactory.instantiateService(AppComponentFactory.java:129) E/AndroidRuntime(18128): at androidx.core.app.CoreComponentFactory.instantiateService(CoreComponentFactory.java:75) E/AndroidRuntime(18128): at android.app.ActivityThread.handleCreateService(ActivityThread.java:4626) E/AndroidRuntime(18128): ... 9 more

youssefm15 commented 3 months ago

@abneradekz @chinmay4github1987 @Soheldeveloper Any new updates regarding this issue or if anyone solved it?

abneradekz commented 3 months ago

@youssefm15 no for me

MalathSam1994 commented 2 months ago

Is there any update regarding this, I have the same Problem

tehokim commented 2 months ago

Hi @X-SLAYER ,

Do you have any plan on this to resolve.

Thanks