HTML 인-앱 메시지
앱에 Braze 자바스크립트 인터페이스를 추가하는 방법을 알아보고, Braze API를 사용하여 사용자 지정 웹뷰에서 HTML 인앱 메시지를 생성할 수 있습니다.
Prerequisites
이 기능을 사용하려면 먼저 Android Braze SDK를 통합해야 합니다.
HTML 메시지 정보
Braze JavaScript 인터페이스를 사용하면 앱 내 커스텀 웹뷰 내에서 Braze를 활용할 수 있습니다. 는 InAppMessageJavascriptInterface 가 담당합니다:
- 사용자 가이드에 설명된 대로 WebView에 Braze JavaScript 브릿지를 삽입합니다: HTML 인앱 메시지.
- WebView에서 받은 브리지 메서드를 Braze 소프트웨어 개발 키트에 전달합니다.
웹뷰에 인터페이스 추가하기
앱의 WebView에서 Braze 기능을 사용하려면 WebView에 Braze JavaScript 인터페이스를 추가하면 됩니다. 인터페이스가 추가되면 사용자 가이드에 동일한 API를 사용할 수 있습니다: HTML 인앱 메시지 는 커스텀 웹뷰 내에서 사용할 수 있습니다.
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>
Prerequisites
이 기능을 사용하려면 먼저 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:
- Injecting the Braze JavaScript bridge into your WebView, as outlined in User Guide: HTML in-app messages.
- 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>
GitHub 에서 이 페이지를 편집합니다.