扑 firebase_messaging
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,),
),
),
);
}
}
- 已在 Firebase 控制台中注册项目以重新接收消息。
- 当我的应用程序处于活动状态(在前台)时从 Firebase 控制台发送消息。它工作完美 - 我可以看到调试消息
onMessage: {notification: {title: My title, body: My message body}, data: {}}
- 当我的应用程序在后台时从 Firebase 控制台发送消息。我可以看到 Firebase 通知。但是当我点击它时,我的应用程序返回到活动状态(回到前台)并且没有调试消息
onResume: {notification: {title: My title, body: My message body}, data: {}}
你能告诉我我的错在哪里吗?
我做错了什么?
firebase_messaging
需要更多配置,并在发送消息时在 firebase 控制台中做一些额外的工作。
在firebase控制台的"Other options"中,你需要像这样添加一个新的键值对:
"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
!
希望对您有所帮助!
我正在尝试在我的 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,),
),
),
);
}
}
- 已在 Firebase 控制台中注册项目以重新接收消息。
- 当我的应用程序处于活动状态(在前台)时从 Firebase 控制台发送消息。它工作完美 - 我可以看到调试消息
onMessage: {notification: {title: My title, body: My message body}, data: {}}
- 当我的应用程序在后台时从 Firebase 控制台发送消息。我可以看到 Firebase 通知。但是当我点击它时,我的应用程序返回到活动状态(回到前台)并且没有调试消息
onResume: {notification: {title: My title, body: My message body}, data: {}}
你能告诉我我的错在哪里吗? 我做错了什么?
firebase_messaging
需要更多配置,并在发送消息时在 firebase 控制台中做一些额外的工作。
在firebase控制台的"Other options"中,你需要像这样添加一个新的键值对:
"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
!
希望对您有所帮助!