首页 > 解决方案 > 飞行模式服务颤抖

问题描述

标签: flutterbackground-service

解决方案


您是为 Android 或 IOS 开发,还是两者兼而有之?在 Android 中,您需要使用 MethodChannel 在 Flutter 和 Android 之间进行通信,在本地完成大部分工作。然后,您需要使用所需的 UI 从 Flutter 调用本机 Notification.Service。

因此,在 android 端,您的代码将如下所示:

 new MethodChannel(getFlutterView(), CHANNEL1).setMethodCallHandler(new MethodCallHandler() {
            final NotificationManager mNotificationManager = 
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


     @Override
     public void onMethodCall(@NonNull MethodCall methodCall, @NonNull Result result) {

                switch (methodCall.method) {
                    case "AIRPLANE MODE ON":
                        assert mNotificationManager != null;
          mNotificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE);
break;
}

然后在 Flutter 方面:

class AppModel extends Model {
  final _platformChannel1 =
      MethodChannel('company.com/package/name');

  Future<Null> dndOn() async {
    await _platformChannel1.invokeMethod('AIRPLANE MODE ON');
    notifyListeners();
  }

不知道IOS...

祝你好运,这比看起来容易!


推荐阅读