我正在尝试在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,),
),
),
);
}
}
onMessage: {notification: {title: My title, body: My message body}, data: {}}
onResume: {notification: {title: My title, body: My message body}, data: {}}
你能建议我我的错在哪里吗? 我做错了什么?
答案 0 :(得分: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
!
希望对您有帮助!