hypertrack / live-app-android

Build live location sharing in your Android app

Home Page:https://play.google.com/store/apps/details?id=com.hypertrack.live

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to prompt user to enable location when app is closed and hypertrack is tracking?

jaysson opened this issue · comments

Steps:

  1. I allow all permissions and start tracking
  2. Kill the app (hypertrack background service is running with foreground notification)
  3. Disable location in system settings

In this situation, I want to show a notification asking the user to enable location. How can we do it?

commented

We updated live app with new SDK interface. Now you can use after initialize HyperTrack.addTrackingStateListener(OnTrackingStateChangeListener)

HyperTrack.addTrackingStateListener(new TrackingStateObserver.OnTrackingStateChangeListener() {
            public void onError(TrackingError trackingError) {
                switch (trackingError.getCode()) {
                    case TrackingError.GPS_PROVIDER_DISABLED_ERROR:
                        // User disabled GPS in device settings.
                        break;
                }
            }

            public void onTrackingStart() {
            }

            public void onTrackingStop() {
            }
        });

Unfortunately, GPS_PROVIDER_DISABLED_ERROR does not work for api 26 and above. We will fix it as soon as possible.

Hey @jaysson!

Kill the app (hypertrack background service is running with foreground notification).

If your app process was killed, you cannot track location service availability preferences. There was possibility to subscribe to OS wide broadcasts in manifest, but it no longer works as of Android Oreo. So in your case you can only detect preferences change on your application launch buy invoking isLocationEnabled on LocatinManager instance. Another point is that if your app's process was intentionally stopped by user, performing any user-facing actions isn't a good UX, unless it was explicitly resumed/started.
HTH

commented

SDK version 3.4.5
Add this to your AndroidManifest.xml

        <receiver
            android:name=".TrackingErrorReceiver"
            android:exported="true">
            <intent-filter>
                <action android:name="com.hypertrack.sdk.TRACKING_ERROR" />
            </intent-filter>
        </receiver>

This is BroadcastReceiver example

public class TrackingErrorReceiver extends BroadcastReceiver {
    private static final String TAG = TrackingErrorReceiver.class.getSimpleName();

    @Override
    public void onReceive(final Context context, Intent intent) {
        if (!((App)context.getApplicationContext()).isForeground()) {
            int errorCode = intent.getIntExtra("code", 0);
            switch (errorCode) {
                case TrackingError.INVALID_PUBLISHABLE_KEY_ERROR:
                case TrackingError.AUTHORIZATION_ERROR:
                    Log.e(TAG, "Authorization failed");
                    break;
                case TrackingError.GPS_PROVIDER_DISABLED_ERROR:
                    Log.e(TAG, "Tracking failed");
                    // User disabled GPS in device settings.
                    NotificationUtils.sendGpsDisabledError(context);
                    HyperTrack.addTrackingStateListener(new MyTrackingStateListener() {
                        @Override
                        public void onTrackingStart() {
                            NotificationUtils.cancelGpsDisabledError(context);
                            HyperTrack.removeTrackingStateListener(this);
                        }
                    });
                    break;
                default:
                    // Some critical error in SDK.
                    break;
            }
        }
    }
}