Flutter Scheduled Android notifications API 28

52 views Asked by At

When app state = terminated Notifications does not appear.

Error only on: Redmi note 8pro Custom virtual device Android 9.0 API 28 (I tried to create similar device).

Library: flutter_local_notifications: ^16.1.0

Notifications initialization

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

FlutterLocalNotificationsPlugin initNotifications() {
  final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

  const initializationSettingsAndroid =
      AndroidInitializationSettings('@drawable/notification_icon');
      // AndroidInitializationSettings('@mipmap/ic_launcher');
  const DarwinInitializationSettings initializationSettingsDarwin =
      DarwinInitializationSettings(
    requestAlertPermission: true,
    requestBadgePermission: true,
    requestSoundPermission: true,
  );
  flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<
          AndroidFlutterLocalNotificationsPlugin>()
      ?.requestNotificationsPermission();
  const initializationSettings = InitializationSettings(
    android: initializationSettingsAndroid,
    iOS: initializationSettingsDarwin,
  );

  return flutterLocalNotificationsPlugin..initialize(initializationSettings);
}

Add scheduled notification:

import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:get/get.dart';
import 'package:get_it/get_it.dart';
import 'package:timezone/timezone.dart' as tz;

class NotificationController extends GetxController {
  static final flutterLocalNotificationsPlugin =
      GetIt.I.get<FlutterLocalNotificationsPlugin>();

  static const _morningId = 0;
  static const _eveningId = 1;

  static const _androidPlatformChannelSpecifics = AndroidNotificationDetails(
    'your channel id',
    'your channel name',
    icon: 'notification_icon',
    importance: Importance.max,
    priority: Priority.high,
  );

  static const _platformChannelSpecifics = NotificationDetails(
    android: _androidPlatformChannelSpecifics,
    iOS: DarwinNotificationDetails(
      presentAlert: true,
      presentBadge: true,
      presentSound: true,
    ),
  );

  void setMorningNotification(bool isTomorrow) {
    flutterLocalNotificationsPlugin.zonedSchedule(
      _morningId,
      'Text',
      _getNextScheduleDate(8, isTomorrow),
      // _getNextScheduleDate(14, isTomorrow),
      _platformChannelSpecifics,
      uiLocalNotificationDateInterpretation:
          UILocalNotificationDateInterpretation.absoluteTime,
      androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
    );
  }

  void setEveningNotification(bool isTomorrow) {
    flutterLocalNotificationsPlugin.zonedSchedule(
      _eveningId,
      'Text',
      _getNextScheduleDate(20, isTomorrow),
      _platformChannelSpecifics,
      uiLocalNotificationDateInterpretation:
          UILocalNotificationDateInterpretation.absoluteTime,
      androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
    );
  }

  tz.TZDateTime _getNextScheduleDate(int hour, bool isTomorrow) {
    final now = tz.TZDateTime.now(tz.local);
    var date = tz.TZDateTime(tz.local, now.year, now.month, now.day, hour);
    if (date.isBefore(now) || isTomorrow) {
      date = date.add(const Duration(days: 1));
    }

    return date;
  }
}

Doctor: [✓] Flutter (Channel stable, 3.13.7, on macOS 13.4 22F66 darwin-arm64, locale ru-RU) • Flutter version 3.13.7 on channel stable at /Users/hehmda/development/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 2f708eb839 (3 недели назад), 2023-10-09 09:58:08 -0500 • Engine revision a794cf2681 • Dart version 3.1.3 • DevTools version 2.25.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2) • Android SDK at /Users/hehmda/Library/Android/sdk • Platform android-33, build-tools 33.0.2 • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694) • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 14E222b • CocoaPods version 1.12.1

[✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.2) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)

[✓] VS Code (version 1.83.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension can be installed from: https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (2 available) • macOS (desktop) • macos • darwin-arm64 • macOS 13.4 22F66 darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 118.0.5993.117

[✓] Network resources • All expected network resources are available.

• No issues found!

Update classpath 'com.android.tools.build:gradle:7.6.1' Update JDK Disable all sort of battery optimization, notfication restrictions, etc. Test on other devices: Pixel 2 API 33, Samsung A12, Simulator 14max pro - all fine. Checked permission state by: await Permission.notification.isGranted.then((value) Its granted...

Add all kind of permissions in AndroidManifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
    <uses-permission android:name="android.permission.USE_EXACT_ALARM" />
    <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />

...

<application
        android:label="App"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        <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>
0

There are 0 answers