> For the complete documentation index, see [llms.txt](https://docs.platform.gamezop.com/publishers/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.platform.gamezop.com/publishers/gamezop/advanced/implement-game-events-listener.md).

# Implement game-events listener

You may want to listen to the events Gamezop games broadcast if you have custom UI overlays to implement at the occurrence of specific events, or if you want to take any action when a particular in-game event occurs. This document guides you on how you can do that.

Guides are available for:

* [Android apps](#android-apps)
* [Flutter apps](#flutter-apps)
* [iOS apps](#ios-apps)

### Android apps

If you have our game running within a WebView, you can listen to events with the following steps:

#### Step 1: Enable Javascript in WebView

Let's say your WebView is defined as `myWebView`. Here's a code-block for enabling Javascript:

```kotlin
val myWebView: WebView = findViewById(R.id.webview)
myWebView.settings.javaScriptEnabled = true
```

#### Step 2: Create a `class` to receive events

We will create a class called `gamezopWebAppInterface`. Within that, we will define a `getGameEvents` function. **Do not change the name of the function**, as this is the function our games will call to pass events to the app.

When our game calls the function, it will pass a stringified JSON object as an argument to the function which will have all the data on the event being sent.

Upon receiving the events, for simplicity sake, let's say we want to just print them in Toasts. You can, of course, replace this with your desired actions.

{% code overflow="wrap" %}

```kotlin
/** Instantiate the interface and set the context  */
class gamezopWebAppInterface(private val mContext: Context) {

    /** Get Game Events and Show a toast from the web page  */
    @JavascriptInterface
    fun getGameEvents(gameEvent: String) {
        Toast.makeText(mContext, gameEvent, Toast.LENGTH_SHORT).show()
    }
}
```

{% endcode %}

#### Step 3: Assign the class to your WebView

Where you initialise the Webview, you need to add a Javascript interface there. This is basically the class you created in Step 2.

{% code overflow="wrap" %}

```kotlin
val webView: WebView = findViewById(R.id.webview)
webView.addJavascriptInterface(gamezopWebAppInterface(this), "AndroidBridge")
```

{% endcode %}

That's all. You're good to go!

### Flutter apps

For Flutter apps, you just need to add a JavaScript channel with the name `AndroidBridge`.

{% code overflow="wrap" %}

```dart
WebView( 
 initialUrl: url, 
 javascriptMode: JavascriptMode.unrestricted, 
 javascriptChannels: Set.from([ 
  JavascriptChannel( 
   name: 'AndroidBridge', onMessageReceived: (JavascriptMessage message) {
     // This is where you can use your game events
     print(message.message);
   }),
  ]), 
 onWebViewCreated: (WebViewController w) { 
  webViewController = w; 
 },
)
```

{% endcode %}

### iOS apps

For iOS applications, you just need to add a JS Message Handler.

{% code overflow="wrap" %}

```swift
override func viewDidLoad() { 
  super.viewDidLoad() 
  let config = WKWebViewConfiguration()
  config.userContentController = WKUserContentController()
  config.userContentController.add(self, name: "gameState")
  let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: 200, height: 200),     
  configuration: config) 
  view.addSubview(webView)
  webView.loadHTMLString(content, baseURL: nil)
 }

 func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
 print(message.body) 
}
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.platform.gamezop.com/publishers/gamezop/advanced/implement-game-events-listener.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
