大家好,关于在 Android 上重新启动的快速问题: 我有一个应该安排提醒的应用程序。当我重新启动设备时,这些通知不再安排,因此我必须重新安排它们。所以我写了一个重新安排的函数。我将警报管理器与 bootListener 结合使用以在重新启动时触发回调。我需要考虑什么才能在重启时成功回调?
import 'package:android_alarm_manager_plus/android_alarm_manager_plus.dart';
import 'package:project_h2o/services/notification_service.dart';
Future<void> setupBootListener() async {
NotificationService notificationService = NotificationService();
await AndroidAlarmManager.initialize();
// Set the delay to a minimum value as we only need to trigger the callback
await AndroidAlarmManager.oneShot(
Duration(seconds: 1), // Minimal delay
0, // Unique identifier for the alarm
notificationService.rescheduleNotifications,
wakeup: true,
rescheduleOnReboot: true,
);
}
在我的主要部分,我称之为设置
void main() async {
await initializeDateFormatting();
WidgetsFlutterBinding.ensureInitialized();
await findSystemLocale();
tz.initializeTimeZones();
tz.setLocalLocation(tz.getLocation('Europe/Berlin'));
NotificationService notificationService = NotificationService();
await notificationService.initNotifications();
await setupBootListener();
runApp(MyApp());
}
我不知道你是否看过这个,但我也遇到了类似的问题。您需要进入 AndroidManifest.xml 编写几行以使警报管理器正常工作并能够在重新启动时出现
请查看入门部分中的此链接来执行此操作