我正在寻找将 Flutter 的插件 date_time_picker 与本地通知集成。
因此,当我选择日期时间时,我还会安排通知。
这是我的代码..你能帮我吗?现在我没有任何错误,但代码不起作用。
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..我已经插入了完整的代码,通知可以工作,但是是在随机时间(在我从选择器设置它后大约 5 秒)