Klarna Docs - On-site Messaging for Android

Adding On-Site Messaging is as easy as it gets. To achieve this tailor made experience for the user you just need to:

  • Request access to On-Site Messaging
  • Create the native view
  • Set the parameters
  • Render the view

If you are using a version of the SDK prior to 2.3.2, to be able to integrate On-site Messaging our team needs to enable the access for it. You can request access through your dedicated Delivery Manager. Please refer the Merchant ID (MID) and/or the name of your brand/company (merchant name), the countries you request access and the environment (playground or production).

The On-Site Messaging native view in Android is called KlarnaOSMView. You can create KlarnaOSMView programmatically or inflate it from an XML file. Additionally you can also set the parameters from the XML.

You can create the native view programmatically and add it to your layout with your desired layout options.

KOTLIN
// Create an instance of OSM view
val osmView = KlarnaOSMView(context)

// Add it to your layout with desired layout parameters
val osmLayoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
containerViewGroup.addView(osmView, osmLayoutParams)

You can also add the native view in your XML like below:

MARKUP
<com.klarna.mobile.sdk.api.osm.KlarnaOSMView
    android:id="@+id/klarnaOsmView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ... />

Now you can access the view like this:

JAVA
val osmView = findViewById<KlarnaOSMView>(R.id.klarnaOsmView)

Parameters used for KlarnaOSMView are set as variables of the View instance or as attributes in the XML declaration. These can be identifier string values, amount or enumeration values (parameters marked with * are mandatory).

ParamTypeDescription
clientId*StringOn-Site Messaging Client Identifier gathered from Klarna Merchant Portal.
placementKey*String ("credit-promotion-badge" or "top-strip-promotion-badge")Placement key identifier from Klarna Merchant Portal. At the moment the supported placement keys are "credit-promotion-badge" and "top-strip-promotion-badge"
locale*StringLocale string for the placement configuration, default value is en-US.
purchaseAmountLongAmount for the placement. Set in micro units ($120.00 = 12000), used for amount based credit promotions.
environment*KlarnaOSMEnvironmentEnumerated environment value to specify merchant’s running environment. Default value is DEMO.
region*KlarnaOSMRegionEnumerated region value to specify merchant’s running market region.
theme*KlarnaOSMThemeEnumerated theme value to specify how to stylize the view on light and dark configurations.
hostActivity*ActivityActivity instance to specify parent activity. It will be used for inner linking for informational pages. The reference will be kept as a weak reference.
styleConfigurationKlarnaOSMStyleConfigurationCan be used to customize the appearance of the OSM view. If not set, a default appearance will be used based on the specified theme. Note: this is available from version 2.6.17.

Environment enumeration will be used to define whether to use a demo placement or fetch placement configurations from either playground or production.

NameDescription
DEMOThis environment ignores all other parameters and shows a demo placement config containing all On-Site Messaging features.
PLAYGROUNDThis environment is for placement configurations from playground Merchant Portal.
PRODUCTIONThis environment is for production builds of the app with placement configurations for production merchants.

Region enumeration will be used to define which endpoint to connect as a source for the placement configurations.

NameDescription
EURegion value for Europe.
NARegion value for North America.
OCRegion value for Oceania.

Theme enumeration will be used to define which style of OSM to be used, depending on either the app’s configuration or the system setting. Each style has a predefined set of values for background color, text color, text size, ... that will be used to style the OSM view.

NameDescriptions
LIGHTLight style for placement view.
DARKDark style for placement view.
AUTOMATICAutomatic theme that will use the system’s user interface style.

This feature is available from version 2.6.17.

By using KlarnaOSMStyleConfiguration you can customize the appearance of the OSM view to some extend. As of now, you can customize the following:

  • Background color
  • Text color
  • Text size
  • Text size unit
  • Text font

Please note that styleConfiguration takes precedence over theme. This has the following implications:

  • If you specify a value thorough styleConfiguration (text color, for example) then this value will be used to style the OSM view regardless of the value that theme provides.
  • If your app supports light and dark mode, then it's your responsibility to update styleConfiguration and call the render method when the device's theme changes so that the appearance of the OSM view updates.

