> ## Documentation Index
> Fetch the complete documentation index at: https://docs.castled.io/llms.txt
> Use this file to discover all available pages before exploring further.

# In-App Messages

> [!NOTE]
>
> **Prerequisites**
>
> * [SDK integration completed](https://docs.castled.io/developer-resources/sdk-integration/android/getting-started.md)

Castled SDK allows you to show in-app messages to users who are currently engaging with your app. These messages are typically used to communicate important information,
deliver targeted promotions, provide user guidance, or engage with users within the app.

## Enabling In-App messages in SDK

Set the flag `enableInApp` to `true` when initializing the SDK

```kotlin
// SDK initialization
CastledNotifications.initialize(
    this, CastledConfigs.Builder()
        .apiKey("<api-key>")
        .location(<location>)
        .enableInApp(true)
        .build()
)
```

## Triggering In-App SDK events

In-App messages are typically shown in response to events that happen within your app. You can decide the trigger
event for in-app display when creating the campaign. By default SDK tracks `app_opened` and `page_viewed` events. Note that these events
are not reported to our sever and is used only to determine if an in-app needs to be shown or not.

If you want to use a custom event as the trigger event, SDK provides a method to raise such an event based on a user action within your app.

```kotlin
CastledNotification.logCustomAppEvent(context, "<event-name>", <event-params>)
```

`<event-name>` is the name of the custom event.
`<event-params>` is a flat map of key value pairs. Keys are always of type string, but values can be string, number, date or boolean.

For e.g. If your shopping app wants to display an in-app message when value of items in cart greater than a certain number, app would log
a custom event as follows whenever user adds an item to cart.

```kotlin
// Name of event: added_to_cart
// Event params:
//     value: total value of items in cart
//     items: total number of items in cart
CastledNotification.logCustomAppEvent(
            context,
            "added_to_cart",
            mapOf("value" to 120, "items" to 3)
        )
```

SDK will then make sure of displaying the eligible in-app to user if there is one.

## Skipping InApps in Activity

This is useful when you want to skip showing InApps when user is on specific screens within your app. For e.g. Splash screen, Checkout screen, etc.

Add the list of Activities that you want to skip to `castled.xml` (`castled.xml` is added to the `res/values` folder of your app)

```xml castled.xml
<resources>
    ...
    <string name="io_castled_inapp_excluded_activities">SplashScreenActivity,CheckoutActivity</string>
    ...
</resources>
```

## Managing InApp Rendering

You can use the following methods to override the default InApp behaviour.

### Pausing InApp

Any new InApps which are ready for display are not shown to users and added to a waiting queue. This can be used to not interrupt users when they are
performing any critical activity within your app.

```java
    CastledNotifications.pauseInApp()
```

### Resuming InApp

InApp display behaviour goes back to normal. If there are any InApps which are previously paused, they get displayed.

```java
    CastledNotifications.resumeInApp()
```

### Stopping InApps

This method stops all InApp messages from displaying until a `CastledNotifications.resumeInApp()` is invoked. Unlike `pauseInApp`,
`stopInApp` completely stops evaluating the trigger condition for displaying InApp and all InApp that would have been displayed otherwise would be ignored.

```java
    CastledNotifications.stopInApp()
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.