transistorsoft / cordova-background-geolocation-lt

The most sophisticated background location-tracking & geofencing module with battery-conscious motion-detection intelligence for iOS and Android.
http://www.transistorsoft.com/shop/products/cordova-background-geolocation
Other
659 stars 276 forks source link

Extras are not capturing consistently #1172

Closed djoyrocks closed 3 years ago

djoyrocks commented 4 years ago

Your Environment

package com.transistorsoft.cordova.bggeo;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import org.greenrobot.eventbus.Subscribe;
import org.json.JSONObject;

import com.transistorsoft.locationmanager.adapter.BackgroundGeolocation;
import com.transistorsoft.locationmanager.event.ActivityChangeEvent;
import com.transistorsoft.locationmanager.event.GeofenceEvent;
import com.transistorsoft.locationmanager.event.GeofencesChangeEvent;
import com.transistorsoft.locationmanager.event.ConnectivityChangeEvent;
import com.transistorsoft.locationmanager.event.HeadlessEvent;
import com.transistorsoft.locationmanager.event.HeartbeatEvent;
import com.transistorsoft.locationmanager.event.MotionChangeEvent;
import com.transistorsoft.locationmanager.event.LocationProviderChangeEvent;
import com.transistorsoft.locationmanager.http.HttpResponse;
import com.transistorsoft.locationmanager.location.TSLocation;
import com.transistorsoft.locationmanager.logger.TSLog;
import com.yashas.teams.MainActivity;
import com.yashas.teams.R;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import android.app.PendingIntent;
import android.app.Activity;
import android.provider.Settings;
import android.util.Log;

import static android.app.PendingIntent.getActivity;
import static android.provider.Settings.System.getString;
import static androidx.core.content.ContextCompat.getSystemService;

/**
 * BackgroundGeolocationHeadlessTask
 * This component allows you to receive events from the BackgroundGeolocation plugin in the native Android environment while your app has been *terminated*,
 * where the plugin is configured for stopOnTerminate: false.  In this context, only the plugin's service is running.  This component will receive all the same
 * events you'd listen to in the Javascript API.
 *
 * You might use this component to:
 * - fetch / post information to your server (eg: request new API key)
 * - execute BackgroundGeolocation API methods (eg: #getCurrentPosition, #setConfig, #addGeofence, #stop, etc -- you can execute ANY method of the Javascript API)
 */

public class BackgroundGeolocationHeadlessTask  {

