# PostMessageServiceConnection

Artifact: [androidx.browser:browser](https://developer.android.com/jetpack/androidx/releases/browser) [View Source](https://cs.android.com/search?q=file:androidx/browser/customtabs/PostMessageServiceConnection.java+class:androidx.browser.customtabs.PostMessageServiceConnection) Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/browser#1.2.0)

*** ** * ** ***


```
abstract class PostMessageServiceConnection : ServiceConnection
```

<br />

*** ** * ** ***

A `https://developer.android.com/reference/android/content/ServiceConnection.html` for Custom Tabs providers to use while connecting to a `https://developer.android.com/reference/kotlin/androidx/browser/customtabs/PostMessageService` on the client side. TODO(peconn): Make this not abstract with API change.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/browser/customtabs/PostMessageServiceConnection#PostMessageServiceConnection(androidx.browser.customtabs.CustomTabsSessionToken)(session: https://developer.android.com/reference/kotlin/androidx/browser/customtabs/CustomTabsSessionToken)` |

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/browser/customtabs/PostMessageServiceConnection#bindSessionToPostMessageService(android.content.Context,java.lang.String)(context: https://developer.android.com/reference/android/content/Context.html, packageName: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)` Binds the browser side to the client app through the given `https://developer.android.com/reference/kotlin/androidx/browser/customtabs/PostMessageService` name. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/browser/customtabs/PostMessageServiceConnection#notifyMessageChannelReady(android.os.Bundle)(extras: https://developer.android.com/reference/android/os/Bundle.html?)` Records that the message channel has been created and notifies the client. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/browser/customtabs/PostMessageServiceConnection#onPostMessageServiceConnected()()` Called when the `https://developer.android.com/reference/kotlin/androidx/browser/customtabs/PostMessageService` connection is established. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/browser/customtabs/PostMessageServiceConnection#onPostMessageServiceDisconnected()()` Called when the connection is lost with the `https://developer.android.com/reference/kotlin/androidx/browser/customtabs/PostMessageService`. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/browser/customtabs/PostMessageServiceConnection#onServiceConnected(android.content.ComponentName,android.os.IBinder)(name: https://developer.android.com/reference/android/content/ComponentName.html, service: https://developer.android.com/reference/android/os/IBinder.html)` |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/browser/customtabs/PostMessageServiceConnection#onServiceDisconnected(android.content.ComponentName)(name: https://developer.android.com/reference/android/content/ComponentName.html)` |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/browser/customtabs/PostMessageServiceConnection#postMessage(java.lang.String,android.os.Bundle)(message: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html, extras: https://developer.android.com/reference/android/os/Bundle.html?)` Posts a message to the client. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/browser/customtabs/PostMessageServiceConnection#unbindFromContext(android.content.Context)(context: https://developer.android.com/reference/android/content/Context.html)` Unbinds this service connection from the given context. |

| ### Inherited functions |
|---|
| From [android.content.ServiceConnection](https://developer.android.com/reference/android/content/ServiceConnection.html) |---|---| | `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/android/content/ServiceConnection.html#onBindingDied(android.content.ComponentName)(name: https://developer.android.com/reference/android/content/ComponentName.html!)` | | `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/android/content/ServiceConnection.html#onNullBinding(android.content.ComponentName)(name: https://developer.android.com/reference/android/content/ComponentName.html!)` | |

## Public constructors

### PostMessageServiceConnection

Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/browser#1.2.0)

```
PostMessageServiceConnection(session: CustomTabsSessionToken)
```

## Public functions

### bindSessionToPostMessageService

Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/browser#1.2.0)

```
fun bindSessionToPostMessageService(context: Context, packageName: String): Boolean
```

Binds the browser side to the client app through the given `https://developer.android.com/reference/kotlin/androidx/browser/customtabs/PostMessageService` name. After this, this `https://developer.android.com/reference/kotlin/androidx/browser/customtabs/PostMessageServiceConnection` can be used for sending postMessage related communication back to the client.

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html` | A context to bind to the service. |
| `packageName: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | The name of the package to be bound to. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | Whether the binding was successful. |

### notifyMessageChannelReady

Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/browser#1.2.0)

```
fun notifyMessageChannelReady(extras: Bundle?): Boolean
```

Records that the message channel has been created and notifies the client. This method should be called when the browser binds to the client side `https://developer.android.com/reference/kotlin/androidx/browser/customtabs/PostMessageService` and also readies a connection to the web frame.

| Parameters |
|---|---|
| `extras: https://developer.android.com/reference/android/os/Bundle.html?` | Unused. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | Whether the notification was sent successfully. |

### onPostMessageServiceConnected

Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/browser#1.2.0)

```
fun onPostMessageServiceConnected(): Unit
```

Called when the `https://developer.android.com/reference/kotlin/androidx/browser/customtabs/PostMessageService` connection is established.

### onPostMessageServiceDisconnected

Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/browser#1.2.0)

```
fun onPostMessageServiceDisconnected(): Unit
```

Called when the connection is lost with the `https://developer.android.com/reference/kotlin/androidx/browser/customtabs/PostMessageService`.

### onServiceConnected

Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/browser#1.2.0)

```
fun onServiceConnected(name: ComponentName, service: IBinder): Unit
```

### onServiceDisconnected

Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/browser#1.2.0)

```
fun onServiceDisconnected(name: ComponentName): Unit
```

### postMessage

Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/browser#1.2.0)

```
fun postMessage(message: String, extras: Bundle?): Boolean
```

Posts a message to the client. This should be called when a tab controlled by related `https://developer.android.com/reference/kotlin/androidx/browser/customtabs/CustomTabsSession` has sent a postMessage. If postMessage() is called from a single thread, then the messages will be posted in the same order.

| Parameters |
|---|---|
| `message: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | The message sent. |
| `extras: https://developer.android.com/reference/android/os/Bundle.html?` | Reserved for future use. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | Whether the postMessage was sent to the remote successfully. |

### unbindFromContext

Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/browser#1.2.0)

```
fun unbindFromContext(context: Context): Unit
```

Unbinds this service connection from the given context.

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html` | The context to be unbound from. |