capacitor-community / background-geolocation

A Capacitor plugin that sends you geolocation updates, even while the app is in the background.
MIT License
180 stars 56 forks source link

Notification is not present upon going to background #18

Closed EpicVidak closed 3 years ago

EpicVidak commented 3 years ago

Only thing i have found is in logcat from Android Studio:

I have tried adding :

MainActivity.java:

import android.os.Bundle;

import com.getcapacitor.BridgeActivity;
import com.getcapacitor.Plugin;

import java.util.ArrayList;

import com.equimaps.capacitor_background_geolocation.BackgroundGeolocation;

public class MainActivity extends BridgeActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Initializes the Bridge
    this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
      // Additional plugins you've installed go here
      // Ex: add(TotallyAwesomePlugin.class);
      add(BackgroundGeolocation.class);
    }});
  }
}

AndroidManifest.xml:

<application
        android:usesCleartextTraffic="true"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <service
            android:name="com.equimaps.capacitor_background_geolocation.BackgroundGeolocationService"
            android:enabled="true"
            android:exported="true"
            android:foregroundServiceType="location" />

        <activity
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"
            android:name="com.enonsolutions.taxi.driver.MainActivity"
            android:label="@string/title_activity_main"
            android:theme="@style/AppTheme.NoActionBarLaunch"
            android:launchMode="singleTask">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="@string/custom_url_scheme" />
            </intent-filter>

        </activity>

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"></meta-data>
        </provider>
    </application>

Implementation:

let watcherId = BackgroundGeolocation.addWatcher(
        {
          backgroundMessage: "Cancel to prevent battery drain.",
          backgroundTitle: "Tracking You.",
          requestPermissions: true,
          stale: false,
          distanceFilter: 0,
        },
        function callback(location, error) {
          if (error) {
            console.log(error);
            if (error.code === "NOT_AUTHORIZED") {
              Modals.confirm({
                title: "Location Required",
                message:
                  "This app needs your location, " +
                  "but does not have permission.\n\n" +
                  "Open settings now?",
              }).then(function ({ value }) {
                if (value) {
                  BackgroundGeolocation.openSettings();
                }
              });
            }
            return console.error(error);
          }
          console.log("Background location:");
          console.log(location);
          return ;
        }
      );
diachedelic commented 3 years ago

Can you please clone this repo and try to run the example on your Android? That should help us rule out whether this is an incompatibility with the device or operating system.

EpicVidak commented 3 years ago

Unfortunately for me, the example app is working as intended. Which means i most likely have some configuration issue that i cannot find. Would be highly appreciated if you could give me a hint as to where to look. As i have previously mentioned, this app was previously a cordova-only project, but since the background-geo(free version) for cordova is no longer maintained, we had to migrate to capacitor after testing and confirming this plugin would serve for our purposes. Im completely stuck and have begun the process of rewriting codebase in new cordova project since we have confirmed it will work. Any advice would be helpful at this point. Thanks in advance :)

diachedelic commented 3 years ago

I did not see the permissions in your AndroidManifest.xml, are they in there?

    <!-- Permissions -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-feature android:name="android.hardware.location.gps" />
</manifest>
diachedelic commented 3 years ago

Watcher is properly added/removed (going to background/foreground respectively)

What do you mean by this? You are adding/removing watchers when the app changes between background an foreground?

EpicVidak commented 3 years ago

I did not see the permissions in your AndroidManifest.xml, are they in there?

    <!-- Permissions -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-feature android:name="android.hardware.location.gps" />
</manifest>

I just have not included whole AndroidManifest.xml because I assumed it was implied i included all necessary permissions: #

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.enonsolutions.taxi.driver">

    <application
        android:usesCleartextTraffic="true"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <service
            android:name="com.equimaps.capacitor_background_geolocation.BackgroundGeolocationService"
            android:enabled="true"
            android:exported="true"
            android:foregroundServiceType="location" />

        <activity
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"
            android:name="com.enonsolutions.taxi.driver.MainActivity"
            android:label="@string/title_activity_main"
            android:theme="@style/AppTheme.NoActionBarLaunch"
            android:launchMode="singleTask">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="@string/custom_url_scheme" />
            </intent-filter>

        </activity>

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"></meta-data>
        </provider>
    </application>

    <!-- Permissions -->

    <uses-permission android:name="android.permission.INTERNET" />
    <!-- Camera, Photos, input file -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- Geolocation API -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-feature android:name="android.hardware.location.gps" />
    <!-- Network API -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <!-- Navigator.getUserMedia -->
    <!-- Video -->
    <uses-permission android:name="android.permission.CAMERA" />
    <!-- Audio -->
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
</manifest>

Watcher is properly added/removed (going to background/foreground respectively)

What do you mean by this? You are adding/removing watchers when the app changes between background an foreground?

Correct. When app goes in background i add a watcher and when it comes back into foreground i remove it.

edit: i copied AndroidManifest from previous experiments

EpicVidak commented 3 years ago

Watcher is properly added/removed (going to background/foreground respectively)

What do you mean by this? You are adding/removing watchers when the app changes between background an foreground?

I have made a test in which:

Result is that it runs properly as intended. Do you have any idea how to implement so that i can addWatcher from background?

diachedelic commented 3 years ago

That's interesting, it seems Android does not permit the app to post the notification once the app is backgrounded. Can you please explain your use case? I'd like to understand why you only need to listen for location updates in the background.