flutter local notifications works fine for instant notification using the mehtod show
but does not schedule a notification for future time even after 1 second while the app is open
i initialized the notifications plugin like using this function and instant notifications works great
static Future initialize(FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin)async{
var androidInitialize = new AndroidInitializationSettings('mipmap/ic_launcher');
var initializationSettings = new InitializationSettings(android:androidInitialize,);
await flutterLocalNotificationsPlugin.initialize(initializationSettings);
}
i initialized timezone and specified the local time zone
i specified the channel name ‘you_can_name_it_whatever1’ in the manifiest and added the permissions as i said it works fine for instant notification by pressing a btn to display a notification using the show method
this is the code for scheduling a notification
Future zonedScheduleNotification(String note, DateTime date, occ) async {
// IMPORTANT!!
//tz.initializeTimeZones(); --> call this before using tz.local (ideally put it in your init state)
int id = math.Random().nextInt(10000);
print(date.toString());
print(tz.TZDateTime.parse(tz.local, date.toString())
.toString());
try {
await flutterLocalNotificationsPlugin.zonedSchedule(
id,
occ,
note,
tz.TZDateTime.parse(tz.local, date.toString()),
NotificationDetails(
android: AndroidNotificationDetails(
'you_can_name_it_whatever1',
'channel_name',
playSound: true,
//sound: RawResourceAndroidNotificationSound('notification'),
importance: Importance.max,
priority: Priority.high
),
),
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle ,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
);
return id;
} catch (e) {
print("Error at zonedScheduleNotification----------------------------$e");
if (e ==
"Invalid argument (scheduledDate): Must be a date in the future: Instance of 'TZDateTime'") {
alert(context, "Select future date");
}
return -1;
}
}
then i called it inside a btn using
DateTime later = DateTime.now().add(Duration(seconds:3));;
print('____________________________________this is later');
print(later);
zonedScheduleNotification('a note',later,'well');
but nothing happens no error no notification OBS
sorry if it was a dump question, am kindof new
i tried many different things like corn with instant notification call using show and it works while the app is either active or in the background but it stops when the app is terminated
i tried the package awsome notifications but it did’t work
i tried calling schedule notification like this…
from the offical documentation
await flutterLocalNotificationsPlugin.zonedSchedule(
0,
'scheduled title',
'scheduled body',
tz.TZDateTime.now(tz.local).add(const Duration(seconds: 5)),
const NotificationDetails(
android: AndroidNotificationDetails( //
'you_can_name_it_whatever1', 'you_can_name_it_whatever1',
channelDescription: 'your channel description')),
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime);
i been at this for days now if there is another way to schedule a notification when the app is in any state active idle or terminated tell me i well use it
again sorry if it was a dump question am new
thank you
2
Answers
solved, if someone was facing the same problem try adding this to your AndroidManifest.xml file after the activity closing tag
worked like charm even if the app is terminated hope i helped
Looks like you haven’t initialized the time zone database.
You put it in the initialize function: