# Send Auto Update Preference

To automatically update your Host App, the Host App must collect the user's Auto Update preference and send it to DT using the `setAutoUpdateEnabled` method.

{% code title="Kotlin" %}

```kt
fun setAutoUpdateEnabled(
   autoUpdateEnabled: Boolean,
   checkInstallSource: Boolean? = false,
   mmpUrl: String? = null,
   metadata: Bundle? = null
)
```

{% endcode %}

| PARAMETER            | TYPE    | STATUS   | DESCRIPTION                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| -------------------- | ------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `autoUpdateEnabled`  | Boolean | Required | <p>Boolean flag that represents the user's preference for automatic updates of the Host App. Use one of the following values, as there is no default value:</p><ul><li>If user agrees to automatic updates of the Host App, use <code>true</code>.</li><li>If user refuses automatic updates of the Host App, use <code>false</code>.</li><li>If you do not know the user's automatic update preference, use <code>false</code>.</li></ul>                                                                                                                                                                                                                                                                                                                                                                            |
| `CheckInstallSource` | Boolean | Optional | <p>Boolean flag that determines whether DT should check for the installation source of the app. If the installation source of the app is Google Play, automatic updates through DTIS SDK are not available.</p><p>Include one of the following values, as there is no default value.</p><ul><li>To check the installation source of the Host App, use <code>true</code>.</li><li>To proceed without checking the installation source of the Host App, use <code>false</code>.</li></ul>                                                                                                                                                                                                                                                                                                                               |
| `mmpUrl`             | String  | Optional | MMP tracking URL to be called when the Host App is updated.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| `metadata`           | Bundle  | Optional | <p>Bundled data for further processing presented as a key-value pair. Use this to specify a variant for a specific operator or for a payment partner app.</p><p>You can send any key-value pair in the metadata bundle. However, before sending a key-value pair in the metadata bundle, you must inform DT which APK version you want to associate with this key-value pair.</p><p>To install a specific APK version for <code>MyTelCo</code> customers, use the following string:</p><p><code>bundle.putString(SdkConstants.HOST\_APP\_OPERATOR\_TOKEN\_NAME, "MyTelCo")</code></p><p>To install a specific APK version that uses a specific payment partner (<code>GameCoin</code>), use the following string:</p><p><code>bundle.putString(SdkConstants.HOST\_APP\_PAYMENT\_PARTNER\_NAME, "GameCoin")</code></p> |

## Tracking Auto Updates <a href="#appupdatebroadcast" id="appupdatebroadcast"></a>

Ignite stores the user preferences for Auto Update and schedules update jobs for those who opted in.

### Broadcast for Auto Update Job Scheduling <a href="#h_01k4n26mhp7smzdrv8ecd3fx0d" id="h_01k4n26mhp7smzdrv8ecd3fx0d"></a>

To track scheduled jobs, listen to the `jobStatusBroadcastReceiver`.

{% code title="Kotlin" %}

```kotlin
override fun onStart() {
        super.onStart()

        val intentFilter = IntentFilter().apply {
            addAction(IgniteServiceSdk.ACTION_APP_UPDATE_JOB_SCHEDULED)
            addAction(IgniteServiceSdk.ACTION_APP_UPDATE_JOB_CANCELLED)
        }

        registerReceiver(jobStatusBroadcastReceiver, intentFilter)
    }
    
    override fun onStop() {
        super.onStop()

        unregisterReceiver(jobStatusBroadcastReceiver)
    }

    private val jobStatusBroadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent) {
            when (intent.action) {
                IgniteServiceSdk.ACTION_APP_UPDATE_JOB_SCHEDULED -> {
                    toast("App Update Job Scheduled.")
                }
                IgniteServiceSdk.ACTION_APP_UPDATE_JOB_CANCELLED -> {
                    toast("App Update Job Cancelled.")
                }
                else -> {
                    println("$TAG Unknown Broadcast Action")
                }
            }
        }
    }
```

{% endcode %}

This broadcast receiver may contain the following actions:

| ACTION                            | DESCRIPTION                                                                                                                                                                                                                             |
| --------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `ACTION_APP_UPDATE_JOB_SCHEDULED` | Update is available, and a job to install the update is scheduled. DT does not broadcast when the Auto Update job fails to schedule. Instead, the next time the Host App initializes the SDK, DT attempts to schedule the update again. |
| `ACTION_APP_UPDATE_JOB_CANCELLED` | Scheduled update job has been cancelled.                                                                                                                                                                                                |

### Broadcast for Auto Update Job Completion <a href="#h_01he8hz6j6xt7q8ar0t3fbdagc" id="h_01he8hz6j6xt7q8ar0t3fbdagc"></a>

To track the installation of scheduled auto updates, listen to the `BroadcastReceiver`.

{% hint style="info" %}
Because tasks may be cancelled before receiving a broadcast, do not define your receiver as an activity in your Host App. Instead, define your receiver as an application class to ensure receipt of Auto Update statuses.
{% endhint %}

{% code title="Kotlin" %}

```kotlin
registerReceiver(
            object : BroadcastReceiver() {
                override fun onReceive(context: Context?, intent: Intent?) {
                    intent?.let {
                        when (it.action) {
                            IgniteServiceSdk.ACTION_APP_UPDATE_SUCCESS -> Log.d(
                                TAG,
                                "App Updated Successful"
                            )
                            IgniteServiceSdk.ACTION_APP_UPDATE_FAILED -> Log.d(
                                TAG,
                                "App Updated Failed"
                            )
                            else -> Log.d(TAG, "Invalid Action")
                        }
                    }
                }
            },
            IntentFilter().apply {
                addAction(IgniteServiceSdk.ACTION_APP_UPDATE_SUCCESS)
                addAction(IgniteServiceSdk.ACTION_APP_UPDATE_FAILED)
            }
        )
```

