[英]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,),
),
),
);
}
}
onMessage: {notification: {title: My title, body: My message body}, data: {}}
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.