Skip to content

HTML アプリ内メッセージs

アプリにBraze JavaScriptインターフェイスを追加する方法を学習し、Braze APIを使用してカスタムWebViewでHTMLアプリ内メッセージを作成できるようにする。

前提条件

この機能を使用する前に、Android Braze SDKを統合する必要があります。

HTMLメッセージについて

BrazeのJavaScriptインターフェイスを使えば、アプリ内のカスタムWebView内でBrazeを活用することができる。以下はその責任である。 InAppMessageJavascriptInterfaceがレスポンシブである:

  1. ユーザーガイドに記載されているように、Braze JavaScriptブリッジをWebViewに注入する:HTMLアプリ内メッセージ.
  2. WebViewから受け取ったブリッジメソッドをBraze Android SDKに渡す。

WebView へのインターフェースの追加

アプリの WebView から Braze 機能を使用するには、WebView に Braze JavaScript インターフェイスを追加します。インターフェイスが追加された後、ユーザーガイドと同じAPIが利用できる:HTMLアプリ内メッセージ はカスタムWebView内で利用できるようになる。

1
2
3
4
5
String javascriptString = BrazeFileUtils.getAssetFileStringContents(context.getAssets(), "braze-html-bridge.js");
myWebView.loadUrl("javascript:" + javascriptString);

final InAppMessageJavascriptInterface javascriptInterface = new InAppMessageJavascriptInterface(context, inAppMessage);
myWebView.addJavascriptInterface(javascriptInterface, "brazeInternalBridge");
1
2
3
4
5
val javascriptString = context.assets.getAssetFileStringContents("braze-html-bridge.js")
myWebView.loadUrl("javascript:" + javascriptString!!)

val javascriptInterface = InAppMessageJavascriptInterface(context, inAppMessage)
myWebView.addJavascriptInterface(javascriptInterface, "brazeInternalBridge")

YouTubeのコンテンツを埋め込む

YouTube やその他の HTML5コンテンツは、HTML アプリ内メッセージで再生できます。これには、アプリ内メッセージが表示されるアクティビティでハードウェアアクセラレーションが有効になっている必要があります。詳細については、Android 開発者ガイドを参照してください。ハードウェアアクセラレーションは、Android API バージョン11以降でのみ利用できます。

以下は、HTML スニペットに YouTube 動画を埋め込んだ例です。

1
2
3
4
5
6
7
8
9
<body>
    <div class="box">
        <div class="relativeTopRight">
            <a href="appboy://close">X</a>
        </div>
        <iframe width="60%" height="50%" src="https://www.youtube.com/embed/_x45EB3BWqI">
        </iframe>
    </div>
</body>

前提条件

この機能を使用する前に、Swift Braze SDKを統合する必要があります。

About HTML messages

With the Braze JavaScript interface, you can leverage Braze inside the custom WebViews within your app. The interface’s ScriptMessageHandler is responsible for:

  1. Injecting the Braze JavaScript bridge into your WebView, as outlined in User Guide: HTML in-app messages.
  2. Passing the bridge methods received from your WebView to the Braze Swift SDK.

Adding the interface to a WebView

First, add the ScriptMessageHandler from WebViewBridge to your app.

1
let scriptMessageHandler = Braze.WebViewBridge.ScriptMessageHandler(braze: braze)

Add the initialized scriptMessageHandler to a WkWebView’s userContentController.

1
2
3
4
configuration.userContentController.add(
  scriptMessageHandler,
  name: Braze.WebViewBridge.ScriptMessageHandler.name
)

Then create the WebView using your configuration.

1
let webView = WKWebView(frame: .zero, configuration: configuration)

When you’re finished, your code should be similar to the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Create the script message handler using your initialized Braze instance.
let scriptMessageHandler = Braze.WebViewBridge.ScriptMessageHandler(braze: braze)

// Create a web view configuration and setup the script message handler.
let configuration = WKWebViewConfiguration()
configuration.userContentController.addUserScript(
  Braze.WebViewBridge.ScriptMessageHandler.script
)
configuration.userContentController.add(
  scriptMessageHandler,
  name: Braze.WebViewBridge.ScriptMessageHandler.name
)

// Create the webview using the configuration
let webView = WKWebView(frame: .zero, configuration: configuration)

Example: Logging a custom event

In the following example, BrazeBridge logs a custom event from existing web content to the Braze Swift SDK.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Logging data via BrazeBridge Example</title>
    <script>
      function logData(data) {
        window.brazeBridge.logCustomEvent(data);
      }
    </script>
  </head>

  <body>
    <input
      type="button"
      value="Click to log a custom Event 'completed_level'"
      onclick="logData('completed_level')"
    />
  </body>
</html>
New Stuff!