To set a custom style for the OSM view you can do like the following:

KOTLIN
val klarnaOSMStyleConfiguration = KlarnaOSMStyleConfiguration.Builder()
    .setBackgroundColor(0xFFFFFF)
    .setTextStyle(
        KlarnaTextStyleConfiguration.Builder()
            .setTextColor(0x000000)
            .setTextSize(unit = TypedValue.COMPLEX_UNIT_SP, size = 16f)
            .setTextFont(ResourcesCompat.getFont(this, R.font.merriweather_regular)) // assuming that there's a font file in the 'res/font' directory with the name 'merriweather_regular'
            .build()
    )
    .build()

All of the parameters for On-Site Messaging are stored as variables on the KlarnaOSMView instance. The required ones need to be set before calling the render method, otherwise the RenderResult will return a validation error for the missing parameter.

Most of the parameters can be set either programmatically or in the XML layout file. The only two exceptions are hostActivity and styleConfiguration that you can only set them programatically. In both cases, the parameters will be stored in the View instance.

KOTLIN
osmView.clientId = "<client-id>"
osmView.placementKey = "<placement-key>"
osmView.locale = "en-US"
osmView.purchaseAmount = 10000
osmView.environment = KlarnaOSMEnvironment.PRODUCTION
osmView.region = KlarnaOSMRegion.NA
osmView.theme = KlarnaOSMTheme.DARK
osmView.styleConfiguration = ...
osmView.hostActivity = this // assuming that 'this' refers to the current Activity
MARKUP
<com.klarna.mobile.sdk.api.osm.KlarnaOSMView
    ...
    app:klarnaOsmClientId="<client-id>"
    app:klarnaOsmPlacementKey="<placement-key>"
    app:klarnaOsmLocale="en-US"
    app:klarnaOsmPurchaseAmount="1000"
    app:klarnaOsmEnvironment="production"
    app:klarnaOsmRegion="na"
    app:klarnaOsmTheme="dark"/>

Once you have set all the parameters to the KlarnaOSMView, you are ready to render the actual placement view. To do that, you need to call the render method of the view with the RenderResult callback parameter.

Once the render method is called, first the view will validate all the parameters. If there is a missing parameter the RenderResult callback will be invoked with corresponding error values. If parameters are valid then the view will try to fetch placement configuration from On-Site Messaging API. Any errors or failures from this network request will also invoke RenderResult callback if necessary. If the placement configuration is fetched and valid then the KlarnaOSMView will render it and invoke the RenderResult callback with a null value.

The RenderResult callback can be used for logging purposes or to make the KlarnaOSMView visible or not, according to the error.

KOTLIN
osmView.render(RenderResult { error ->
    ...
})

Similar to other parts of the SDK, On-Site Messaging also makes use of the KlarnaMobileSDKError class. To read more about the error handling and error object properties refer to the On-Site Messaging Error Handling guide.

These are the predefined names for errors that can happen during the placement render flow. These names are available as static variables inside KlarnaOSMError class.

Error NameDescription
KlarnaOSMErrorMissingClientIdClient ID was not set. Set the clientId variable and invoke render again.
KlarnaOSMErrorMissingPlacementKeyPlacement key was not set. Set the placementKey variable and invoke render again.
KlarnaOSMErrorMissingRegionRegion was not set. Set the region variable and invoke render again.
KlarnaOSMErrorMissingHostHosting activity was not set. Set the hostActivity variable and invoke render again.
KlarnaOSMErrorInvalidPlacementConfigFetched configuration from the API can not be rendered by the native view.
KlarnaOSMErrorPlacementErrorOn-Site Messaging API has returned an error. Message of this error will contain information sent from the API.
KlarnaOSMErrorNetworkErrorA network error occurred and the placement will not be rendered.
KlarnaOSMErrorDisabledNative On-Site Messaging has been disabled by Klarna, the placement will not be rendered.