简体   繁体   English

Flutter firebase_messaging

[英]Flutter firebase_messaging

I'm trying to handle firebase_messaging in my Flutter test project.我正在尝试在我的 Flutter 测试项目中处理 firebase_messaging。 What I've done.我做了什么。 1. Created simple Flutter project. 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. Registered project in Firebase console in order to reseive messages.在 Firebase 控制台中注册项目以接收消息。
  2. Sent message from Firebase console when my app in active state (in foreground).当我的应用程序在活动的 state(在前台)中时,从 Firebase 控制台发送消息。 It works perfect - I can see debug message它完美无缺 - 我可以看到调试消息
onMessage: {notification: {title: My title, body: My message body}, data: {}}
  1. Send message from Firebase console when my app in background.当我的应用程序在后台时,从 Firebase 控制台发送消息。 I could see Firebase notification.我可以看到 Firebase 通知。 But when I click on it my application returned to active state (back to foreground) and no debug message但是当我点击它时,我的应用程序返回到活动 state(回到前台)并且没有调试消息
onResume: {notification: {title: My title, body: My message body}, data: {}}

Could you suggest me where is my fault?你能告诉我我的错在哪里吗? What I've done wrong?我做错了什么?

firebase_messaging needs some more configurations and doing some extra work in the firebase console when sending the message. firebase_messaging在发送消息时需要更多的配置并在 firebase 控制台中做一些额外的工作。

In "Other options" in the firebase console, you need to add a new key-value pair like this:在 firebase 控制台的“其他选项”中,您需要添加一个新的键值对,如下所示:

"click_action": "FLUTTER_NOTIFICATION_CLICK",

Then, you need to tell your app to handle notifications with this click_action in your AndroidManifest.xml然后,您需要告诉您的应用在 AndroidManifest.xml 中使用此click_action处理通知

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

And that should be it, now your app will be able to handle the notification click and trigger onResume !应该就是这样,现在您的应用程序将能够处理通知点击并触发onResume

I hope this helps you!我希望这可以帮助你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 flutter版本与位置和firebase_messaging冲突 - flutter version conflict with location and firebase_messaging Flutter [firebase_messaging] IOS 构建错误 - Flutter [firebase_messaging] IOS Build Error 将 firebase_messaging 插件添加到 Flutter 项目 - Add firebase_messaging Plugin to Flutter Project Flutter firebase_messaging:^6.0.13 失败并出现异常: - Flutter firebase_messaging: ^6.0.13 failing with exception: Flutter 错误:错误:无法解析 'package:firebase_messaging/firebase_messaging.dart' 中的 package 'firebase_messaging' - Flutter Error: Error: Could not resolve the package 'firebase_messaging' in 'package:firebase_messaging/firebase_messaging.dart' 添加 firebase_messaging 后,Flutter 应用程序在启动时崩溃 - Flutter app crashes on start after adding firebase_messaging 如果处理后台消息,firebase_messaging flutter 插件在启动时崩溃 - firebase_messaging flutter plugin crashing on startup if handling background messages Flutter:如何在 firebase_messaging package 中定义通知通道? - Flutter: How to define notification channel in firebase_messaging package? firebase_messaging/未知:Flutter IOS 发生未知错误 - firebase_messaging/unknown: An unknown error has occured on Flutter IOS firebase_messaging &gt;=7.0.3 &lt;8.0.0-dev.7 是被禁止的。 Flutter - firebase_messaging >=7.0.3 <8.0.0-dev.7 is forbidden. Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM