[英]Flutter firebase_messaging
我正在嘗試在我的 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: {}}
你能告訴我我的錯在哪里嗎? 我做錯了什么?
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
!
我希望這可以幫助你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.