问题
I'm looking to integrate Flutter's plugin date_time_picker with local notification.
So when I select a date&time I also schedule a notification.
Here's my code..could you help me? Now I don't have any error but code is not working.
class test1 extends StatefulWidget {
@override
_test1State createState() => _test1State();
}
class _test1State extends State {
FlutterLocalNotificationsPlugin fltrNotification;
String _selectedParam;
String task;
int val;
@override
void initState() {
super.initState();
var androidInitilize =
new AndroidInitializationSettings('icon_notification');
var iOSinitilize = new IOSInitializationSettings();
var initilizationsSettings = new InitializationSettings(
android: androidInitilize, iOS: iOSinitilize);
fltrNotification = new FlutterLocalNotificationsPlugin();
fltrNotification.initialize(initilizationsSettings,
onSelectNotification: notificationSelected);
}
Future scheuleAtParticularTime(DateTime timee) async {
// var time = DateTime(timee.day, timee.month, timee.year, timee.hour,
// timee.minute, timee.second);
var time =
DateTime(timee.year, timee.month, timee.day, timee.hour, timee.minute);
var scheduledNotificationDateTime =
new DateTime.now().add(new Duration(seconds: 5));
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'your other channel id',
'your other channel name',
'your other channel description');
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
NotificationDetails platformChannelSpecifics = new NotificationDetails(
android: androidPlatformChannelSpecifics,
iOS: iOSPlatformChannelSpecifics);
// tz.initializeTimeZones();
// tz.setLocalLocation(tz.getLocation('Italy'));
await fltrNotification.schedule(1, 'scheduled title', 'scheduled body',
scheduledNotificationDateTime, platformChannelSpecifics);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FlatButton(
onPressed: () {
DatePicker.showDateTimePicker(context, showTitleActions: true,
onChanged: (date) {
print('change $date');
}, onConfirm: (date) {
print('confirm $date');
scheuleAtParticularTime(DateTime.fromMillisecondsSinceEpoch(
date.millisecondsSinceEpoch));
}, currentTime: DateTime.now(), locale: LocaleType.it);
},
child: Text(
'Select time for notification arrival',
style:
TextStyle(color: Colors.black, fontWeight: FontWeight.w900),
),
),
],
),
),
);
}
Future notificationSelected(String payload) async {
showDialog(
context: context,
builder: (context) => AlertDialog(
content: Text("Notification Clicked $payload"),
),
);
}
}
Thanks!
ps.. I've insert full code, notifications works but at random time (about 5 second after I set it from the picker)
回答1:
Assuming you have configured the AndroidManifest.xml file, this is for setting up the plugin for Android (IOS isn't that different), note this is for version 3.0.0 of flutter_local_notifications (Last version when I checked).
First the helper class :
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:timezone/timezone.dart' as tz;
class NotificationsHelper {
static final _flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
//needs an icon
static final _initializationSettingsAndroid =
AndroidInitializationSettings('app_icon');
static final _initializationSettings =
InitializationSettings(android: _initializationSettingsAndroid);
static Future<void> init() async {
await _flutterLocalNotificationsPlugin.initialize(_initializationSettings);
tz.initializeDatabase([]);
}
static final _androidNotificationDetails = AndroidNotificationDetails(
'channel id',
'channel name',
'channel description',
importance: Importance.max,
priority: Priority.high,
);
static final _notificationDetails =
NotificationDetails(android: _androidNotificationDetails);
// set Notification methoud
static Future<void> setNotification(DateTime dateTime, int id) async {
await _flutterLocalNotificationsPlugin.zonedSchedule(
id,
'scheduled title',
'scheduled body',
tz.TZDateTime(tz.local, dateTime.year, dateTime.month, dateTime.hour,
dateTime.minute),
_notificationDetails,
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
);
}
// cancel Notification methoud
static Future<void> cancelNotification(int id) async {
await _flutterLocalNotificationsPlugin.cancel(id);
}
}
The AndroidInitializationSettings
needs an icon and it should be in android\app\src\main\res\drawable
folder, in may case the icon name is app_icon.png.
Second in the main function:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await NotificationsHelper.init();
runApp(MyApp());
}
This will initialize the plugin.
Now anywhere in your app you can call NotificationsHelper.setNotification()
to set the notification or NotificationsHelper.cancelNotification()
to cancel it.
The id should be unique because you can't have 2 or more scheduled notifications with the same id, the last notification you add will replace the old.
回答2:
Ok, I've found the problem:
await fltrNotification.schedule(1, 'scheduled title', 'scheduled body', timee, platformChannelSpecifics);
instead of
await fltrNotification.schedule(1, 'scheduled title', 'scheduled body',
scheduledNotificationDateTime, platformChannelSpecifics);
来源:https://stackoverflow.com/questions/64415592/flutter-scheduled-notification-datetime