java - 从 android_alarm_manager 调用 flutter_local_notification 时出现问题,运行时会出现 MissingPluginException
问题描述
我正在尝试实现我的 Flutter 应用程序的一项功能,在该功能中,用户将通过通知横幅每 15 分钟被提醒一次。由于此提醒的间隔很长,我打算在后台运行一个后台服务,该服务将通过某种警报管理器进行接口,该管理器将执行代码以间歇性地显示通知。另外,请注意,提醒的间隔不需要精确。
我的实现计划是统一使用两个 Flutter 包(flutter_local_notification
和android_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 )。
解决方案
推荐阅读
- laravel-5 - 在 laravel 中提供页面的链接
- java - Java Arrays Manipulation
- r - r - 与人类死亡率数据库上的 StMoMo 包的模型拟合不佳
- c# - LINQ where clause condition in if
- teamcity - 禁用 TeamCity 内置身份验证会删除保存的密码吗?
- java - 追踪继承关系?
- c - C Accept Client Socket "Invalid Argument" Error when adding Signal Thread
- java - 从 BDDMockito 中删除实体
- c++ - 我在这里如何滥用 C++ 承诺和期货?
- python-2.7 - 使用 saltstack 如何编写自定义信标