我正在用Flutter为iOS和Android做一个语音聊天应用。我使用Firebase Firestore在聊天室中保持活跃用户。当用户杀死一个应用程序(通过在应用程序列表中向上滑动)时,我无法检测到它。因此,无法从Firestore users
字段中删除用户。我想让用户留在firestore中,而应用程序是后台的。如何成功地知道用户何时离开应用程序?顺便说一句,我正在使用Agora RTC SDK进行语音通话。感谢您的阅读。
Firestore的结构如下
chatRoom (collection)
- docidA (document)
- users: ["name1", "name2"] (Array field)
源代码:
class RoomPage extends StatefulWidget {
const RoomPage({Key key,}) : super(key: key);
@override
_RoomPageState createState() => _RoomPageState();
}
class _RoomPageState extends State<RoomPage> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
// Join into a chat and add myself to firebase firestore users field
addMyselfToFirestore("docidA", "name1");
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() { // Unfortunately Not called when a user kills this app.
super.dispose();
removeMyselfFromFirestore("docidA", "name1");
WidgetsBinding.instance.removeObserver(this);
}
@override
build(BuildContext context) {...}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
// Unfortunately detached not called when a user kills the app
case AppLifecycleState.detached:
removeMyselfFromFirestore("docidA", "name1");
break;
}
}
}
颤动
$ flutter --version
Flutter 2.0.4 • channel stable • https://github.com/flutter/flutter.git
Framework • revision b1395592de (7 days ago) • 2021-04-01 14:25:01 -0700
Engine • revision 2dce47073a
Tools • Dart 2.12.2
发布于 2021-04-22 16:58:38
我找到了解决方法。你不能在你的应用上做什么。当你的应用被终止时,让后台检测到你的应用断开连接。现在我集成了Firestore和Firebase实时数据库。https://firebase.google.com/docs/firestore/solutions/presence
发布于 2021-04-22 15:45:50
你试过使用AppLifeCycleState吗?
您可以尝试在这里针对您的用例使用分离状态,然后触发Firebase Firestore代码。你可以在这里阅读更多关于应用程序状态的内容:https://api.flutter.dev/flutter/dart-ui/AppLifecycleState-class.html
https://stackoverflow.com/questions/67016294
复制相似问题