I have a terrible problem with AlarmManager and FlutterLocalNotification.

When I call show() from AlarmManager callback, in order to write a scheduled notification, no notification is displayed.

I initialise the flutter_local_notification plugin in the main() function of the App in this way:

NotificationManager n = new NotificationManager();

void main() async
{
    WidgetsFlutterBinding.ensureInitialized();
    await AndroidAlarmManager.initialize();
    n.initNotificationManager();
    runApp(MainClass());
}

NotificationManager class is:

class NotificationManager
{
    FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
    AndroidInitializationSettings initializationSettingsAndroid;
    IOSInitializationSettings initializationSettingsIOS;
    InitializationSettings initializationSettings;

    void initNotificationManager()
    {
        initializationSettingsAndroid = new AndroidInitializationSettings('@mipmap/ic_launcher');
        initializationSettingsIOS = new IOSInitializationSettings();
        initializationSettings = new InitializationSettings(initializationSettingsAndroid, initializationSettingsIOS);

        flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
        flutterLocalNotificationsPlugin.initialize(initializationSettings);
    }

    void showNotificationWithDefaultSound(String title, String body, String payload)
    {
        var androidPlatformChannelSpecifics =
            new AndroidNotificationDetails('your channel id',
                                           'your channel name',
                                           'your channel description',
                                           importance: Importance.Max,
                                           priority: Priority.High);

        var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
        var platformChannelSpecifics = new NotificationDetails(androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
        flutterLocalNotificationsPlugin.show(0, title, body, platformChannelSpecifics, payload: payload);
    }
}

In the initState() of a page inside the App I make AlarmManager calls the callback in this way:

void initState()
{
    super.initState();
    AndroidAlarmManager.oneShotAt(DateTime.now().add(Duration(seconds: 5)), 0, alarmCallback);

    //......
}

The callback is:

alarmCallback()
{
    n.showNotificationWithDefaultSound("Title", "MySuperBody", "Hey, this is the Payload!");
    return;
}

How can I fix this?


Solution 1: sal65535

The solution is to migrate your project to AndroidX using Refractor in android studio.