未处理的异常:PlatformException(exact_alarms_not_permission,不允许精确警报,null,null)

Jer*_*Yao 3 android flutter flutter-local-notification

我正在开发一个使用flutter_local_notifications的 flutter 应用程序,以便向用户发送每日通知。目前,在我的模拟器上运行 flutter 应用程序时,出现以下错误:

E / flutter(9973):[错误:flutter/runtime/dart_vm_initializer.cc(41)]未处理的异常:PlatformException(exact_alarms_not_permission,不允许精确警报,null,null)

并且应用程序冻结。这是导致错误的代码:

Future<void> repeatNotification() async {
  
  const AndroidNotificationDetails androidNotificationDetails =
      AndroidNotificationDetails(
          'repeating channel id', 'repeating channel name',
          channelDescription: 'repeating description');
  const NotificationDetails notificationDetails =
      NotificationDetails(android: androidNotificationDetails);
  
  // Create a list of greetings
  List<String> greetings = [
    "Hi!",
    "Hey there!",
    "Hello!",
    "Hi there!",
    "What's up?",
    "Howdy!",
  ];
  
  // Create a list of daily check-in messages
  List<String> checkInMessages = [
    "It's time for your daily check in.",
    "Don't forget to check in today.",
    "Remember to take a moment to check in.",
    "How are you doing today?",
    "Time for your daily mindfulness moment.",
    "Can we chat?",
    "Let's talk!",
  ];
    
  // Create a random number generator
  var rng = math.Random();
    
  // Pick a random greeting
  String randomGreeting = greetings[rng.nextInt(greetings.length)];
  
  // Pick a random check-in message
  String randomCheckInMessage = checkInMessages[rng.nextInt(checkInMessages.length)];
    
  await flutterLocalNotificationsPlugin.periodicallyShow(
    id++,
    randomGreeting,
    randomCheckInMessage,
    RepeatInterval.everyMinute,
    notificationDetails,
    androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,

  );
}
Run Code Online (Sandbox Code Playgroud)

我尝试在模拟器上运行该应用程序,并期望它根据我指定的时间间隔发送定期通知。但是,该应用程序冻结并且无法运行。

Rau*_*nak 10

我最近遇到了同样的错误,并通过对位于目录“android\app\src\main”中的文件“AndroidManifest.xml”进行以下修改来解决它。

打开文件并导航到“<manifest>”标签。在此标记内,添加以下代码行:

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"
        android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />
Run Code Online (Sandbox Code Playgroud)

您修改后的“AndroidManifest.xml”文件应如下所示:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.your_company.your_app_name">

    <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"
        android:maxSdkVersion="32" />
    <uses-permission android:name="android.permission.USE_EXACT_ALARM" />

    <!-- Rest of the code... -->
</manifest>
Run Code Online (Sandbox Code Playgroud)

完成这些更改后,保存文件并关闭 Android Studio 或 Visual Studio Code(无论您使用哪个)。重新打开 IDE 后,重新运行整个应用程序。

您可以在以下来源中找到有关所使用权限的更多信息:

[1]:Android 开发人员 - SCHEDULE_EXACT_ALARM
[2]:Android 开发人员 - USE_EXACT_ALARM

我希望这有帮助!