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

Pra*_*ima 5 push-notification firebase flutter

我是 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");
      },
    );
Run Code Online (Sandbox Code Playgroud)

有人可以帮我解决这个问题吗?

Pra*_*ima 9

最后,我能够使用overlay_support包来管理我的问题

我已经提到了以下问题链接:

Flutter - Firebase Messaging Snackbar 未显示

Flutter - 如何获取当前上下文?

我通过遵循以下教程和包来解决我的问题

教程:https://medium.com/flutter-community/in-app-notifications-in-flutter-9c1e92ea10b3

包: https: //pub.dev/packages/overlay_support/install

我将我的MaterialApp()小部件包裹在OverlaySupport()小部件中。

return OverlaySupport(
            child: MaterialApp(....
               
          ));
Run Code Online (Sandbox Code Playgroud)

然后我添加showOverlayNotification到我的 _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");
      },
    );
Run Code Online (Sandbox Code Playgroud)