3

我试图使用 Firebase 云评估,在此过程中,我只想在推送通知到达时向用户显示一个弹出对话框。showDialog但是为了显示对话,我们需要将上下文对象作为is的参数之一BuildContext

我尝试了很多方法,但没有奏效。截至目前,我的代码如下所示:

_firebaseMessaging.configure(
        onMessage: (Map<String, dynamic> message) {
          print('onMessage: $message');
          return;
        },
        onBackgroundMessage: myBackgroundMessageHandler,
        onResume: (Map<String, dynamic> message) {
          print('onResume: $message');
          return;
        },
        onLaunch: (Map<String, dynamic> message) {
          print('onLaunch: $message');
            Text('onLaunch: $message'),
          );
          return;
        });

注意:此代码是在一个单独的类中编写的,我试图在没有任何第三部分库的情况下实现它。

4

2 回答 2

0

我解决了一个类似的问题,它显示了一个 SnackBar 而不是一个对话框。使用此代码,您可以在应用程序中的任何位置启动 SnackBar 或 Dialog。

import 'package:collectio/pages/init_screen_page.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'helper.dart';

void main() {
  Provider.debugCheckInvalidValueType = null;
  runApp(App());
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
      // here the context refers to MaterialApp widget,
      // you can´t call Scaffold.of(context)
        body: Builder(builder: (context) {
          // here the context refers to Scaffold widget
          Helper.startFirebaseMessaging(context);
          return InitScreenPage();
        }),
    ));
  }
}

class Helper {
  static startFirebaseMessaging(BuildContext buildContext) {
    final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
        var notification = message['notification'];

        // build the snackbar
        final snackBar = SnackBar(
          content: Text(notification['title']),
          action: SnackBarAction(
              label: 'Ok',
              onPressed: (){}
          ),
        );

        try {
          Scaffold.of(buildContext).showSnackBar(snackBar);
        } catch (e) {
          print(e);
        }
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");
      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
      },
    );
    _firebaseMessaging.requestNotificationPermissions(
        const IosNotificationSettings(
            sound: true, badge: true, alert: true, provisional: true));
    _firebaseMessaging.onIosSettingsRegistered
        .listen((IosNotificationSettings settings) {
      print("Settings registered: $settings");
    });
    _firebaseMessaging.getToken().then((String token) {
      assert(token != null);
      print("Push Messaging token: $token");;
    });
    print("Waiting for token...");
  }
}

也许您可以将其应用于您的代码。

于 2020-07-01T18:31:43.523 回答
0

首先,您不能在没有有效上下文的情况下显示对话框。你为什么不像这样简单地将 a 传递BuildContext给你的班级?

class SeparateClass {
  final BuildContext context;

  SeparateClass(this.context);

  void configure() {
    // your rest of the configuration code
    // you can use showDialog(context, ...) here
  }
}
于 2020-05-07T12:23:13.027 回答