开发者社区> 问答> 正文

应用研发平台EMAS中进程杀掉的前提下,收到辅助弹窗通知,在这个方法怎么把推送内容给flutter?

应用研发平台EMAS中进程杀掉的前提下,收到辅助弹窗通知,在onSysNoticeOpened方法中怎么把推送内容给到flutter呢?

展开
收起
小易01 2023-12-12 22:46:06 31 0
1 条回答
写回答
取消 提交回答
  • 在应用研发平台EMAS中,当进程被杀掉后收到辅助弹窗通知,并希望在onSysNoticeOpened方法中将推送内容传递给Flutter,可以按照以下步骤进行:

    1. 创建MethodChannel
      在Android原生代码中创建一个MethodChannel,用于与Flutter通信。
    import io.flutter.embedding.engine.FlutterEngine;
    import io.flutter.plugin.common.MethodChannel;
    
    public class MainActivity extends FlutterActivity {
        private static final String CHANNEL_NAME = "com.example.yourapp/sys_notice_opened";
    
        private MethodChannel methodChannel;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            FlutterEngine flutterEngine = FlutterFragmentActivity.findFlutterView(this).getFlutterEngine();
            methodChannel = new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL_NAME);
        }
    
        public void onSysNoticeOpened(Context context, String title, String content, Map<String, String> extraParams) {
            // 调用Flutter的方法
            Map<String, Object> messageData = new HashMap<>();
            messageData.put("title", title);
            messageData.put("content", content);
            messageData.put("extraParams", extraParams);
            methodChannel.invokeMethod("onSysNoticeOpened", messageData);
        }
    }
    
    1. 在Flutter中接收消息
      在Flutter的Dart代码中创建一个函数来接收来自原生代码的消息。
    import 'package:flutter/services.dart';
    
    class NotificationHelper {
      static const MethodChannel _channel =
          const MethodChannel('com.example.yourapp/sys_notice_opened');
    
      static Future<void> handleSysNoticeOpened(Map<String, dynamic> message) async {
        await _channel.invokeMethod('onSysNoticeOpened', message);
      }
    }
    
    1. 在onSysNoticeOpened方法中调用Flutter方法
      onSysNoticeOpened方法中,调用NotificationHelper.handleSysNoticeOpened方法并将通知内容传递给Flutter。
    public void onSysNoticeOpened(Context context, String title, String content, Map<String, String> extraParams) {
        // 调用Flutter的方法
        Map<String, Object> messageData = new HashMap<>();
        messageData.put("title", title);
        messageData.put("content", content);
        messageData.put("extraParams", extraParams);
        methodChannel.invokeMethod("onSysNoticeOpened", messageData);
    }
    
    1. 在Flutter中处理接收到的通知内容
      在你的Flutter应用程序中,注册一个监听器来处理从原生代码接收到的onSysNoticeOpened事件。
    import 'package:flutter/material.dart';
    import 'package:your_app/notification_helper.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatefulWidget {
      
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      late MethodChannel _methodChannel;
    
      
      void initState() {
        super.initState();
        _methodChannel = MethodChannel('com.example.yourapp/sys_notice_opened');
        _setupNotificationListener();
      }
    
      void _setupNotificationListener() {
        _methodChannel.setMethodCallHandler((call) async {
          if (call.method == "onSysNoticeOpened") {
            Map<String, dynamic> message = call.arguments;
            String title = message["title"];
            String content = message["content"];
            Map<String, String> extraParams = message["extraParams"];
    
            // 处理接收到的标题、内容和额外参数
            // ...
          }
        });
      }
    
      // ...
    }
    

    这样,当用户点击辅助弹窗通知时,onSysNoticeOpened方法会被调用,然后通过MethodChannel将通知内容传递给Flutter。在Flutter中,你可以根据需要处理这些信息,例如显示一个自定义的对话框或者更新应用程序的状态。

    2023-12-29 10:57:21
    赞同 展开评论 打赏
来源圈子
更多
收录在圈子:
基于阿里巴巴以及合作伙伴的最佳实践,围绕大前端、云原生领域的相关技术热点(小程序、Serverless、应用中间件、低代码、DevOps)展开行业探讨,与开发者一起探寻云原生时代应用研发的新范式。
相关文档: 移动研发平台
问答排行榜
最热
最新

相关电子书

更多
基于flutter的产品应用实践 立即下载
《Flutter in action》 立即下载
闲鱼《Flutter 技术解析与实战》 立即下载