首页 > 解决方案 > 从 android_alarm_manager 调用 flutter_local_notification 时出现问题,运行时会出现 MissingPluginException

问题描述

我正在尝试实现我的 Flutter 应用程序的一项功能,在该功能中,用户将通过通知横幅每 15 分钟被提醒一次。由于此提醒的间隔很长,我打算在后台运行一个后台服务,该服务将通过某种警报管理器进行接口,该管理器将执行代码以间歇性地显示通知。另外,请注意,提醒的间隔不需要精确。

我的实现计划是统一使用两个 Flutter 包(flutter_local_notificationandroid_alarm_manager)。我不能flutter_local_notifications单独使用,因为重复通知的间隔限制为一分钟、一小时、一天等,而我的应用程序必须有 15 分钟的间隔。有了这个,我从包中完全删除了调度功能,flutter_local_notification并将该责任交给android_alarm_manager.

我通过为每个包创建单独的类来使用这两个包。包的第flutter_local_notifications一个类与本文中的类类似:https ://itnext.io/local-notifications-in-flutter-6136235e1b51 。另一方面,该类在中android_alarm_manager具有以下实现notification_alarm_manager.dart

class NotificationAlarmManager {

  NotificationAlarmManager() {
    AndroidAlarmManager.initialize();
  }

  //I used interval in seconds here only for testing
  void startNotifications(int intervalInSeconds) async {
    await AndroidAlarmManager.periodic(
      Duration(seconds: intervalInSeconds),
      0,
      _showRepeatingNotification,
    );
    print('Periodic alarm initialized');
  }

  static void _showRepeatingNotification() async {
    print('Repeating notification will be shown');
    var notification = LocalNotificationsPlugin();
    await notification.showSingleNotification();
  }
}

这个类是从这个实现的函数中实例化的main

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  var alarmManager = NotificationAlarmManager();
  runApp(CushionSensorApp(alarmManager));
}

通知被触发以从状态的initState()功能开始CushionSensorApp,如下所示:

class _CushionSensorAppState extends State<CushionSensorApp> {
  @override
  void initState() {
    ...
    this.widget.alarmManager.startNotifications(15);
  }
  ...
}

应用程序构建正确,但每当android_alarm_manager执行_showRepeatingNotification()时,将重复显示主要错误和一堆其他错误:

MissingPluginException(No implementation found for method initialize on channel dexterous.com/flutter/local_notifications)
...

android_alarm_manager是的,我已经遵循了(添加权限、接收者)的所有初始要求。此外,我已经将自定义Application.java文件添加为覆盖并更改android:name.Application. 我什至尝试使用此 Github 问题https://github.com/MaikuB/flutter_local_notifications/issues/238中的 Kotlin 文件,但它仍然无法正常工作。

有关我的代码的详细信息,请参阅我的存储库 ( https://github.com/lewisbolaton/cushion_sensor_app )。

标签: javaandroidflutterbackground-service

解决方案


推荐阅读