do not disconnect android when the app is in background

This commit is contained in:
Salvatore Giordano
2020-04-09 16:57:48 +02:00
parent 5a38908bfb
commit d6033ec2b6
2 changed files with 29 additions and 6 deletions
+2 -2
View File
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:stream_chat_flutter/stream_chat_flutter.dart';
Future<void> _handleBackgroundNotification(
Future<void> _handleAndroidNotification(
Map<String, dynamic> notification,
) async {
final notificationData =
@@ -30,7 +30,7 @@ void main() async {
final client = Client(
's2dxdhpxd94g',
logLevel: Level.INFO,
customAndroidNotificationHandler: _handleBackgroundNotification,
androidNotificationHandler: _handleAndroidNotification,
);
await client.setUser(
+27 -4
View File
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
@@ -61,6 +62,7 @@ class StreamChat extends StatefulWidget {
class StreamChatState extends State<StreamChat> with WidgetsBindingObserver {
Client get client => widget.client;
final GlobalKey<NavigatorState> _navigatorKey = GlobalKey<NavigatorState>();
Timer _disconnectTimer;
@override
Widget build(BuildContext context) {
@@ -185,14 +187,35 @@ class StreamChatState extends State<StreamChat> with WidgetsBindingObserver {
WidgetsBinding.instance.addObserver(this);
}
StreamSubscription _newMessageSubscription;
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.paused) {
client.disconnect();
if (Platform.isAndroid) {
_newMessageSubscription =
client.on(EventType.messageNew).listen((event) {
client.androidNotificationHandler({
'data': {
'message_id': event.message.id,
},
});
});
_disconnectTimer = Timer(Duration(minutes: 1), () {
client.disconnect();
});
} else {
client.disconnect();
}
} else if (state == AppLifecycleState.resumed) {
if (client.wsConnectionStatus.value == ConnectionStatus.disconnected) {
NotificationService.handleIosMessageQueue(client);
client.connect();
_newMessageSubscription?.cancel();
if (_disconnectTimer?.isActive == true) {
_disconnectTimer.cancel();
} else {
if (client.wsConnectionStatus.value == ConnectionStatus.disconnected) {
NotificationService.handleIosMessageQueue(client);
client.connect();
}
}
}
}