{% endcode %}

This broadcast receiver may contain the following actions:

| ACTION                      | DESCRIPTION                                                  |
| --------------------------- | ------------------------------------------------------------ |
| `ACTION_APP_UPDATE_SUCCESS` | Installation of the updated Host App completed successfully. |
| `ACTION_APP_UPDATE_FAILED`  | Installation of the updated Host App failed.                 |

## Examples <a href="#h_01k4n26mhq7sqc0jcm36v3np2e" id="h_01k4n26mhq7sqc0jcm36v3np2e"></a>

{% code title="Kotlin" %}

```kotlin
binding.checkboxUserOptIn.setOnCheckedChangeListener { _, isAutoUpdateEnabled -
    val bundle = Bundle()
    if (!"null".equals(binding.hostAppPartnerName.text.toString(), true)) {
        bundle.putString(SdkConstants.HOST_APP_PAYMENT_PARTNER_NAME,

binding.hostAppPartnerName.text.toString())
    }
    if (!"null".equals(binding.hostAppOperatorToken.text.toString(), true)) {
        bundle.putString(SdkConstants.HOST_APP_OPERATOR_TOKEN_NAME,

binding.hostAppOperatorToken.text.toString())
    }

    IgniteServiceSdk.setAutoUpdateEnabled(
        isAutoUpdateEnabled,
        binding.checkInstallSource.isChecked,
        binding.clientMMPUrlEditText.text.toString(),
        bundle
    )
}
```

{% endcode %}

{% code title="Kotlin" %}

```kotlin
class MainActivity : AppCompatActivity(), IConnectionCallback {
    lateinit var igniteService: IIgniteService

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initializeSdk(clientSecret, clientMMPUrl) //clientSecret and MMPUrl are stored somewhere in your app.
        igniteService.connect(this)

        // Call remote function on button click
        findViewById(R.id.userOptInCheckBox).setOnCheckedChangeListener {
            IgniteServiceSdk.setAutoUpdateEnabled(isChecked)
        }

    }

    override fun onConnected() {
        Toast.makeText(applicationContext, "Connected and authenticated with Ignite Service", Toast.LENGTH_SHORT).show()
    }

    override fun onDisconnected(message: String?) {
        Toast.makeText(applicationContext, "Disconnected from Ignite Service: $message", Toast.LENGTH_SHORT).show()
    }

    override fun onDestroy() {
        super.onDestroy()
        igniteService.disconnect(this)
    }
    
    override fun onStart() {
        super.onStart()

        val intentFilter = IntentFilter().apply {
            addAction(IgniteServiceSdk.ACTION_APP_UPDATE_JOB_SCHEDULED)
            addAction(IgniteServiceSdk.ACTION_APP_UPDATE_JOB_CANCELLED)
        }

        registerReceiver(jobStatusBroadcastReceiver, intentFilter)
    }
    
    override fun onStop() {
        super.onStop()

        unregisterReceiver(jobStatusBroadcastReceiver)
    }

    private fun initializeSdk(clientSecret: String = CLIENT_SECRET, clientMMPUrl: String) {
        igniteService = IgniteServiceSdk.init(
            applicationContext,
            clientSecret,
            clientMMPUrl
        )

        if (IgniteServiceSdk.getIsAutoUpdateEnabled() == null) {
            Toast.makeText(this, "No user Opt in value for auto update", Toast.LENGTH_LONG).show()
        } else if (IgniteServiceSdk.getIsAutoUpdateEnabled() == true) {
            checkboxUserOptIn.isChecked = true
        }
    }
    
    private val jobStatusBroadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent) {
            when (intent.action) {
                IgniteServiceSdk.ACTION_APP_UPDATE_JOB_SCHEDULED -> {
                    toast("App Update Job Scheduled.")
                }
                IgniteServiceSdk.ACTION_APP_UPDATE_JOB_CANCELLED -> {
                    toast("App Update Job Cancelled.")
                }
                else -> {
                    println("$TAG Unknown Broadcast Action")
                }
            }
        }
    }

    companion object {
        private const val clientSecret = "my-client-secret-secretly-stored"
    }
}
```

{% endcode %}

{% code title="Kotlin" %}

```kotlin
class MyApplication: Application() {

    override fun onCreate() {
        super.onCreate()

        registerReceiver(
            object : BroadcastReceiver() {
                override fun onReceive(context: Context?, intent: Intent?) {
                    intent?.let {
                        when (it.action) {
                            IgniteServiceSdk.ACTION_APP_UPDATE_SUCCESS - Log.d(
                                TAG,
                                "App Updated Successful"
                            )
                            IgniteServiceSdk.ACTION_APP_UPDATE_FAILED - Log.d(
                                TAG,
                                "App Updated Failed"
                            )
                            else - Log.d(TAG, "Invalid Action")
                        }
                    }
                }
            },
            IntentFilter().apply {
                addAction(IgniteServiceSdk.ACTION_APP_UPDATE_SUCCESS)
                addAction(IgniteServiceSdk.ACTION_APP_UPDATE_FAILED)
            }
        )
    }

    companion object {
        const val TAG = "DEMOAPP"
    }

}
```

{% endcode %}
