微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在后台/终止的应用程序中运行 Android 本机代码

如何解决在后台/终止的应用程序中运行 Android 本机代码

我的主要想法是在 Flutter 端通过 FCM 接收推送通知,并在 Android 原生代码端启动一个可定制的通知。 我已经设法在原生 Android 代码中创建了我的通知,并通过 MethodChannel 的 invokeMethod 调用 Flutter。 当应用程序在前台时,没有问题,我可以实时接收推送通知显示在本机代码中实现的通知

这里的问题是当应用程序在后台时,我通过 FCM 接收通知数据,onBackgroundMessage 方法没有任何问题,但我无法执行 invokeMethod 来显示通知,因为它给了我 MissingPluginException 错误

firebaseCloudMessagingListener(BuildContext context) {
    //* iOS Config
    _messaging.requestNotificationPermissions(IosNotificationSettings(sound: true,badge: true,alert: true));

    _messaging.onIosSettingsRegistered.listen((event) {
      print('Registered: $event');
    });

    _messaging.configure(
        onBackgroundMessage: Platform.isIOS ? null : onBackgroundMessageHandler,//* When app is in foreground (open)
        onMessage: (Map<String,dynamic> message) async {
          NotificationHandler.showNotification(message['data']); //* This works
        },//* When app is in background
        onResume: (Map<String,dynamic> message) async {},//* When app is closed
        onLaunch: (Map<String,dynamic> message) async {});
  }

  static Future onBackgroundMessageHandler(Map<String,dynamic> message) async {
    if (message['data'] != null) {
      dynamic data = message['data'];
      print("Receive notification in background");
      NotificationHandler.showNotification(Map<String,dynamic>.from(data)); //! This gives MissingPluginException error
    }

    return Future<void>.value();
  }

显示通知方法

static void showNotification(var message) async {
    const channel = MethodChannel('channel.luis/notifications');

    channel.invokeMethod('showNotification',{
      "title": message['title'],"description": message['body'],"type": message['ntfType'],});
  }

日志:

I/Flutter ( 4160): Receive notification in background
E/Flutter ( 4160): [ERROR:Flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: MissingPluginException(No implementation found for method showNotification on channel channel.luis/notifications)
E/Flutter ( 4160): #0      MethodChannel._invokeMethod
package:Flutter/…/services/platform_channel.dart:157
E/Flutter ( 4160): <asynchronous suspension>
E/Flutter ( 4160): #1      MethodChannel.invokeMethod
package:Flutter/…/services/platform_channel.dart:332
E/Flutter ( 4160): #2      FirebaseNotifications.onBackgroundMessageHandler
package:fact_mobile_app/firebase/firebase_notification_handler.dart:53
E/Flutter ( 4160): #3      _fcmSetupBackgroundChannel.<anonymous closure>
package:firebase_messaging/firebase_messaging.dart:38
E/Flutter ( 4160): #4      MethodChannel._handleAsMethodCall
package:Flutter/…/services/platform_channel.dart:430
E/Flutter ( 4160): #5      MethodChannel.setMethodCallHandler.<anonymous closure>
package:Flutter/…/services/platform_channel.dart:383
E/Flutter ( 4160): #6      _DefaultBinaryMessenger.handlePlatformMessage
package:Flutter/…/services/binding.dart:283
E/Flutter ( 4160): #7      _invoke3.<anonymous closure> (dart:ui/hooks.dart:280:15)
E/Flutter ( 4160): #8      _rootRun (dart:async/zone.dart:1190:13)
E/Flutter ( 4160): #9      _CustomZone.run (dart:async/zone.dart:1093:19)
E/Flutter ( 4160): #10     _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/Flutter ( 4160): #11     _invoke3 (dart:ui/hooks.dart:279:10)
E/Flutter ( 4160): #12     _dispatchPlatformMessage (dart:ui/hooks.dart:154:5)
E/Flutter ( 4160):

有人可以帮我吗?谢谢!

解决方法

你为什么不使用

flutter_local_notifications(https://pub.dev/packages/flutter_local_notifications)

awesome_notification(https://pub.dev/packages/awesome_notifications)

弹出通知的插件?

特别是 awrsome_notification 插件为您的通知提供了许多自定义因素,例如,操作按钮、颜色、图标、大图片、媒体播放器、进度条等。但是您必须知道,如果您想要自定义 fcm 通知,fcm 消息应该只包含数据图

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?