How to invoke Flutter method from android-widget without app in foreground

We are trying to invoke Flutter code from an Android home screen widget in Kotlin, using the pigeon package. The widget is supposed to invoke Flutter code from a screen touch implemented by a PendingIntent in the AppWidgetProvider class which should call the Flutter code through the pigeon-generated Kotlin/Flutter bridge.

This works fine as long as the Intent spawning the call is in the MainActivity activity. The caveat is that this also activates the app to the foreground, which we do not want.

When we instead try to use a Service as the Intent, the pigeon bridge in Kotlin is kicked off correctly but the Flutter code is not invoked. The following code snippets show the Service-setup.

Is a foreground/background issue? Are we missing something here? What is required to get this working without app popping into the foreground?

Pigeon spec:

import 'package:pigeon/pigeon.dart';

@FlutterApi()
abstract class MessageFlutterApi {
  String flutterMethod(String operation, String? param);
}

Flutter code:
Class instantiated at main.dart level

class WidgetCallback extends interaction.MessageFlutterApi {
  WidgetCallback._internal();

  static final WidgetCallback _callback = WidgetCallback._internal();

  factory WidgetCallback() {
    interaction.MessageFlutterApi.setUp(_callback);
    return _callback;
  }

  @override
  String flutterMethod(String operation, String? param) {
    return "Received $operation ($param)";
  }
}

Kotlin – Service:

Logically we want to use a service (non-activity) as the Intent. In our scenario, onStartCommand is called successfully, the generated flutterMehod in Kotlin is executed, but corresponding method at Flutter end is not invoked

class InteractionService:Service() {
    private lateinit var flutterEngine: FlutterEngine
    var caller: MessageFlutterApi? = null

    override fun onCreate() {
        super.onCreate()

        flutterEngine = FlutterEngine(this)
        caller = MessageFlutterApi(flutterEngine.dartExecutor.binaryMessenger)
        FlutterEngineCache.getInstance().put("my_engine_id", flutterEngine)
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        intent?.action?.let { action -> caller?.flutterMethod(action, "", {}) }
        return START_STICKY
    }
    override fun onDestroy() {
        super.onDestroy()
        FlutterEngineCache.getInstance().remove("my_engine_id")
    }
}

Kotlin – AppWidgetProvider:
Intent set to InteractionService, and PendingIntent to corresponding service

class TinyTempWidget : AppWidgetProvider(){

override fun onUpdate(
    context: Context,
    appWidgetManager: AppWidgetManager,
    appWidgetIds: IntArray
) {
    super.onUpdate(context, appWidgetManager, appWidgetIds)

    val flags = PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE

    for (appWidgetId in appWidgetIds) {
        val widgetData = HomeWidgetPlugin.getData(context)

        val intent = Intent(context, InteractionService::class.java)
        val pendingIntentUpdate = PendingIntent.getService(context,0,intent.setAction("ACTION_WIDGET_UPDATE"),flags)

        val thisView = RemoteViews(context.packageName, R.layout.tiny_temp_widget_1x1).apply {
            setOnClickPendingIntent(R.id.updated, pendingIntentUpdate)
        }
        val views = RemoteViews(thisView)
        appWidgetManager.updateAppWidget(appWidgetId, views)
    }
}

AndroidManifest.xml:

    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

    <activity
        android:name=".MainActivity"
        android:launchMode="singleInstance"
    </activity>

    <service android:name=".InteractionService"
        android:exported="false"
        android:stopWithTask="false" />

pubspec.yaml:

dev_dependencies:
  pigeon: ^20.0.2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật