Utility Methods (Android)

Explore the Android utility methods related to location permissions.

Set Device Token

The Roam SDK is capable of sending push notifications to your users. Add Firebase to your Android project to get the device token.

Roam.setDeviceToken("FCM DeviceToken")

Get Device Token

This method gets the FCM DeviceToken from the SDK.

Roam.getDeviceToken()

Check location permission

Check whether your app has location permission. Returns a boolean, which is true if location permission has been granted or false otherwise.

Roam.checkLocationPermission()

Check Location Services

Check if the device has location services enabled. This method returns a boolean, which is true if the location services are on and false otherwise.

Roam.checkLocationServices()

Check Background Location Permission

This utility method checks whether your app has background location permissions. It returns a boolean as true if background location permission has been granted, or false otherwise.

Roam.checkBackgroundLocationPermission()

Request Location Permissions

Call this method to request the user to enable location permissions.

Roam.requestLocationPermission(this)
override fun onRequestPermissionsResult(requestCode: Int, permissions:
    Array<String>, grantResults:IntArray) {
        when (requestCode) {
            Roam.REQUEST_CODE_LOCATION_PERMISSION->
            if (grantResults[0] ==PackageManager.PERMISSION_GRANTED) {
            ....
            } else if (grantResults[0] ==PackageManager.PERMISSION_DENIED) {
            ....
            }
        }
}

Request Location Services

Call this method to enable location services.

Roam.requestLocationServices(this)
 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
      super.onActivityResult(requestCode, resultCode, data)
      if (requestCode == Roam.REQUEST_CODE_LOCATION_ENABLED) {
            ....
      }
}

Request Background Location Permission

Call this method to request the user to enable background location permissions.

Roam.requestBackgroundLocationPermission(this)
override fun onRequestPermissionsResult(requestCode: Int, permissions:
Array<String>, grantResults: IntArray) {
    when (requestCode) {
    Roam.REQUEST_CODE_BACKGROUND_LOCATION_PERMISSION->
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            ...
        } 
        else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
            ...
        }
    }
}

Check Location Tracking

Check whether location tracking has been started or not. This method returns a boolean value.

Roam.isLocationTracking()

Update Current Location

Using the updateCurrentLocation method, you can update the user's current location and set the accuracy ranging from 10 to 100 meters (default is 10).

Roam.updateCurrentLocation(DesiredAccuracy, accuracy)

This method should be used only if you need to update the current location of the device with better accuracy. Using this method consistently will consume more battery.

The higher the accuracy, the longer the response time. In some cases, it can take up to 30 seconds depending on the GPS signal strength.

Logout

Logout from Roam SDK using the logout() method.

NOTE - You need to reinitialize the SDK to login again.

Roam.logout(object : RoamLogoutCallback {
        override fun onSuccess(message: String) {
                
        }
        override fun onFailure(roamError: RoamError) {
                // Access Error code and message here
                // roamError.code
                // roamError.message
        }
})

Notification Opened Handler

By using this method inside FirebaseMessagingService class, track the campaign impressions and counts.

override fun onMessageReceived(RemoteMessage remoteMessage) {
  super.onMessageReceived(remoteMessage)
  val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as
        NotificationManager
  ....
  intent.putExtra(Roam.EXTRA, Roam.notificationReceiveHandler(remoteMessage.getData()))
  ....
}
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
      ....
    Roam.notificationOpenedHandler(intent)
)

Disable Battery Optimization (android 6+)

When running the SDK on Android 6 (and higher), it is recommended to ask the user to disable battery optimization for your application. This ensures that location tracking continues to work when the device is in Doze mode.

Moreover, on Android Pie, disabling battery optimization prevents Adaptive Battery from bucketing your app based on usage and restricting background processing, which can impact the detection quality of the SDK.

After explaining the benefits of disabling battery optimization, call the disableBatteryOptimization() method of the Roam class.

Roam.disableBatteryOptimization()

This will trigger a system dialog asking the user to allow disabling battery optimization for your app.

Check Battery Optimization

Roam.isBatteryOptimizationEnabled()

Last updated