I have built a todo list app where users can schedule notifications based on certain dates and hours, the notifications show up without problems when the app is in the foreground but when the app is in the background or the app is closed, they do not show up, and I cannot understand why, any reason why, Thank you.
- This is my code
late FlutterLocalNotificationsPlugin _localNotificationsPlugin;
List<Task> _list = [];
late MoorController moorController;
@override
void initState() {
super.initState();
moorController = Get.put(MoorController());
createNotification();
//showNotification();
}
void createNotification(){
_localNotificationsPlugin = FlutterLocalNotificationsPlugin();
var initializationSettingsAndroid = const AndroidInitializationSettings("@mipmap/ic_launcher");
var initializationSettingsIOs = const IOSInitializationSettings();
var initSettings = InitializationSettings(android: initializationSettingsAndroid,iOS: initializationSettingsIOs);
_localNotificationsPlugin.initialize(initSettings);
}
void showNotification() async {
var android = const AndroidNotificationDetails(
Utils.CHANNEL_ID,
Utils.CHANNEL_NAME,
channelDescription: Utils.DESCRIPTION,
priority: Priority.high,
importance: Importance.max);
var iOS = const IOSNotificationDetails();
var platform = NotificationDetails(android: android,iOS: iOS);
for (var element in moorController.tasks) {
if(element.date == getCurrentDate() && element.time == getCurrentTime()){
await _localNotificationsPlugin.schedule(
0,element.date,element.time,DateTime.now(),platform, payload : 'Welcome to todo app',androidAllowWhileIdle: true);
//0, element.date,element.time,platform, payload: 'Simple Payload'
}
}
}
- Receiver Manifest File
- Permissions