How to fecth location fcm service running time in android java

31 views Asked by At
    public class MyFirebaseMessagingService extends FirebaseMessagingService implements LocationListener{

    private static final String TAG = "MyFirebaseMsgService";
  
    private FusedLocationProviderClient fusedLocationClient;
    String lat = "", lon = "";
    @SuppressLint("WrongThread")
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e("remoteMessage","#"+remoteMessage.toString());

     
       // messageDao = chatDatabase.messageDao();
        try {
            fusedLocationClient = LocationServices.getFusedLocationProviderClient(getApplicationContext());
fetchlocation();
            // Check if message contains a data payload.
          
        } catch (Exception e) {

        }


        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
    }
 





     private void fetchlocation()
    {
Log.e("fetchlocation","fetchlocation");
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        Log.e("fetchlocation","fetchlocation1");
        fusedLocationClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
            @Override
            public void onComplete(@NonNull Task<Location> task) {
                Log.e("fetchlocation","fetchlocation2"+task.getResult());
                Location location = task.getResult();
                if (location != null) {
                    Log.e("clatzz", "@" + location.getLatitude());
                    lat = "" + location.getLatitude();
                    Log.e("clonzz", "@" + location.getLongitude());
                    lon = "" + location.getLongitude();
                    String val=lat+","+lon;
                   // savelocation(val);
                    Log.e("fetchlocation","fetchlocation3");
                }
            }

        });
        fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
        fusedLocationClient.getCurrentLocation(PRIORITY_HIGH_ACCURACY, new CancellationToken() {
            @Override
            public boolean isCancellationRequested() {
                return false;
            }

            @NonNull
            @Override
            public CancellationToken onCanceledRequested(@NonNull OnTokenCanceledListener onTokenCanceledListener) {
                return null;
            }
        }).addOnSuccessListener(location -> {
            Location  currentLocation = location;
            Log.e("currentLocation","#"+currentLocation.toString());
            // use this current location
        });
        Log.e("fetchlocation","fetchlocation4");
    }




    @Override
    public void onLocationChanged(Location location) {
        Log.e("location","#"+location.toString());
        try{
            lat= String.valueOf(location.getLatitude());
            lon= String.valueOf(location.getLongitude());

           // LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        }catch (Exception e)
        {

        }
    }






}


 <uses-permission android:name="com.google.android.gms.permission.AD_ID" tools:node="remove"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    
    compileSdk 33
    targetSdk 33
    
    
       implementation 'com.google.firebase:firebase-inappmessaging-display'
    implementation 'com.google.firebase:firebase-messaging:23.1.2'
    implementation 'com.google.android.libraries.places:places:3.0.0'
    
    implementation 'com.google.android.gms:play-services-location:21.0.1'
 

this code app open time working good but app closed or minimized scenario fcm message receive time location null. how to solve this problem anyone help me. app open time fcm message received time location fetching correctly. here FusedLocationProviderClient i used latest libray and also my code android sdk 33 support based code

0

There are 0 answers