From d96bf2308b9225668a949eccf097e601557e29ad Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Fri, 15 Jul 2022 10:19:40 -0400 Subject: [PATCH] fix: more aggressively keep app in foreground (#554) --- android/app/src/main/AndroidManifest.xml | 3 ++- lib/models/audio.dart | 19 +++++++++++++++++++ pubspec.lock | 7 +++++++ pubspec.yaml | 1 + 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 54ccabb74..8da99debb 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,9 +1,10 @@ - + + diff --git a/lib/models/audio.dart b/lib/models/audio.dart index 975b5126a..3513e0486 100644 --- a/lib/models/audio.dart +++ b/lib/models/audio.dart @@ -4,6 +4,7 @@ import 'dart:io'; import 'package:audioplayers/audioplayers.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_background/flutter_background.dart'; import 'package:rtchat/audio_channel.dart'; import 'package:rtchat/models/adapters/profiles.dart'; import 'package:rtchat/models/channels.dart'; @@ -67,6 +68,7 @@ class AudioModel extends ChangeNotifier { _hostChannelStateSubscription = null; _isOnline = false; _syncWebViews(); + _toggleBackground(false); notifyListeners(); return; } @@ -75,10 +77,27 @@ class AudioModel extends ChangeNotifier { .listen((isOnline) { _isOnline = isOnline; _syncWebViews(); + _toggleBackground(isOnline); notifyListeners(); }); } + // this isn't really the best place to put this function since it applies + // outside audio, but it's a convenient place since we already have the listener. + void _toggleBackground(bool enable) async { + const androidConfig = FlutterBackgroundAndroidConfig( + notificationTitle: "RealtimeChat", + notificationText: "RealtimeChat is running in the background", + ); + if (enable) { + if (await FlutterBackground.initialize(androidConfig: androidConfig)) { + await FlutterBackground.enableBackgroundExecution(); + } + } else if (FlutterBackground.isBackgroundExecutionEnabled) { + await FlutterBackground.disableBackgroundExecution(); + } + } + bool get isSettingsVisible => _isSettingsVisible; set isSettingsVisible(bool value) { diff --git a/pubspec.lock b/pubspec.lock index c8192323d..bf0ef98d4 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -237,6 +237,13 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_background: + dependency: "direct main" + description: + name: flutter_background + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" flutter_inappwebview: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index ba6eed876..445e89770 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -39,6 +39,7 @@ dependencies: path_provider: ^2.0.10 in_app_purchase: ^3.0.4 firebase_database: ^9.0.18 + flutter_background: ^1.1.0 dev_dependencies: flutter_lints: ^2.0.1