NowSDK - Android

  • Release version: Xanadu
  • Updated August 1, 2024
  • 1 minute to read
  • The NowSDK class is a singleton that provides the public API for the NowSDK. This class is the gateway to all Android SDK feature services.

    Before initializing a feature service, you must initialize the SDK itself by calling NowSDK.configure(). For additional information on getting started with the Android NowSDK, refer to the ServiceNow Mobile SDK Developers Guide - Android.

    NowSDK - configure(application: Application, configuration: NowSDKConfiguration)

    Configures the NowSDK for use.

    You must call this function before attempting to use any of the feature services provided by the SDK.

    Table 1. Parameters
    Name Type Description
    application Application Reference to the hosting application object.
    configuration NowServiceConfiguration Object containing the information necessary to initialize the NowSDK.
    Table 2. Returns
    Type Description
    None NowSDKError is thrown if the provided configuration is invalid.

    The following code example shows how to call this function.

    class SampleApplication : Application(), NowSDKAuthorizationProviding, DevicePermissionDelegate {
    
        private val nowSdkSettings = NowSDKSettings(
            instanceBaseURL = "https://instance-name.service-now.com",
            clientId = "client_id",
            user = "user"
        )
    
        private val coroutineScope = CoroutineScope(Dispatchers.IO)
    
        private val nowSDKConfiguration = NowSDKConfiguration(this, this, NowLogLevel.Debug)
        override fun onCreate() {
            super.onCreate()
    
            NowSDK.configure(this, nowSDKConfiguration)
        }
    
    
        override fun requestAuthorization(
            instanceURL: URL,
            callback: Consumer<List<AuthorizationToken>?>
        ) {
            coroutineScope.launch {
                when {
                    nowSdkSettings.user.isNullOrBlank().not() -> authorizeWithJWT(
                        callback = callback,
                        user = nowSdkSettings.user,
                        clientId = nowSdkSettings.clientId
                    )
    
                    else -> authorizeWithGuest(callback = callback)
                }
            }
        }
    
        override fun canRequestPermission(permission: DevicePermission): Boolean {
            return true
        }
    }

    NowSDK - logout()

    Clears all user sessions persisted in memory.

    Table 3. Parameters
    Name Type Description
    None
    Table 4. Returns
    Type Description
    None

    The following code example shows how to call this function.

    class SampleApplication : Application() {
    
        private val nowSDKConfiguration = NowSDKConfiguration(authorizationProvider, permissionDelegate, NowLogLevel.Debug)
    
        override fun onCreate() {
            super.onCreate()
            NowSDK.configure(this, nowSDKConfiguration)
        }
    
        fun logout() {
            // Clear all user session when an application resets the user session
            NowSDK.logout()
        }
    }