# Broadcast

{% hint style="info" %}
**Version compatibility**

This feature is only available on Android devices running version **4.29.0** or newer versions of the JourneyApps Container.
{% endhint %}

The `Broadcast` service is used to send data to and receive data from external Android apps using JavaScript.

The Broadcast service supports Android broadcast categories. Please see the [action test](https://developer.android.com/guide/components/intents-filters.html#ActionTest) and [category test](https://developer.android.com/guide/components/intents-filters.html#CategoryTest) sections in the Android documentation on [Intents and Intent Filters](https://developer.android.com/guide/components/intents-filters.html) for more information on intent resolution.

### Receiving Data

Use the `register` function to receive data:

```javascript
Broadcast.register(target, callback, options);
```

| Parameter            | Type             | Details                                                                                                                                      |
| -------------------- | ---------------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| `target`             | Broadcast.Target | Broadcast registration target.                                                                                                               |
| `target.action`      | string           | Unique name used to identify the broadcasting event. This should correspond to the event name in your external application.                  |
| `target.categories`  | string\[]        | Array of categories to register broadcast against.                                                                                           |
| `callback`           | function         | <p>Function to be called as soon as a broadcast event is received. The function takes a single parameter, <code>data</code> (object)<br></p> |
| `options`            | object           | Optional object to specify options. Added in v21.2.1 of the JourneyApps Container and v4.78.2 of the JourneyApps Runtime.                    |
| `options.background` | boolean          | Should Broadcasts trigger when the container is in the background. Defaults to `true`                                                        |

e.g.

```javascript
function callback(data) {
   console.log("Received: " + JSON.stringify(data));
}
```

### Sending Data

Use the `trigger` function to send data:

```javascript
Broadcast.trigger(target, eventData);
```

| Parameter           | Type             | Details                                                                                                                     |
| ------------------- | ---------------- | --------------------------------------------------------------------------------------------------------------------------- |
| `target`            | Broadcast.Target | Listener target to broadcast to.                                                                                            |
| `target.action`     | string           | Unique name used to identify the broadcasting event. This should correspond to the event name in your external application. |
| `target.categories` | string\[]        | Array of categories for broadcast event.                                                                                    |
| `eventData`         | object           | <p>Data sent to the external application. Note that <code>eventData</code> should be an object.<br></p>                     |
| e.g.                |                  |                                                                                                                             |

```javascript
{
   "action": "START"
}
```

### Finding out if the Broadcast API is supported

Use the `getCapabilities` function to see whether the active device and JourneyApps Container supports the Broadcast API:

```javascript
Broadcast.getCapabilities();
```

\*\* Returns \*\*: `object` with a `supported` field. This field will be `true` if the active device and JourneyApps Container supports the Broadcast API and `false` otherwise.

### Other Usage Notes

Here are some other code examples that show how the Broadcast API can be used.

#### Registering a broadcast receiver:

```javascript
var watchId;

function callback(data) {
console.log("Callback:", JSON.stringify(data));
}

var target = {
    action: "com.example.action",
    categories: [
        "com.example.category.a",
        "com.example.category.b"
    ]
};

watchId = Broadcast.register(target, callback);
```

#### Removing a single broadcast receiver registration:

```javascript
Broadcast.deregister(watchId);
```

#### Removing all broadcast receiver registrations:

```javascript
Broadcast.deregisterAll();
```

#### Triggering a broadcast (`eventData` must be an object):

```javascript
var target = {
action: "com.example.action",
    categories: [
        "com.example.category.a",
        "com.example.category.b"
    ]
};

Broadcast.trigger(target, {foo: "bar", "name": "trigger"});
```
