do not disconnect android when the app is in background
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:stream_chat_flutter/stream_chat_flutter.dart';
|
import 'package:stream_chat_flutter/stream_chat_flutter.dart';
|
||||||
|
|
||||||
Future<void> _handleBackgroundNotification(
|
Future<void> _handleAndroidNotification(
|
||||||
Map<String, dynamic> notification,
|
Map<String, dynamic> notification,
|
||||||
) async {
|
) async {
|
||||||
final notificationData =
|
final notificationData =
|
||||||
@@ -30,7 +30,7 @@ void main() async {
|
|||||||
final client = Client(
|
final client = Client(
|
||||||
's2dxdhpxd94g',
|
's2dxdhpxd94g',
|
||||||
logLevel: Level.INFO,
|
logLevel: Level.INFO,
|
||||||
customAndroidNotificationHandler: _handleBackgroundNotification,
|
androidNotificationHandler: _handleAndroidNotification,
|
||||||
);
|
);
|
||||||
|
|
||||||
await client.setUser(
|
await client.setUser(
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
@@ -61,6 +62,7 @@ class StreamChat extends StatefulWidget {
|
|||||||
class StreamChatState extends State<StreamChat> with WidgetsBindingObserver {
|
class StreamChatState extends State<StreamChat> with WidgetsBindingObserver {
|
||||||
Client get client => widget.client;
|
Client get client => widget.client;
|
||||||
final GlobalKey<NavigatorState> _navigatorKey = GlobalKey<NavigatorState>();
|
final GlobalKey<NavigatorState> _navigatorKey = GlobalKey<NavigatorState>();
|
||||||
|
Timer _disconnectTimer;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@@ -185,14 +187,35 @@ class StreamChatState extends State<StreamChat> with WidgetsBindingObserver {
|
|||||||
WidgetsBinding.instance.addObserver(this);
|
WidgetsBinding.instance.addObserver(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
StreamSubscription _newMessageSubscription;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||||
if (state == AppLifecycleState.paused) {
|
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) {
|
} else if (state == AppLifecycleState.resumed) {
|
||||||
if (client.wsConnectionStatus.value == ConnectionStatus.disconnected) {
|
_newMessageSubscription?.cancel();
|
||||||
NotificationService.handleIosMessageQueue(client);
|
if (_disconnectTimer?.isActive == true) {
|
||||||
client.connect();
|
_disconnectTimer.cancel();
|
||||||
|
} else {
|
||||||
|
if (client.wsConnectionStatus.value == ConnectionStatus.disconnected) {
|
||||||
|
NotificationService.handleIosMessageQueue(client);
|
||||||
|
client.connect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user