【问题标题】:How to Work zonedSchedule local notifications in flutter如何在颤振中工作 zonedSchedule 本地通知
【发布时间】:2021-03-26 03:19:37
【问题描述】:

设置为通知

习惯于 Flutter 本地通知包

https://pub.dev/packages/flutter_local_notifications

我想多次设置相同的通知

DateTime current = 2020-12-15 21:00:00.000;

NotificationUtils.showNotification1(
    time: current.subtract(Duration(days: 3)),
    );
NotificationUtils.showNotification1(
    time: current.subtract(Duration(hours: 24)),
    );
NotificationUtils.showNotification1(
    time: current.subtract(Duration(hours: 2)),
    );
NotificationUtils.showNotification1(
    time: current.subtract(Duration(minutes: 30)),
   );
NotificationUtils.showNotification1(
    time: current,
    );

本地通知功能

class NotificationUtils {
static FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;

static void configLocalNotification() {
  var androidInitilize = AndroidInitializationSettings('appstore');
  var iOSinitilize = IOSInitializationSettings();
  var initilizationsSettings = new InitializationSettings(android: androidInitilize, 
      iOS:iOSinitilize);
  flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
  flutterLocalNotificationsPlugin.initialize(initilizationsSettings,onSelectNotification: (String 
     payload) async {});
         }  

static Future showNotification1({DateTime time}) async {

var androidDetails = new AndroidNotificationDetails(
  "notification 1",
  "App Notification 1",
  "Notification 1",
  importance: Importance.max,
  priority: Priority.high,
  color: Colors.blue,
  playSound: true,
  // timeoutAfter: 5000,
);
var iSODetails = new IOSNotificationDetails();
var generalNotificationDetails =
    new NotificationDetails(android: androidDetails, iOS: iSODetails);

await flutterLocalNotificationsPlugin.zonedSchedule(
  0,
  "App",
  "Notification",
  time,
  generalNotificationDetails,
  androidAllowWhileIdle: true,
);}
static Future<void> cancelNotification() async {
 await flutterLocalNotificationsPlugin.cancel(1);}}

【问题讨论】:

  • 嗨哥们,你找到解决办法了吗?

标签: flutter dart


【解决方案1】:

据我了解,您不能将 DateTime 对象直接传递给 flutterLocalNotificationsPlugin 的方法“zonedSchedule”,因为它需要 TZDateTime 类型。所以你应该做的是根据你的时区转换你的 DateTime :

  1. 首先初始化您的时区
  2. 设置时区当前位置
import 'package:timezone/data/latest.dart' as tz;
import 'package:timezone/timezone.dart' as tz;
...
tz.initializeTimeZones();
tz.setLocalLocation(tz.getLocation('Europe/Warsaw'));
var currentDateTime = tz.TZDateTime.now().add(const Duration(hours: 10));

【讨论】:

    猜你喜欢
    • 2020-11-28
    • 2021-07-14
    • 1970-01-01
    • 2021-10-31
    • 2021-08-25
    • 2021-09-06
    • 2021-10-12
    • 2020-07-31
    相关资源
    最近更新 更多