Skip to content

HTMLアプリ内メッセージ

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

前提条件

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

HTMLメッセージについて

Braze JavaScriptインターフェイスを使用すると、アプリ内のカスタムWebViewでBrazeを活用できます。InAppMessageJavascriptInterfaceは以下の役割を担います。

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

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

アプリのWebViewからBraze機能を使用するには、WebViewにBraze JavaScriptインターフェイスを追加します。インターフェイスが追加されると、ユーザーガイド:HTMLアプリ内メッセージで利用可能な同じAPIがカスタム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");
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動画を埋め込んだ例です。

<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" title="YouTube video player">
        </iframe>
    </div>
</body>

AndroidのHTMLアプリ内メッセージでディープリンクや外部リンクを使用する場合、JavaScriptでbrazeBridge.closeMessage()を呼び出さないでください。SDKの内部ロジックは、リンクにリダイレクトする際にアプリ内メッセージを自動的に閉じます。brazeBridge.closeMessage()を呼び出すとこのプロセスが妨げられ、ユーザーがアプリに戻った際にメッセージが応答しなくなる可能性があります。

以下は、コードスニペットにおけるディープリンクの例です。

<script>
document.querySelectorAll('[data-button-id]').forEach(function (node)
Unknown macro: { node.addEventListener('click', function () { brazeBridge.logClick(node.dataset.buttonId); brazeBridge.closeMessage(); }); }
);
</script>

前提条件

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

HTMLメッセージについて

Braze JavaScriptインターフェイスを使用すると、アプリ内のカスタムWebViewでBrazeを活用できます。インターフェイスのScriptMessageHandlerは以下の役割を担います。

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

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

まず、WebViewBridgeScriptMessageHandlerをアプリに追加します。

1
2
3
4
let scriptMessageHandler = Braze.WebViewBridge.ScriptMessageHandler(
  channel: .inAppMessage,
  braze: braze
)

初期化したscriptMessageHandlerをWkWebViewのuserContentControllerに追加します。

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

次に、設定を使用してWebViewを作成します。

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

完了すると、コードは以下のようになります。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Create the script message handler using your initialized Braze instance.
let scriptMessageHandler = Braze.WebViewBridge.ScriptMessageHandler(
  channel: .inAppMessage,
  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)

例:カスタムイベントのログ記録

以下の例では、BrazeBridgeが既存のWebコンテンツから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!