Home > Back-end >  Notification not showing when app is closed - Flutter
Notification not showing when app is closed - Flutter

Time:02-18

I have built a todo list app where users can schedule notifications based on certain date and hour , the notifications show up without problems when the app is on foreground but when app is in background or 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'
       }
     }
   }

CodePudding user response:

You should check if your payload contains the click_action with FLUTTER_NOTIFICATION_CLICK :

{
  "notification": {
    "body": "this is a body",
    "title": "this is a title",
  },
  "data": {
    "click_action": "FLUTTER_NOTIFICATION_CLICK",
    "sound": "default", 
    "status": "done",
    "screen": "screenA",
  },
  "to": "<FCM TOKEN>"
}'

CodePudding user response:

Did you add these permissions in your AndroidManifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

Inside the application section:

<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" />
  • Related