0

I'm using Flutter local notification plugin for alarm app, the alarm fire when the app is opened or closed but when it's in the background it's not working until it resumed even it's not at the correct time, I know it's a strange thing but it's happening

Initialization:

 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));
  }

alarm function:

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,
    );
  }

My permissions and receivers:

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

I don't know what's the problem exactly but I tried a lot of things and it's not working.

Ramez Sleem
  • 53
  • 1
  • 6

1 Answers1

0

I see you are setting the fullScreenIntent: true,, so based on the official documentation:

If your application needs the ability to schedule full-screen intent notifications, add the following attributes to the activity you're opening. For a Flutter application, there is typically only one activity extends from FlutterActivity. These attributes ensure the screen turns on and shows when the device is locked.

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

Make sure that you've added this to your AndroidManifest.xml.

mkobuolys
  • 4,499
  • 1
  • 11
  • 27
  • I already added this and it not working too. – Ramez Sleem Jun 12 '21 at 19:22
  • There is one more thing mentioned in the documentation: "For Android 8.0+, sounds and vibrations are associated with notification channels and can only be configured when they are first created. Showing/scheduling a notification will create a channel with the specified id if it doesn't exist already. If another notification specifies the same channel id but tries to specify another sound or vibration pattern then nothing occurs." Maybe just try changing the channel ID just to verify if that's not the case. – mkobuolys Jun 12 '21 at 19:36