ekasetiawans / flutter_background_service

262 stars 183 forks source link

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

Open Farouk615 opened 1 month ago

Farouk615 commented 1 month ago

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_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<!--bg location-->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
<!-- For apps with targetSDK=31 (Android 12) -->
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
<application
    android:label="Ally"
    android:name="${applicationName}"
    android:icon="@mipmap/ic_launcher">
    <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

syderbit commented 1 month ago

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