颤动中的每日定期通知
Daily recurring notifications in flutter
我正在尝试使用插件,flutter_local_notifications 每天向用户发送定期通知,但插件可用的资源仅包含代码且难以理解,我已经完成了设置插件如下:
- 添加了flutter_local_notifications
的依赖
- 在
AppDelegate.swift
中添加了 iOS 的权限代码
我通过参考大量资源(如 medium 和其他一些网站)来完成这些工作,所以有人可以写一个发送用户的方法吗(android & iOS)每天重复通知?谢谢!
写完那条评论后,我做了一些研究,到目前为止这对我有用:
Future<void> showAlertNotification() async {
var time = Time(8, 0, 0);
var androidChannel = AndroidNotificationDetails(
'channelID', 'channelName', 'channelDescription',
importance: Importance.defaultImportance,
priority: Priority.defaultPriority,
playSound: true);
var iosChannel = IOSNotificationDetails();
var platformChannel =
NotificationDetails(android: androidChannel, iOS: iosChannel);
await flutterLocalNotificationsPlugin.showDailyAtTime(2, 'notification title',
'message here', time, platformChannel,
payload: 'new payload'));
}
你不应该使用 showDailyAtTime 现在它有很多问题,比如与当地时区同步。因此,您应该使用分区计划,但您应该首先使用您当地的时区对其进行初始化,这可以通过另一个名为 flutter_native_timezone 的包来完成。这是我使用的工作流程:
import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest.dart' as tz;
Future<void> showNotif() async {
tz.initializeTimeZones();
String dtz = await FlutterNativeTimezone.getLocalTimezone();
if (dtz == "Asia/Calcutta") {
dtz = "Asia/Kolkata";
}
final localTimeZone = tz.getLocation(dtz);
tz.setLocalLocation(localTimeZone);
await _flutterLocalNotificationsPlugin.zonedSchedule(..., matchDateTimeComponents: DateTimeComponents.time);
}
// 上面的...是我没写的常用参数
我正在尝试使用插件,flutter_local_notifications 每天向用户发送定期通知,但插件可用的资源仅包含代码且难以理解,我已经完成了设置插件如下:
- 添加了flutter_local_notifications 的依赖
- 在
AppDelegate.swift
中添加了 iOS 的权限代码
我通过参考大量资源(如 medium 和其他一些网站)来完成这些工作,所以有人可以写一个发送用户的方法吗(android & iOS)每天重复通知?谢谢!
写完那条评论后,我做了一些研究,到目前为止这对我有用:
Future<void> showAlertNotification() async {
var time = Time(8, 0, 0);
var androidChannel = AndroidNotificationDetails(
'channelID', 'channelName', 'channelDescription',
importance: Importance.defaultImportance,
priority: Priority.defaultPriority,
playSound: true);
var iosChannel = IOSNotificationDetails();
var platformChannel =
NotificationDetails(android: androidChannel, iOS: iosChannel);
await flutterLocalNotificationsPlugin.showDailyAtTime(2, 'notification title',
'message here', time, platformChannel,
payload: 'new payload'));
}
你不应该使用 showDailyAtTime 现在它有很多问题,比如与当地时区同步。因此,您应该使用分区计划,但您应该首先使用您当地的时区对其进行初始化,这可以通过另一个名为 flutter_native_timezone 的包来完成。这是我使用的工作流程:
import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest.dart' as tz;
Future<void> showNotif() async {
tz.initializeTimeZones();
String dtz = await FlutterNativeTimezone.getLocalTimezone();
if (dtz == "Asia/Calcutta") {
dtz = "Asia/Kolkata";
}
final localTimeZone = tz.getLocation(dtz);
tz.setLocalLocation(localTimeZone);
await _flutterLocalNotificationsPlugin.zonedSchedule(..., matchDateTimeComponents: DateTimeComponents.time);
}
// 上面的...是我没写的常用参数