【问题标题】:How Can action be triggered by changing object status?如何通过更改对象状态来触发动作?
【发布时间】:2019-11-22 18:00:56
【问题描述】:

我的订单有 4 个状态:准备中、待处理、交付和已交付。当订单状态从一个更改为另一个时,我想向用户显示更改发生的通知。

*我使用了本地通知插件。在此处显示的订单页面小部件中,它由上面的流触发,该流从 fire-base 获取订单。 *这就是为什么我认为每次状态发生变化时 orderPage 将再次重建,initstate 将被调用并发送带有新状态的新通知消息,但这并没有发生。

  • 另一个解决方案是使用 didchangedependency 但我没有得到不同的结果。 我知道这是一个错过的工作,但这就是我想到的。

*我真正想要的是让我监听状态的东西,当发生变化时,将调用一个函数“singleNotification”来显示通知。 任何帮助将不胜感激。

class OrderPage extends StatefulWidget {
  const OrderPage({
    Key key,
    @required this.order,

  }) : super(key: key);
  final Order order;

  @override
  OrderPageState createState() {
    return new OrderPageState();
  }
}

class OrderPageState extends State<OrderPage> {
  final DateTime now = DateTime.now().toUtc().add(
        Duration(seconds: 3),
      );
  String title = "notification";
  String msg = "";

  FlutterLocalNotificationsPlugin localNotificationsPlugin =
      FlutterLocalNotificationsPlugin();
  initializeNotifications() async {
    var initializeAndroid =
        AndroidInitializationSettings('@mipmap/ic_launcher');
    var initializeIOS = IOSInitializationSettings();
    var initSettings = InitializationSettings(initializeAndroid, initializeIOS);
    await localNotificationsPlugin.initialize(initSettings);
  }

  Future singleNotification(
      DateTime datetime, String message, String subtext, int hashcode,
      {String sound}) async {
    var androidChannel = AndroidNotificationDetails(
      'channel-id',
      'channel-name',
      'channel-description',
      importance: Importance.Max,
      priority: Priority.Max,
    );

    var iosChannel = IOSNotificationDetails();
    var platformChannel = NotificationDetails(androidChannel, iosChannel);
    localNotificationsPlugin.schedule(
        hashcode, message, subtext, datetime, platformChannel,
        payload: hashcode.toString());
  }

  @override
  void initState() {
    super.initState();
    getMsgState(widget.order.status);
    initializeNotifications();
    singleNotification(
      now,
      title,
      msg,
      98123871,
    );
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    getMsgState(widget.order.status);
    initializeNotifications();
    singleNotification(
      now,
      title,
      msg,
      98123871,
    );
  }
Widget build(BuildContext context) {
return Container();
}

String getMsgState(String orderStatus) {
    switch (orderStatus) {
      case 'pending':
        return msg = "Your order is pending";
        break;
      case 'preparing':
        return msg = "your order is currently preparing";
        break;
      case 'delivering':
        return msg = "your order is currently delivering";
        break;
      case 'delivered':
        return msg = "Your order is delivered";
      default:
        return msg = "CustomStepState.Complete";
        break;
    }
  }

【问题讨论】:

    标签: flutter listener


    【解决方案1】:

    如果我理解正确,Firebase 会知道订单状态何时更改。它会发送一个通知。你想把它展示给用户。 您可以使用 FCM 和 Firebase 应用内通知。我的一个项目有类似的要求,无论是服务器进行处理还是 Flutter 移动应用程序显示状态。我做了以下事情:

    1. 在服务器端编写了一个小代码,该代码使用用户显示消息和数据负载调用 Firebase 云消息 API。
    2. 在 Flutter 移动应用端编写代码以在应用已在前台显示通知(以应用内样式)。

    示例代码sn-p:

    _firebaseMessaging.configure(
        onMessage: (Map<String, dynamic> message) async {
          Helper.write('on message $message');
          //Map data = json.decode(message);
          this.scaffoldKey.currentState.showSnackBar(SnackBar(content: Text(message['aps']['alert']['body']),));
        },
        onResume: (Map<String, dynamic> message) async {
          Helper.write('on resume $message');
        },
        onLaunch: (Map<String, dynamic> message) async {
          Helper.write('on launch $message');
        },
      );
      analytics.logEvent(name:'firebaseCloudMessaging_Listeners_Done',parameters:null);
    }
    

    【讨论】:

    • 是的,我知道我们可以通过 fcm 发送通知,但问题在于它没有像 whatsapp 那样弹出消息,它只将它放在主题列表中,这就是我被指示使用本地通知的原因
    • 是的,这是设计使然。这就是他们给出 3 个特定事件的原因 - onMessage、onResume 和 onLaunch。一旦您有要显示的信息,您就可以使用snackBar 或弹出窗口。上面的代码 sn-p 只是打印从 Firebase 服务器接收到的信息消息。
    • 我之前没想过使用快餐栏或弹出窗口,因为我知道它们不会像我希望的那样出现“就像我的意思是 whatsapp 通知”,如果你有任何想法关于一个小部件,就像在 onmessage 中调用的通知一样。会有帮助的。
    • 您可以使用覆盖小部件。签出this
    • 好的去试试
    猜你喜欢
    • 2022-06-14
    • 1970-01-01
    • 2019-05-29
    • 2021-10-31
    • 2018-06-19
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多