Husen Patel
Husen Patel

Reputation: 1

Local notifications are delayed even after following all necessary steps

I use awesome_notification package for setting local scheduled notification:

My problem: All notifications delay for undetermined time , sometimes for 30 seconds and sometimes even for a minute. I have also disabled battery optimization for the app in my phone. This problem is happening in both debug and release modes, and both in background and foreground. I want them to show up on exact time specified, when the time arrives in the status bar.

If there is a delay of 30 seconds to a minute, it is tolerable, but there is a 5 to 7 minute delay sometimes.

My phone has android 14

I implemented a notification service using awesome_notifications. I added a date and time picker for the user to set the due date and time, and I used NotificationCalendar.fromDate() to schedule the notification.

I also ensured the app requests notification permissions during initialization. I expected the notification to trigger precisely at the selected due date and time, showing the task's title in the notification. Although the notification is created and no errors appear, it doesn't trigger at the scheduled time.

Instead, it triggers with undetermined delay between 30 seconds to 1 and a half minute. I checked the console logs but didn't find any relevant errors nor any exception. I verified that the dueDate is correctly set and confirmed that notification permissions are granted. I also ensured the awesome_notifications setup follows the documentation. I tried to find solution but found none. Previous similar questions are about notifications not working at all, not delaying.

github repo of minimal reproducible example

Clone and run on your physical android device with Android Studio in both debug and release mode.

awesome_notification version: ^0.10.0

Output of flutter --version:

Flutter 3.24.5 • channel stable • https://github.com/flutter/flutter.git
Framework • revision dec2ee5c1f (2 weeks ago) • 2024-11-13 11:13:06 -0800
Engine • revision a18df97ca5
Tools • Dart 3.5.4 • DevTools 2.37.3

My Configuration:

AndroidManifest.xml

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application...
  

<receiver android:name="me.carda.awesome_notifications.core.broadcasters.receivers.ScheduledNotificationReceiver" android:exported="true" />
<receiver android:name="me.carda.awesome_notifications.core.broadcasters.receivers.RefreshSchedulesReceiver" android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
    </intent-filter>
</receiver>

android/app/build.gradle:

android{
...
compileSdk = 34
ndkVersion = "26.1.10909125"
...

defaultConfig{
...
minSdk = 23
targetSdk = 34
versionCode = flutter.versionCode
versionName = flutter.versionName
...

notfication_service.dart

// Initialize notifications
static Future<void> initNotifications() async {
  _notif.initialize(
    null,
    [
      NotificationChannel(
        channelKey: 'task_notif',
        channelName: 'task_notifications',
        channelDescription: 'Channel used to notify users about their tasks',
        importance: NotificationImportance.Max,
        playSound: true,
        defaultRingtoneType: DefaultRingtoneType.Notification,
        enableLights: true,
        channelShowBadge: true,
        criticalAlerts: true,
      ),
    ],
  );

  // Function used to show notification
  await _notif.createNotification(
    content: NotificationContent(
      id: task.id!,
      channelKey: 'task_notif',
      title: 'Task Due Now',
      body: task.title,
      actionType: ActionType.Default,
      payload: {
        'task': taskPayload,
      },
      notificationLayout: NotificationLayout.Default,
      category: NotificationCategory.Reminder,
      wakeUpScreen: true,
    ),
    schedule: NotificationCalendar.fromDate(
      date: task.dueDate!,
      allowWhileIdle: true,
      preciseAlarm: true,
    ),
  );
}

Upvotes: 0

Views: 95

Answers (0)

Related Questions