我正在为警报应用程序使用 Flutter 本地通知插件,当应用程序打开或关闭时警报会触发,但是当它在后台时它无法工作,直到它恢复,即使它不是在正确的时间,我知道这是一件奇怪的事情,但它是正在发生
初始化:
Future<void> initNotifications() async {
final initSettingsAndroid =
const AndroidInitializationSettings("@mipmap/ic_launcher");
final initSettings = InitializationSettings(
android: initSettingsAndroid,
);
await FlutterLocalNotificationsPlugin().initialize(initSettings);
tz.initializeTimeZones();
final String currentTimeZone =
await FlutterNativeTimezone.getLocalTimezone();
tz.setLocalLocation(tz.getLocation(currentTimeZone));
}
报警功能:
void alarm(String title, String body, DateTime fireTime) async {
const androidPlatformChannalSpecifics = AndroidNotificationDetails(
"alarm notification",
"alarm notification",
"Channal for Alarm Notification",
icon: "@mipmap/ic_launcher",
sound: RawResourceAndroidNotificationSound("alarm"),
importance: Importance.max,
priority: Priority.max,
fullScreenIntent: true,
);
const notificationDetails =
NotificationDetails(android: androidPlatformChannalSpecifics);
await FlutterLocalNotificationsPlugin().zonedSchedule(
0,
title,
body,
TZDateTime.from(fireTime, tz.local),
notificationDetails,
payload: "isPressed",
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
);
}
我的权限和接收者:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<application
android:label="Prodkit"
android:icon="@mipmap/ic_launcher">
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize"
android:showWhenLocked="true"
android:turnScreenOn="true">
我不知道究竟是什么问题,但我尝试了很多东西,但它不起作用。