1

I want to execute a function at a specific time but when the app is closed. The function will display a notification.

This is the function which I want to execute:

Future<void> dailyNotification() async {
DateTime now= DateTime.now();
Time reviewTime = Time(13, 02, 20 );
DateTime notTime = DateTime(now.year, now.month, now.day, reviewTime.hour, reviewTime.minute, reviewTime.second);
var androidChannelSpecifics = AndroidNotificationDetails(
  'CHANNEL_ID 2',
  'CHANNEL_NAME 2',
  "CHANNEL_DESCRIPTION 2",
  importance: Importance.max,
  priority: Priority.high,
 );
 var iosChannelSpecifics = IOSNotificationDetails();
 var platformChannelSpecifics =
 NotificationDetails(android: androidChannelSpecifics, iOS: iosChannelSpecifics);
 await flutterLocalNotificationsPlugin.zonedSchedule(
   2,
   'Test Title at ${reviewTime.hour}:${reviewTime.minute}.${reviewTime.second}',
   'Test Body', //null
    notTime,
    platformChannelSpecifics,
    payload: 'Test Payload',
   );
 }

Edit:

As per the suggestion of Tirth Patel in the post I have implemented it like this:

void callbackDispatcher() {
 Workmanager().executeTask((taskName, inputData) async {
notificationPlugin.showDailyAtTime();
return Future.value(true);
 });
}

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Workmanager().initialize(callbackDispatcher);
Workmanager().registerPeriodicTask(
  "1",
  "simplePeriodicTask",
  frequency: Duration(minutes: 15),
);
runApp(MyApp());
}

But still, it doesn't work. I am using the flutter local notification package.

I couldn't solve this problem yet.

Please help me with this.

Thanks for your replies.

Sanchay Kasturey
  • 161
  • 1
  • 17

1 Answers1

0

Some of the Android device manufacturing company not allowing the android phone to run in the background, I am sure your device might be one of them.

Solution: we need to enable auto-start in settings for an app to keep a service running. (App info -> Auto Start -> Enable)

Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147