Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
全局on_消息回调firebase颤振_Firebase_Flutter_Dart - Fatal编程技术网

全局on_消息回调firebase颤振

全局on_消息回调firebase颤振,firebase,flutter,dart,Firebase,Flutter,Dart,在我的应用程序中,我配置了firebase推送通知,配置代码如下。它显示了我在initState中实现此代码的页面上的onMessage。我在应用程序中有多页表单字段,我希望在应用程序中的任何页面上显示警报。是否有任何方法可以在每个屏幕上显示警报,而无需在每页的initState中调用函数 _firebaseMessaging.configure( onMessage: (Map<String, dynamic> message) async { show

在我的应用程序中,我配置了firebase推送通知,配置代码如下。它显示了我在
initState
中实现此代码的页面上的onMessage。我在应用程序中有多页表单字段,我希望在应用程序中的任何页面上显示警报。是否有任何方法可以在每个屏幕上显示警报,而无需在每页的
initState
中调用函数

_firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        showDialog(
                context: context,
                builder: (context) => AlertDialog(
                        content: ListTile(
                        title: Text(message['notification']['title']),
                        subtitle: Text(message['notification']['body']),
                        ),
                        actions: <Widget>[
                        FlatButton(
                            child: Text('Ok'),
                            onPressed: () => Navigator.of(context).pop(),
                        ),
                    ],
                ),
            );
        print("onMessage: $message");
        final notification = message['notification'];
        setState(() {
          messages.add(Message(
              title: notification['title'], body: notification['body']));
        });
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");

        final notification = message['data'];
        setState(
          () {
            messages.add(
              Message(
                title: '${notification['title']}',
                body: '${notification['body']}',
              ),
            );
          },
        );
        Navigator.pushNamed(context, '/notify');
      },
\u firebaseMessaging.configure(
onMessage:(映射消息)异步{
显示对话框(
上下文:上下文,
生成器:(上下文)=>AlertDialog(
内容:ListTile(
标题:文本(消息['notification']['title']),
字幕:文本(消息['notification']['body']),
),
行动:[
扁平按钮(
子项:文本('Ok'),
onPressed:()=>Navigator.of(context.pop(),
),
],
),
);
打印(“onMessage:$message”);
最终通知=消息[‘通知’];
设置状态(){
消息。添加(消息)(
标题:通知['title'],正文:通知['body']);
});
},
onLaunch:(映射消息)异步{
打印(“onLaunch:$message”);
最终通知=消息['data'];
设定状态(
() {
messages.add(
信息(
标题:“${notification['title']}”,
正文:“${notification['body']}”,
),
);
},
);
Navigator.pushNamed(上下文“/notify”);
},

您应该只调用一次FCM configure,否则您将遇到问题(无论如何,我的经验)。如果您在主屏幕的initState中调用.configure,则位于树下(从属)且您导航到并从中返回的任何页面都将通过onMessage回调对FCM通知作出反应