transistorsoft / capacitor-background-geolocation

The most sophisticated background location-tracking & geofencing module with battery-conscious motion-detection intelligence for iOS and Android.
MIT License
97 stars 16 forks source link

Foreground location is not working app crashes on android 14 #280

Closed adeel399 closed 2 months ago

adeel399 commented 2 months ago

Your Environment

Installed Dependencies:

@capacitor/cli: 6.0.0 @capacitor/core: 6.1.2 @capacitor/android: 6.0.0 @capacitor/ios: 6.0.0

[success] Android looking great! đź‘Ś


```javascript <!-- syntax-highlighting:  paste your code below -->
// MainActivity.java
package be.bbconsulting.unrestricted;

import android.os.Bundle;

import com.getcapacitor.BridgeActivity;

public class MainActivity extends BridgeActivity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // registerPlugin(com.getcapacitor.community.facebooklogin.FacebookLogin.class);

     Intent serviceIntent = new Intent(this, LocationForegroundService.class);
     startForegroundService(serviceIntent); 
  }
}
//LocationForegroundServices.java

package be.bbconsulting.unrestricted;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import androidx.core.app.NotificationCompat;
import android.content.pm.ServiceInfo;
import android.os.Build;
import android.os.Build.VERSION_CODES;
public class LocationForegroundService extends Service {

    private static final String CHANNEL_ID = "LocationServiceChannel";
    private static final int NOTIFICATION_ID = 1;

    @Override
    public void onCreate() {
        super.onCreate();
        createNotificationChannel();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Notification notification = createNotification();

        // Check if the Android version is less than Android 14 (Tiramisu)
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
            // For Android versions below 14, use startForeground without service type
            startForeground(NOTIFICATION_ID, notification);
        } else {
            // For Android 14 (API level 34) and above, specify the service type (e.g., LOCATION)
            startForeground(NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION);
        }

        // Return START_STICKY to keep the service alive
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // Handle service destruction logic
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(
                CHANNEL_ID,
                "Location Service Channel",
                NotificationManager.IMPORTANCE_DEFAULT
            );
            NotificationManager manager = getSystemService(NotificationManager.class);
            if (manager != null) {
                manager.createNotificationChannel(serviceChannel);
            }
        }
    }

    private Notification createNotification() {
        // Create a notification for the foreground service
        return new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Location Service")
                .setContentText("Tracking your location")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .build();
    }
}

Expected Behavior

When i try to start recording it should work in the foreground and update my location

Actual Behavior

App crash as i start to run run background location function everything is working fine on android 13 before target api 34 after updating to 34 now app is crash as i clicked

i tried to add all permissions and well as new update for forground service type location but nothing works app still crashing

Steps to Reproduce

just update target api to 34 in variable file and you will see that crash

Context

Debug logs

Logs ``` Caused by: android.app.MissingForegroundServiceTypeException: Starting FGS without a type callerApp=ProcessRecord{a1bfbdf 10435:be.bbconsulting.unrestricted/u0a295} targetSDK=34 ```
adeel399 commented 2 months ago

i have added new issue and follow the template please help me as i already wasted 2 days on that issue please

christocracy commented 2 months ago

Plugin version:4.0.1

You’re using a very old version. See the CHANGELOG and update to latest version.

christocracy commented 2 months ago

https://github.com/transistorsoft/capacitor-background-geolocation/blob/master/CHANGELOG.md

christocracy commented 2 months ago

already wasted 2 days on that issue please

Because you’re using an old version from before targetSdkVersion 34 (I presume Google Play Store sent you a warning to update to targetSdkVerion 34). You’re not going to get away with sitting on an old version of this plug-in. You must update to the latest version.

adeel399 commented 2 months ago

in my package.json i have "cordova-background-geolocation-lt": "^4.0.1", "cordova-plugin-background-fetch": "^7.0.1",

after updating to latest npm install @transistorsoft/capacitor-background-geolocation --save npm install @transistorsoft/capacitor-background-fetch --save

i have added these but older is also there in package.json file should i remove cordova doing that does effect my code

@transistorsoft/capacitor-background-fetch": "^6.0.1", "@transistorsoft/capacitor-background-geolocation": "^6.1.0",

"cordova-background-geolocation-lt": "^4.0.1",
"cordova-plugin-background-fetch": "^7.0.1",
adeel399 commented 2 months ago

also while updating to latest do i still need to add below code in androidmenifest or that is handled automatically

''' <service android:name=".LocationForegroundService" android:enabled="true" android:exported="false" android:foregroundServiceType="location" /> '''

christocracy commented 2 months ago

Remove every plug-in containing the words “background-geolocation” in its name.

Re-install the plugin according to the Installation Steps linked in the readme here.

pretend you’re installing the plugin for the first time ever.

christocracy commented 2 months ago

And uninstall every plugin containing the words “background-fetch” as well.

adeel399 commented 2 months ago

ok i did that the removing part

''' { "name": "unrestricted", "version": "3.3.3", "author": "BB Consulting", "homepage": "https://www.unrestricted.eu/", "scripts": { "ng": "ng", "start": "ng serve", "build": "ng build", "test": "ng test", "lint": "ng lint", "e2e": "ng e2e", "postinstall": "patch-package && jetifier" }, "private": true, "dependencies": { "@angular/common": "~13.2.2", "@angular/core": "~13.2.2", "@angular/fire": "^7.3.0", "@angular/forms": "~13.2.2", "@angular/google-maps": "^13.3.3", "@angular/platform-browser": "~13.2.2", "@angular/platform-browser-dynamic": "~13.2.2", "@angular/router": "~13.2.2", "@awesome-cordova-plugins/core": "^5.41.0", "@awesome-cordova-plugins/diagnostic": "^5.41.0", "@awesome-cordova-plugins/facebook": "^5.41.0", "@awesome-cordova-plugins/file": "^5.41.0", "@awesome-cordova-plugins/file-opener": "^6.9.0", "@awesome-cordova-plugins/file-transfer": "^5.41.0", "@awesome-cordova-plugins/geolocation": "^5.41.0", "@awesome-cordova-plugins/screen-orientation": "^5.41.0", "@capacitor-community/apple-sign-in": "^6.0.0", "@capacitor-community/keep-awake": "^5.0.1", "@capacitor/android": "6.0.0", "@capacitor/app": "^6.0.0", "@capacitor/camera": "^6.0.0", "@capacitor/core": "^6.0.0", "@capacitor/device": "^6.0.0", "@capacitor/filesystem": "^6.0.0", "@capacitor/haptics": "^6.0.0", "@capacitor/ios": "6.0.0", "@capacitor/keyboard": "^6.0.0", "@capacitor/network": "^6.0.0", "@capacitor/share": "^6.0.0", "@capacitor/splash-screen": "^6.0.0", "@capacitor/status-bar": "^6.0.0", "@google/maps": "^1.1.3", "@ionic/angular": "^6.0.0", "@transistorsoft/capacitor-background-fetch": "^6.0.1", "@transistorsoft/capacitor-background-geolocation": "^6.1.0", "capacitor-firebase-auth": "^3.0.0", "cordova-plugin-facebook-connect": "^3.2.0", "cordova-plugin-file": "^6.0.2", "cordova-plugin-file-opener2": "^2.2.1", "cordova-plugin-file-transfer": "github:apache/cordova-plugin-file-transfer", "cordova-plugin-geolocation": "^4.1.0", "cordova-plugin-screen-orientation": "^3.0.2", "cordova.plugins.diagnostic": "^6.1.1", "date-fns": "^2.28.0", "easytimer.js": "^4.5.3", "es6-promise-plugin": "^4.2.2", "fast-xml-parser": "3.12.16", "firebase": "^9.6.10", "geolib": "^3.3.3", "google-maps": "^4.3.3", "ngx-image-cropper": "^6.3.2", "ngx-pipes": "^3.0.0", "ngx-skeleton-loader": "^5.0.0", "rxjs": "~6.6.0", "snazzy-info-window": "^1.1.1", "tslib": "^2.2.0", "zone.js": "~0.11.4" }, "devDependencies": { "@angular-devkit/build-angular": "~13.2.3", "@angular-eslint/builder": "~13.0.1", "@angular-eslint/eslint-plugin": "~13.0.1", "@angular-eslint/eslint-plugin-template": "~13.0.1", "@angular-eslint/template-parser": "~13.0.1", "@angular/cli": "^15.1.5", "@angular/compiler": "~13.2.2", "@angular/compiler-cli": "~13.2.2", "@angular/language-service": "~13.2.2", "@capacitor/cli": "6.0.0", "@ionic/angular-toolkit": "^6.0.0", "@types/jasmine": "~3.6.0", "@types/jasminewd2": "~2.0.3", "@types/node": "^12.11.1", "@typescript-eslint/eslint-plugin": "5.3.0", "@typescript-eslint/parser": "5.3.0", "eslint": "^7.6.0", "eslint-plugin-import": "2.22.1", "eslint-plugin-jsdoc": "30.7.6", "eslint-plugin-prefer-arrow": "1.2.2", "jasmine-core": "~3.8.0", "jasmine-spec-reporter": "~5.0.0", "jetifier": "^2.0.0", "karma": "~6.3.2", "karma-chrome-launcher": "~3.1.0", "karma-coverage": "~2.0.3", "karma-coverage-istanbul-reporter": "~3.0.2", "karma-jasmine": "~4.0.0", "karma-jasmine-html-reporter": "^1.5.0", "patch-package": "^6.4.7", "protractor": "~7.0.0", "ts-node": "~8.3.0", "typescript": "~4.4.4" }, "description": "Unrestricted" }

'''

adeel399 commented 2 months ago

after removing plugin and adding new as u mention this is what i get while building ''' Could not resolve all files for configuration ':app:debugRuntimeClasspath'.

Could not find any matches for com.transistorsoft:tsbackgroundfetch:+ as no versions of com.transistorsoft:tsbackgroundfetch are available. Searched in the following locations:

christocracy commented 2 months ago

Like I said, pretend you're installing the plugin for the first time ever. Starting with the Setup Instructions

Just follow the instructions.

adeel399 commented 2 months ago

thanks alot it worked