flutter_background_service icon indicating copy to clipboard operation
flutter_background_service copied to clipboard

foreground service used to keep sending http requests when the app is killed but now wont for new android versions

Open Farouk615 opened this issue 1 year ago • 1 comments

Im using a foreground service that have 2 main tasks : 1: keep a ble connection with a device even if the app is killed 2:keep sending http requests (every 50 seconds I have requests to send) even when the app is killed

It used to work with all android versions, now it works only for old versions ( I tried with android 8.1.0 ) and it works perfectly as expected but wont for android 13, when i pause the app or kill it no http request is sent, but when i bring it up they all send simultaneously

this is the foreground service declaration

`const notificationId = 888; final foregroundService = FlutterBackgroundService(); NotificationService notificationService = Get.put(NotificationService());

Future initializeService() async { const AndroidNotificationChannel androidChannel = AndroidNotificationChannel( notificationChannelId, // id 'Ally', // title description: 'Ally is running', // description importance: Importance.low, // importance must be at low or higher level );

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

await flutterLocalNotificationsPlugin .resolvePlatformSpecificImplementation< AndroidFlutterLocalNotificationsPlugin>() ?.createNotificationChannel(androidChannel); await foregroundService.configure( androidConfiguration: AndroidConfiguration( notificationChannelId: notificationChannelId, // this must match with notification channel you created above. initialNotificationTitle: 'Ally', initialNotificationContent: 'The application is recording', foregroundServiceNotificationId: notificationId, // this will be executed when app is in foreground or background in separated isolate onStart: onStart, // auto start service autoStart: true, isForegroundMode: true, ), iosConfiguration: IosConfiguration( // auto start service autoStart: false, // this will be executed when app is in foreground in separated isolate onForeground: onStart, // you have to enable background fetch capability on xcode project ), );`

this is the AndroidManifest.xml ` <uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation"/> <activity android:name=".MainActivity" android:exported="true" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize">

        <!-- Specifies an Android theme to apply to this Activity as soon as
             the Android process has started. This theme is visible to the user
             while the Flutter UI initializes. After that, this theme continues
             to determine the Window background behind the Flutter UI. -->
        <meta-data
            android:name="io.flutter.embedding.android.NormalTheme"
            android:resource="@style/NormalTheme"
            />
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <service
        android:name="id.flutter.flutter_background_service.BackgroundService"
        android:foregroundServiceType="connectedDevice|dataSync"
        />`

note that the foreground service launches correctly, and when i kill the app it keeps runing, only issue is http requests are paused

Farouk615 avatar Jul 12 '24 09:07 Farouk615

you can read at https://developer.android.com/training/monitoring-device-state/doze-standby

syderbit avatar Jul 18 '24 10:07 syderbit