1

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

enter image description here

  • Permissions

enter image description here

Taki
  • 3,290
  • 1
  • 16
  • 41

3 Answers3

4

I believe you need an instance of the TZDateTime from the timezone package in order to schedule a notification.

This is a snippet from the documentation:

Starting in version 2.0 of the plugin, scheduling notifications now requires developers to specify a date and time relative to a specific time zone. This is to solve issues with daylight savings that existed in the schedule method that is now deprecated. A new zonedSchedule method is provided that expects an instance TZDateTime class provided by the timezone package. As the flutter_local_notifications plugin already depends on the timezone package, it's not necessary for developers to add the timezone package as a direct dependency. In other words, the timezone package will be a transitive dependency after you add the flutter_local_notifications plugin as a dependency in your application.

So now in your code, import the package

import 'package:timezone/data/latest_all.dart' as tz;
import 'package:timezone/timezone.dart' as tz;

Initialize the timezone database:

tz.initializeTimeZones();

Once the time zone database has been initialised, developers may optionally want to set a default local location/time zone

tz.setLocalLocation(tz.getLocation(timeZoneName));

Now you should be able to call it like this:

await _localNotificationsPlugin.zonedSchedule(
    0,
    'title',
    'body',
    tz.TZDateTime.now(tz.local).add(const Duration(seconds: 5)),
    platform,
    androidAllowWhileIdle: true,
    uiLocalNotificationDateInterpretation:
        UILocalNotificationDateInterpretation.absoluteTime);

More info can be found in the docs here

Let us know if this helps!

pblead26
  • 755
  • 1
  • 9
  • 30
2

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" />
Rohith Nambiar
  • 2,957
  • 4
  • 17
  • 37
  • Yes i did add these permissions into the manifest file and still was not showing , does it require a native android code to run like we usually do in android by creating a receiver ?? – Taki Feb 18 '22 at 17:23
1

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>"
}'
F Perroch
  • 1,988
  • 2
  • 11
  • 23
  • Oh sorry i forgot to mention that it is local notification , it has no interfaction with firebase or any online service , i basically want to schedule local notification and trigger it . – Taki Feb 18 '22 at 00:50