【问题标题】:Flutter firebase_messaging颤振 firebase_messaging
【发布时间】:2020-08-12 16:50:25
【问题描述】:

我正在尝试在我的 Flutter 测试项目中处理 firebase_messaging。 我做了什么。 1. 创建简单的 Flutter 项目。

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  final FirebaseMessaging _fcm = FirebaseMessaging(); // For FCM
  final GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>(); // To be used as navigator

  String _message;

  @override
  void initState() {
    /* Handle Notifications */
    _fcm.configure(
      onMessage: (Map<String, dynamic> message) async {
        debugPrint("onMessage: $message");
        setState(() {
          _message = message.toString();
        });
      },
      onLaunch: (Map<String, dynamic> message) async {
        debugPrint("onLaunch: $message");
        setState(() {
          _message = message.toString();
        });
      },
      onResume: (Map<String, dynamic> message) async {
        debugPrint("onResume: $message");
        setState(() {
          _message = message.toString();
        });
      },
    );

    _fcm.getToken().then((String token) {
      debugPrint("token: $token");
    });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorKey: navigatorKey,
      home: Scaffold(
        appBar: AppBar(
          title: Text("Firebase messages"),
        ),
        body: Container(
          child: Text(_message == null ? "NO MESSAGE YET" : _message,),
        ),
      ),
    );

  }
}
  1. 在 Firebase 控制台中注册项目以重新接收消息。
  2. 当我的应用程序处于活动状态(在前台)时,从 Firebase 控制台发送消息。它运行完美 - 我可以看到调试消息
onMessage: {notification: {title: My title, body: My message body}, data: {}}
  1. 当我的应用程序在后台时从 Firebase 控制台发送消息。我可以看到 Firebase 通知。但是当我点击它时,我的应用程序返回到活动状态(回到前台)并且没有调试消息
onResume: {notification: {title: My title, body: My message body}, data: {}}

你能告诉我我的错在哪里吗? 我做错了什么?

【问题讨论】:

    标签: firebase flutter firebase-cloud-messaging


    【解决方案1】:

    firebase_messaging 在发送消息时需要更多配置并在 Firebase 控制台中做一些额外的工作。

    在 Firebase 控制台的“其他选项”中,您需要添加一个新的键值对,如下所示:

    "click_action": "FLUTTER_NOTIFICATION_CLICK",
    

    然后,您需要告诉您的应用在 AndroidManifest.xml 中使用 click_action 处理通知

    ...
            <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    

    应该就是这样,现在您的应用将能够处理通知点击并触发onResume

    希望对你有帮助!

    【讨论】:

    • 非常感谢弗朗西斯科。我已经按照你的建议做了。现在我有 onResume 事件触发。但我收到空消息。例如onResume: {notification: {}, data: {collapse_key: ru.alexeyzhulin.firebasemessages, google.original_priority: high, google.sent_time: 1588142576210, google.delivered_priority: high, google.ttl: 2419200, from: 40921386609, click_action: FLUTTER_NOTIFICATION_CLICK, google.message_id: 0:1588142576213008%03eb999a03eb999a}}你能建议我如何获取消息内容
    • 当然,只需将两个键值对添加到带有通知标题和消息的数据标签中(再次)。 Android(无论出于何种原因)会破坏通知中的内容,因此您需要备份数据。
    • 知道了。非常感谢:)
    猜你喜欢
    • 2020-06-06
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 2021-05-01
    • 2021-11-26
    • 1970-01-01
    • 2020-01-29
    相关资源
    最近更新 更多