# 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)

*** ** * ** ***


```
public abstract class PostMessageServiceConnection implements 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/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/androidx/browser/customtabs/PostMessageServiceConnection#PostMessageServiceConnection(androidx.browser.customtabs.CustomTabsSessionToken)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/browser/customtabs/CustomTabsSessionToken session)` |

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

| ### Inherited methods |
|---|
| From [android.content.ServiceConnection](https://developer.android.com/reference/android/content/ServiceConnection.html) |---|---| | `void` | `https://developer.android.com/reference/android/content/ServiceConnection.html#onBindingDied(android.content.ComponentName)(https://developer.android.com/reference/android/content/ComponentName.html name)` | | `void` | `https://developer.android.com/reference/android/content/ServiceConnection.html#onNullBinding(android.content.ComponentName)(https://developer.android.com/reference/android/content/ComponentName.html name)` | |

## Public constructors

### PostMessageServiceConnection

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

```
public PostMessageServiceConnection(@NonNull CustomTabsSessionToken session)
```

## Public methods

### bindSessionToPostMessageService

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

```
public boolean bindSessionToPostMessageService(
    @NonNull Context context,
    @NonNull String packageName
)
```

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

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | A context to bind to the service. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html packageName` | The name of the package to be bound to. |

| Returns |
|---|---|
| `boolean` | Whether the binding was successful. |

### notifyMessageChannelReady

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

```
public final boolean notifyMessageChannelReady(@Nullable Bundle extras)
```

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/androidx/browser/customtabs/PostMessageService` and also readies a connection to the web frame.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/os/Bundle.html extras` | Unused. |

| Returns |
|---|---|
| `boolean` | Whether the notification was sent successfully. |

### onPostMessageServiceConnected

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

```
public void onPostMessageServiceConnected()
```

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

### onPostMessageServiceDisconnected

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

```
public void onPostMessageServiceDisconnected()
```

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

### onServiceConnected

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

```
public final void onServiceConnected(@NonNull ComponentName name, @NonNull IBinder service)
```

### onServiceDisconnected

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

```
public final void onServiceDisconnected(@NonNull ComponentName name)
```

### postMessage

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

```
public final boolean postMessage(@NonNull String message, @Nullable Bundle extras)
```

Posts a message to the client. This should be called when a tab controlled by related `https://developer.android.com/reference/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 |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html message` | The message sent. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/os/Bundle.html extras` | Reserved for future use. |

| Returns |
|---|---|
| `boolean` | 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)

```
public void unbindFromContext(@NonNull Context context)
```

Unbinds this service connection from the given context.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | The context to be unbound from. |