    @Subscribe
    public void onHeadlessTask(HeadlessEvent event) {
        String name = event.getName();
        TSLog.logger.debug("\uD83D\uDC80  event (CUSTOM IMPLEMENTATION): " + event.getName());
        TSLog.logger.debug("- event: " + event.getEvent());
        // showNotification(event.getContext());

        if (name.equals(BackgroundGeolocation.EVENT_TERMINATE)) {
           // JSONObject state = event.getTerminateEvent();
//        } else if (name.equals(BackgroundGeolocation.EVENT_LOCATION)) {
//            TSLocation location = event.getLocationEvent();
//            showNotification(event.getContext());
//        } else if (name.equals(BackgroundGeolocation.EVENT_MOTIONCHANGE)) {
//            MotionChangeEvent motionChangeEvent = event.getMotionChangeEvent();
//            TSLocation location = motionChangeEvent.getLocation();
//        } else if (name.equals(BackgroundGeolocation.EVENT_HTTP)) {
//            HttpResponse response = event.getHttpEvent();
       // } else if (name.equals(BackgroundGeolocation.EVENT_PROVIDERCHANGE)) {
       //     LocationProviderChangeEvent providerChange = event.getProviderChangeEvent();
//        } else if (name.equals(BackgroundGeolocation.EVENT_PROVIDERCHANGE)) {
//            LocationProviderChangeEvent providerChange = event.getProviderChangeEvent();
//        } else if (name.equals(BackgroundGeolocation.EVENT_ACTIVITYCHANGE)) {
//            ActivityChangeEvent activityChange = event.getActivityChangeEvent();
//        } else if (name.equals(BackgroundGeolocation.EVENT_SCHEDULE)) {
//            JSONObject state = event.getScheduleEvent();
//        } else if (name.equals(BackgroundGeolocation.EVENT_BOOT)) {
//            JSONObject state = event.getBootEvent();
        } else if (name.equals(BackgroundGeolocation.EVENT_GEOFENCE)) {
            GeofenceEvent geofenceEvent = event.getGeofenceEvent();
            showNotification(event.getContext());
//        } else if (name.equals(BackgroundGeolocation.EVENT_GEOFENCESCHANGE)) {
//            GeofencesChangeEvent geofencesChangeEvent = event.getGeofencesChangeEvent();
//        } else if (name.equals(BackgroundGeolocation.EVENT_HEARTBEAT)) {
//            HeartbeatEvent heartbeatEvent = event.getHeartbeatEvent();
//        } else if (name.equals(BackgroundGeolocation.EVENT_NOTIFICATIONACTION)) {
//            String buttonId = event.getNotificationEvent();
//        } else if (name.equals(BackgroundGeolocation.EVENT_CONNECTIVITYCHANGE)) {
//            ConnectivityChangeEvent connectivityChangeEvent = event.getConnectivityChangeEvent();
//        } else if (name.equals(BackgroundGeolocation.EVENT_ENABLEDCHANGE)) {
//            boolean enabled = event.getEnabledChangeEvent();
        } else {
            TSLog.logger.warn(TSLog.warn("Unknown Headless Event: " + name));
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void createNotificationChannel(Context appContext, String CHANNEL_ID) {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = CHANNEL_ID;
            String description ="YASHAS.TEAMS.FK for Trip Notification";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = (NotificationManager) appContext.getApplicationContext().getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

    private void showNotification(Context appContext) {
        TSLog.logger.warn(TSLog.warn("showNotification: -> from Background"));

      try   {
          String CHANNEL_ID="YASHAS.TEAMS.FK";
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
              createNotificationChannel(appContext,CHANNEL_ID );
          }
          Intent intent = new Intent(appContext.getApplicationContext(), MainActivity.class);
          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
          PendingIntent pendingIntent = getActivity(appContext.getApplicationContext(), 0, intent, 0);

          NotificationCompat.Builder builder = new NotificationCompat.Builder(appContext, CHANNEL_ID)
                  .setContentTitle("Arriving at Destination")
                  .setSmallIcon(R.drawable.ic_launcher_foreground)
                  .setContentText("You are arriving at your trip destination. if you have reached your destination kindly mark trip as completed.")
                  .setContentIntent(pendingIntent)
                  .setPriority(NotificationCompat.PRIORITY_DEFAULT);

          NotificationManagerCompat notificationManager = NotificationManagerCompat.from(appContext.getApplicationContext());
          // notificationId is a unique int for each notification that you must define
          notificationManager.notify(1, builder.build());
      } catch (Exception ex)
      {
          TSLog.logger.warn(TSLog.error(ex.getMessage()));
      }

    }
}

Expected Behavior

Should capture Extras provided consistently

Actual Behavior

For some events, it's capturing and for some, it's not

Steps to Reproduce

Context

We are trying to capture location in both the foreground and background from an ionic android app. We have ou own db structure and we are capturing the locations from the location change event and writing to our db. For the locations captured in the background, we are trying to read then on app resume and write to our db. Is there any better way?

Foreground handling: we have enrolled for a location change event. Please confirm are we doing it right. In the foreground also extras are not coming in event argument consistently only a few of them have it. as we are in application context we are able to handle this issue externally ourselves.

Background handling: We have implemented headless mode and have a java file that has enrolled in events to show geofence alert which is working fine. But the extras added while inserting locations are not getting added consistently. only a few records have the extras added.

Find the attached DB , logs and code

Please check the rows 500 onwards. BG logs.txt db.zip

Debug logs

Logs ``` PASTE_YOUR_LOGS_HERE ```
christocracy commented 4 years ago

Plugin version: 3.1.9

No such version. You need to show the cordova plugin version, not the version of the Android library found in the logs.

djoyrocks commented 4 years ago

Hi Chris,

"cordova-background-geolocation-lt": "^3.8.2",

On Tue, Aug 25, 2020 at 7:54 PM Chris Scott notifications@github.com wrote:

Plugin version: 3.1.9

No such version. You need to show the cordova plugin version, not the version of the Android library found in the logs.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/transistorsoft/cordova-background-geolocation-lt/issues/1172#issuecomment-680057289, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACG737XLENLZY7MWXU4RVBLSCPCRTANCNFSM4QJ4IYBQ .

-- Regards Dhananjaya C K

christocracy commented 4 years ago

But the extras added while inserting locations are not getting added consistently. only a few records have the extras added.

The only way extras can be missing is if these values you're applying are actually null:

extras: {
        'membershipId': this.membershipId,
        'membershipUserId': this.membershipUserId,
        'employeeId': this.employeeId,
        'employeeDisplayName': this.employeeDisplayName,
        'employeeCode': this.employeeCode,
        'tripId': this.tripId,
        'tripUid': this.tripUid,
        'mobileId': this.mobileId
      },
christocracy commented 4 years ago

Also, modifying extras has no effect on records already recorded and inserted into the plugin's database.

extras are applied at the time the location is recorded. Records already inserted into the database are read-only. They can never be modified once inserted.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You may also mark this issue as a "discussion" and I will leave this open.

stale[bot] commented 3 years ago

Closing this issue after a prolonged period of inactivity. Fell free to reopen this issue, if this still affecting you.