通过FCM收到通知时触发

通过FCM收到通知时触发

当前,onMessage函数将显示一个AlertDialog。此功能工作正常。
但是,我想在应用程序处于前台时显示小吃店。
有人有这个例子吗?
我当前的initState:

void initState() {
super.initState();
getCurrentUser();
extractMachineIDs(); //IGNORE
getMachineStatus(0); //IGNORE
_fcm.configure(
  onMessage: (Map<String, dynamic> message) async {
    print("onMessage: $message");
    showDialog(   // I WANT A SNACKBAR HERE INSTEAD
      context: context,
      builder: (context) => AlertDialog(
        content: ListTile(
          title: Text(message['notification']['title']),
          subtitle: Text(message['notification']['body']),
        ),
        actions: <Widget>[
          FlatButton(
            child: Text('DISMISS'),
            onPressed: () => Navigator.of(context).pop(),
          ),
        ],
      ),
    );
  },
  onLaunch: (Map<String, dynamic> message) async {
    print("onLaunch: $message");
  },
  onResume: (Map<String, dynamic> message) async {
    print("onResume: $message");
    Navigator.pushNamed(context, AlertsNotifications.id);
  },
);
}

最佳答案

用它代替showDialog:

final snackBar = SnackBar(
  content: Text('Yay! A SnackBar!'),
  action: SnackBarAction(
    label: 'Some Text',
    onPressed: () {
      // Some code.
    },
  ),
);
Scaffold.of(context).showSnackBar(snackBar);

关于flutter - 通过FCM收到通知时触发 snackbar ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63613808/

10-12 06:19