1

I am using flutter awesome notification.

Wish to send a notification when the app is closed (not running in background). Just like an Alarm Clock, the notification will be send at specific time. Not push from external services like firebase.

Is there a way for doing that? Or I need another package like android_alarm_manager?

s k
  • 4,342
  • 3
  • 42
  • 61

3 Answers3

1

you can use flutter_local_notification and it has a feature called scheduling-a-notification and also you need to provide timezone with timezone package to work that,

Import the timezone package

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

Initialise the time zone database

tz.initializeTimeZones();

Once the time zone database has been initialised, developers may optionally want to set a default local location/time zone, you can use https://pub.dev/packages/flutter_native_timezone to getting the local timezone of the os.

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

Assuming the local location has been set, the zonedScheduled method can then be called in a manner similar to the following code

await flutterLocalNotificationsPlugin.zonedSchedule(
0,
'scheduled title',
'scheduled body',
tz.TZDateTime.now(tz.local).add(const Duration(seconds: 5)),
const NotificationDetails(
    android: AndroidNotificationDetails('your channel id',
        'your channel name', 'your channel description')),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
    UILocalNotificationDateInterpretation.absoluteTime);
Ruchit
  • 2,137
  • 1
  • 9
  • 24
0

You can achieve this with schedule parameter when creating notification

Example:

String localTimeZone =
    await AwesomeNotifications().getLocalTimeZoneIdentifier();

AwesomeNotifications().createNotification(
  content: NotificationContent(
    payload: {},
    id: id,
    channelKey: 'scheduled',
    title: 'title',
    body: 'body',
  ),
  schedule: NotificationCalendar(
    day: day,
    month: month,
    year: year,
    second: second,
    millisecond: millisecond,
    timeZone: localTimeZone,
    repeats: false,
  ),
);
Sneh Mehta
  • 121
  • 1
  • 2
0

If using Awesome_notifications on Android with flutter, use the schedule parameter as Sneh Mehta's reply but also make sure not to have your phone with battery saver mode and run the app in release mode with "flutter run --release"

Jeremie Houet
  • 253
  • 3
  • 10