Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.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
Flatter Firebase消息-应用程序打开时推送通知不显示_Firebase_Flutter_Push Notification - Fatal编程技术网

Flatter Firebase消息-应用程序打开时推送通知不显示

Flatter Firebase消息-应用程序打开时推送通知不显示,firebase,flutter,push-notification,Firebase,Flutter,Push Notification,我是新来的flutter,我只是想接收firebase推送通知到我的flutter应用程序。推送通知在应用程序关闭时在后台接收。但当应用程序打开时,推送通知正在接收,但它不会显示警报通知(如果我的应用程序已打开,我希望将推送通知标题和正文显示为警报)。这是我的代码 _fcm.configure( onMessage: (Map<String, dynamic> message) async { showDialog( context:

我是新来的flutter,我只是想接收firebase推送通知到我的flutter应用程序。推送通知在应用程序关闭时在后台接收。但当应用程序打开时,推送通知正在接收,但它不会显示警报通知(如果我的应用程序已打开,我希望将推送通知标题和正文显示为警报)。这是我的代码

_fcm.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");
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");
        
      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
      },
    );
\u fcm.configure(
onMessage:(映射消息)异步{
显示对话框(
上下文:上下文,
生成器:(上下文)=>AlertDialog(
内容:ListTile(
标题:文本(消息['notification']['title']),
字幕:文本(消息['notification']['body']),
),
行动:[
扁平按钮(
子项:文本('Ok'),
onPressed:()=>Navigator.of(context.pop(),
),
],
),
);
打印(“onMessage:$message”);
},
onLaunch:(映射消息)异步{
打印(“onLaunch:$message”);
},
onResume:(映射消息)异步{
打印(“onResume:$message”);
},
);

有人能帮我吗?

FCM为您提供三次回调
OnResume
OnLaunch
OnMessage

当应用程序位于前台时,会触发
onMessage
,让您有机会执行任何自定义操作

为了在应用程序处于前台时显示通知,您可以使用 包裹

由于onMessage回调中缺少上下文,您可能无法看到警报对话框。尝试将
\u fcm.配置
内部

SchedulerBinding.instance.addPostFrameCallback((_){ [_fcm.configure block] });

FCM为您提供三个回调
OnResume
OnLaunch
OnMessage

当应用程序位于前台时,会触发
onMessage
,让您有机会执行任何自定义操作

为了在应用程序处于前台时显示通知,您可以使用 包裹

由于onMessage回调中缺少上下文,您可能无法看到警报对话框。尝试将
\u fcm.配置
内部

SchedulerBinding.instance.addPostFrameCallback((_){ [_fcm.configure block] });

最后,我能够通过使用overlay\u-support包来管理我的问题

我已转介以下问题连结:

我按照下面的教程和程序包来处理我的问题

教程:

包装:

我将我的
MaterialApp()
小部件包装在
overlysupport()
小部件中

return OverlaySupport(
            child: MaterialApp(....
               
          ));
然后我将
showOverlynotification
添加到我的_fcm.configure-->onMessage:

_fcm.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
        showOverlayNotification((context) {
          return Card(
            margin: const EdgeInsets.symmetric(horizontal: 4),
            child: SafeArea(
              child: ListTile(
                leading: SizedBox.fromSize(
                    size: const Size(40, 40),
                    child: ClipOval(
                        child: Container(
                      color: Colors.black,
                    ))),
                title: Text(message['notification']['title']),
                subtitle: Text(message['notification']['body']),
                trailing: IconButton(
                    icon: Icon(Icons.close),
                    onPressed: () {
                      OverlaySupportEntry.of(context).dismiss();
                    }),
              ),
            ),
          );
        }, duration: Duration(milliseconds: 4000));

        print(message['notification']['title']);
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");
      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
      },
    );
\u fcm.configure(
onMessage:(映射消息)异步{
打印(“onMessage:$message”);
showOverlynotification((上下文){
回程卡(
边距:常量边集。对称(水平:4),
儿童:安全区(
孩子:ListTile(
前导:SizedBox.fromSize(
大小:常数大小(40,40),
孩子:斜坡(
子:容器(
颜色:颜色,黑色,
))),
标题:文本(消息['notification']['title']),
字幕:文本(消息['notification']['body']),
尾随:图标按钮(
图标:图标(Icons.close),
已按下:(){
overlySupportEntry.of(context.disclose();
}),
),
),
);
},持续时间:持续时间(毫秒:4000));
打印(消息['notification']['title']);
},
onLaunch:(映射消息)异步{
打印(“onLaunch:$message”);
},
onResume:(映射消息)异步{
打印(“onResume:$message”);
},
);

最后,我能够通过使用覆盖支持包来管理我的问题

我已转介以下问题连结:

我按照下面的教程和程序包来处理我的问题

教程:

包装:

我将我的
MaterialApp()
小部件包装在
overlysupport()
小部件中

return OverlaySupport(
            child: MaterialApp(....
               
          ));
然后我将
showOverlynotification
添加到我的_fcm.configure-->onMessage:

_fcm.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
        showOverlayNotification((context) {
          return Card(
            margin: const EdgeInsets.symmetric(horizontal: 4),
            child: SafeArea(
              child: ListTile(
                leading: SizedBox.fromSize(
                    size: const Size(40, 40),
                    child: ClipOval(
                        child: Container(
                      color: Colors.black,
                    ))),
                title: Text(message['notification']['title']),
                subtitle: Text(message['notification']['body']),
                trailing: IconButton(
                    icon: Icon(Icons.close),
                    onPressed: () {
                      OverlaySupportEntry.of(context).dismiss();
                    }),
              ),
            ),
          );
        }, duration: Duration(milliseconds: 4000));

        print(message['notification']['title']);
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");
      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
      },
    );
\u fcm.configure(
onMessage:(映射消息)异步{
打印(“onMessage:$message”);
showOverlynotification((上下文){
回程卡(
边距:常量边集。对称(水平:4),
儿童:安全区(
孩子:ListTile(
前导:SizedBox.fromSize(
大小:常数大小(40,40),
孩子:斜坡(
子:容器(
颜色:颜色,黑色,
))),
标题:文本(消息['notification']['title']),
字幕:文本(消息['notification']['body']),
尾随:图标按钮(
图标:图标(Icons.close),
已按下:(){
overlySupportEntry.of(context.disclose();
}),
),
),
);
},持续时间:持续时间(毫秒:4000));
打印(消息['notification']['title']);
},
onLaunch:(映射消息)异步{
打印(“onLaunch:$message”);
},
onResume:(映射消息)异步{
打印(“onResume:$message”);
},
);
当应用程序位于前台时,用户收到通知时,您可以利用该软件包显示一个snackBar

_fcm.configure(
  onMessage: (Map<String, dynamic> message) async {
    Get.snackbar("message['notification']['title']", snackPosition: SnackPosition.TOP,);
  },
  onLaunch: (Map<String, dynamic> message) async {
    print("onLaunch: $message");
  },
  onResume: (Map<String, dynamic> message) async {
    print("onResume: $message");
  },
);
\u fcm.configure(
onMessage:(映射消息)异步{
Get.snackbar(“message['notification']['title']”,snackPosition:snackPosition.TOP,);
},
onLaunch:(映射消息)异步{
打印