【问题标题】:Flutter local notification int id parameterFlutter 本地通知 int id 参数
【发布时间】:2020-11-26 09:03:58
【问题描述】:

我正在使用颤振本地通知插件来通知用户事件开始。

所有事件都以日期和时间存储在 firebase 中。

如果用户将事件添加到他的日程表中,他会收到通知。

我的问题是本地通知插件在参数中需要一个 int,而在 firebase 中我的 id 类型为 String(该事件与另一个生成 String ID 的 webapp 一起添加到 firebase)。

那么有没有办法在颤振中使通知与字符串 ID 一起工作?

这是数据库:

   lass DetailScreen extends StatefulWidget {
  final String id;
  DetailScreen(this.id);

  @override
  _DetailScreenState createState() => _DetailScreenState();
}

class _DetailScreenState extends State<DetailScreen> {
  String firstHalf;

  String secondHalf;

  bool flag = true;

  int selectedValue;

  bool isScheduled = false;

  bool isLoading = true;

  initState() {
    selectedValue = 0;
    Future.delayed(Duration(seconds: 0), () async {
      await getStatus();
      final response = await flutterLocalNotificationsPlugin
          .getNotificationAppLaunchDetails();
      if (response.didNotificationLaunchApp) {
        await Provider.of<ShowsModel>(context, listen: false).getShows();
        await Provider.of<ChannelModel>(context, listen: false).getChannels();
      }
      setState(() {
        isLoading = false;
      });
    });
    super.initState();
  }

  showNotification(String title, DateTime showTime) async {
    var android = AndroidNotificationDetails(
        'channel id', 'channel name', 'channel description',
        enableVibration: true,
        playSound: true,
        importance: Importance.High,
        priority: Priority.High);
    var iOS = IOSNotificationDetails();
    var platform = NotificationDetails(android, iOS);
    new DateTime.now().add(Duration(seconds: 10));
    await flutterLocalNotificationsPlugin.schedule(
        int.parse(widget.id),
        title,
        'The show is about to start in 15 minutes',
        showTime.subtract(Duration(minutes: 15)),
        platform,
        payload: widget.id);
  }

【问题讨论】:

    标签: flutter android-notifications


    【解决方案1】:

    不幸的是,我不知道通知的 iOS 实现是什么样的,但 Android 实现与 int 一起工作。而且我认为没有办法解决它。

    您可以做的只是使用 hashcode 函数为您的 String id 生成一个 int 哈希:

    String eventID = "asdnasjdnaskdnbaihbd2in1en213123";
    int notificationId = eventID.hashCode;
    

    哈希码的文档说:

      /**
       * Returns a hash code derived from the code units of the string.
       *
       * This is compatible with [operator ==]. Strings with the same sequence
       * of code units have the same hash code.
       */
      int get hashCode;
    

    意味着只要您的 eventId 是唯一的,这应该可以正常工作。

    【讨论】:

      猜你喜欢
      • 2021-10-04
      • 2022-11-29
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      • 2021-10-12
      • 2020-12-03
      • 2022-12-07
      • 1970-01-01
      相关资源
      最近更新 更多