0

我正在为警报应用程序使用 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">

我不知道究竟是什么问题,但我尝试了很多东西,但它不起作用。

4

1 回答 1

0

我看到你正在设置fullScreenIntent: true,,所以基于官方文档:

如果您的应用程序需要能够安排全屏意图通知,请将以下属性添加到您正在打开的活动中。对于 Flutter 应用程序,通常只有一个活动从 FlutterActivity 扩展。这些属性确保屏幕在设备锁定时打开并显示。

<activity
    android:showWhenLocked="true"
    android:turnScreenOn="true">

确保您已将此添加到您的AndroidManifest.xml.

于 2021-06-12T19:17:22.567 回答