使用 flutter_local_notifications 安排通知
Schedule notification using flutter_local_notifications
我正在使用 flutter_local_notifications
插件向我的用户发送通知。但我需要安排通知。所以我使用 zonedSchedule()
来做到这一点。
问题是 zonedSchedule()
需要 TZDateTime,所以我需要将 DateTime 转换为 TZDateTime。我看到 this 问题,但它对我不起作用。我只需要获取用户的 Location
并传递给 TZDateTime.from(dateTime,location)
。我怎样才能得到这个位置?
提前致谢。
我刚刚问了这个问题,但我已经有了答案。对于那些使用 Location
有问题的人,您可以使用 tz.getLocation(await FlutterNativeTimezone.getLocalTimezone())
来获取 TZDateTime.from(dateTime,location)
的 location
参数。
PS.: 记得导入:import 'package:timezone/data/latest.dart' as tz;
和
import 'package:timezone/timezone.dart' as tz;
.
此外,您必须初始化 tz
:
tz.initializeTimeZones();
tz.setLocalLocation(tz.getLocation(await FlutterNativeTimezone.getLocalTimezone()));
就这些了,希望对你有用
我正在使用 flutter_local_notifications
插件向我的用户发送通知。但我需要安排通知。所以我使用 zonedSchedule()
来做到这一点。
问题是 zonedSchedule()
需要 TZDateTime,所以我需要将 DateTime 转换为 TZDateTime。我看到 this 问题,但它对我不起作用。我只需要获取用户的 Location
并传递给 TZDateTime.from(dateTime,location)
。我怎样才能得到这个位置?
提前致谢。
我刚刚问了这个问题,但我已经有了答案。对于那些使用 Location
有问题的人,您可以使用 tz.getLocation(await FlutterNativeTimezone.getLocalTimezone())
来获取 TZDateTime.from(dateTime,location)
的 location
参数。
PS.: 记得导入:import 'package:timezone/data/latest.dart' as tz;
和
import 'package:timezone/timezone.dart' as tz;
.
此外,您必须初始化 tz
:
tz.initializeTimeZones();
tz.setLocalLocation(tz.getLocation(await FlutterNativeTimezone.getLocalTimezone()));
就这些了,希望对你有用