【问题标题】:Daily recurring notifications in flutter颤振中的每日定期通知
【发布时间】:2020-09-03 12:14:36
【问题描述】:

我正在尝试使用插件flutter_local_notifications 每天向用户发送定期通知,但插件可用的资源仅包含代码且难以理解,我已对插件进行了如下设置:

我通过参考大量资源(如medium 和其他一些网站)做了很多工作,所以有人可以编写一个每天向用户(Android 和 iOS)发送重复通知的方法吗?谢谢!

【问题讨论】:

  • 你找到解决办法了吗?
  • 很抱歉我没有找到,我找到了用于预定通知但没有定期通知的资源。

标签: flutter dart push-notification hybrid-mobile-app


【解决方案1】:

你现在不应该使用 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);
}

//上面的...是常用的参数,我没写。

【讨论】:

    【解决方案2】:

    写完评论后,我做了一些研究,到目前为止这对我有用:

    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'));
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      • 2019-02-26
      • 2021-10-30
      • 1970-01-01
      • 2021-10-31
      • 2021-09-06
      • 2020-06-26
      相关资源
      最近更新 更多