Jerry Yao
Jerry Yao

Reputation: 91

Unhandled Exception: PlatformException(exact_alarms_not_permitted, Exact alarms are not permitted, null, null)

I am developing a flutter app that uses flutter_local_notifications in order to send daily notifications to users. Currently, when running the flutter app on my emulator, I get this error:

E/flutter ( 9973): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(exact_alarms_not_permitted, Exact alarms are not permitted, null, null)

and the app freezes. This is the code that is causing the error:

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,

  );
}

I tried running the app on my emulator, and expected it to send periodic notifications based on the interval I specified. However, the app freezes and fails to run.

Upvotes: 9

Views: 9393

Answers (3)

Arikaran S
Arikaran S

Reputation: 1

I also faced the same issue, If the app uses scheduling notification with exact timing and the permission have not enabled in android manifest file then this issue might come:

I have fixed it by adding these permission in my (appName/android/app/src/main/AndroidManifest.xml):

Add the following permissions under the manifest tag:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />

Add the necessary receivers under the application tag:

<receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
    <receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
        </intent-filter>
    </receiver>

Result:

notification.png

Upvotes: 0

GraSim
GraSim

Reputation: 4230

If you app is not a clock or an alarm, you may not be allowed to use <uses-permission android:name="android.permission.USE_EXACT_ALARM" /> in your manifest. To handle the PlatformException you can using this androidScheduleMode: AndroidScheduleMode.inexactAllowWhileIdle, instead of androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle

Upvotes: 5

Raunak
Raunak

Reputation: 210

I encountered the same error recently and managed to solve it by making the following modifications to the file "AndroidManifest.xml" located in the directory "android\app\src\main".

Open the file and navigate to the "<manifest>" tag. Inside this tag, add the following lines of code:

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

Your modified "AndroidManifest.xml" file should look like this:

<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>

Once you have made these changes, save the file and close your Android Studio or Visual Studio Code (whichever you are using). After reopening the IDE, rerun the entire application.

You can find more information on the permissions used in the following sources:

[1]: Android Developer - SCHEDULE_EXACT_ALARM
[2]: Android Developer - USE_EXACT_ALARM

I hope this helps!

Upvotes: 20

Related Questions