KlarnaHybridSDK - 被消息淹没 "Notifying Native Hook" [ANDROID]
KlarnaHybridSDK - Getting swamped with the message "Notifying Native Hook" [ANDROID]
问题描述
在 chrome://inspect/#devices 中或直接在 android studio 中调试我的应用程序的 Web 部分时,我可以看到我被消息淹没了 "Notifying Native Hook"。
当使用 chrome://inspect/#devices 检查应用程序中使用的网页时,我看到该消息具有来源 VM298:5(很确定我也看到了另一个数字虚拟机之后)。单击源时,我发现以下函数:
(function () {
if (document.readyState === "complete") {
var foundMessageBridge = false
function notifyMessageBridge() {
console.log("Notifying Native Hook")
if (window.__KlarnaNativeHook != null) {
console.log("Klarna Native Hook was notified")
window.__KlarnaNativeHook.setNativeReady()
foundMessageBridge = true
} else {
window.setTimeout(notifyMessageBridge, 500)
}
}
notifyMessageBridge()
}
window.addEventListener('load', () => {
let interval = null
let notifyMessageBridge = () => {
if (window.__KlarnaNativeHook != null) {
window.__KlarnaNativeHook.setNativeReady()
clearInterval(interval)
}
}
interval = setInterval(notifyMessageBridge, 500)
})
}())
这最终让我意识到它与 Klarna 有关。因为我被这个淹没了,所以我想我的应用程序没有进入 if (window.__KlarnaNativeHook != null)。
经过一些调试后,我注意到以下内容:
- 当我开始与 Klarna 交互时,我的应用程序输入 if (window.__KlarnaNativeHook != null)。但只有一次,之后垃圾邮件会恢复。
- 如果我删除行
(activity as MainActivity).klarnaHybridSDK.addWebView(myWebView)
,消息淹没就会停止。我想这并不奇怪。
- 除了我的应用程序的网页之外,还生成了空的无标题网页,这些网页仅在控制台中打印出 "Notifying Native Hook",没有其他任何内容,没有代码,什么也没有。
- 除了卡顿之外,该应用程序与 Klarna 配合使用时运行良好。
我不确定这是SDK中的错误还是我的错误。
关于如何停止垃圾邮件的任何建议 "Notifying Native Hook"?
(我知道如何过滤它,但是 120 messages/min *(我的应用程序的网页 + 生成的无标题)并且每次都这样做很烦人)
代码
MainActivity
class MainActivity : AppCompatActivity(), KlarnaHybridSDKCallback {
...
lateinit var klarnaHybridSDK : KlarnaHybridSDK
override fun onCreate(savedInstanceState: Bundle?) {
klarnaHybridSDK = KlarnaHybridSDK("myapp://", this)
...
}
override fun didHideFullscreenContent(webView: WebView, completion: OnCompletion) {
Timber.i("Klarna didHideFullscreenContent called")
completion.run()
}
override fun didShowFullscreenContent(webView: WebView, completion: OnCompletion) {
Timber.i("Klarna didShowFullscreenContent called")
completion.run()
}
override fun onErrorOccurred(webView: WebView, error: KlarnaMobileSDKError) {
Timber.i("Klarna onErrorOccurred called")
Timber.i("Klarna Error: $error")
}
override fun willHideFullscreenContent(webView: WebView, completion: OnCompletion) {
Timber.i("Klarna willHideFullscreenContent called")
completion.run()
}
override fun willShowFullscreenContent(webView: WebView, completion: OnCompletion) {
Timber.i("Klarna willShowFullscreenContent called")
completion.run()
}
}
片段Class
class myFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
...
/*WEBVIEW*/
myWebView.settings.javaScriptEnabled = true
WebView.setWebContentsDebuggingEnabled(true)
val context = this.activity?.applicationContext
myWebView.addJavascriptInterface(
WebAppInterface(
myWebView, context
), "Android")
myWebView.settings.loadWithOverviewMode = true
myWebView.settings.useWideViewPort = true
myWebView.settings.domStorageEnabled = true
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
myWebView.settings.mixedContentMode = WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE
}
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT){
myWebView.settings.cacheMode = WebSettings.LOAD_NO_CACHE
if(this.activity?.applicationInfo?.flags != 0 && ApplicationInfo.FLAG_DEBUGGABLE != 0) {
WebView.setWebContentsDebuggingEnabled(true)
Timber.i("setWebContentsDebuggingEnabled")
}
}
ticketWebView.webViewClient = object: WebViewClient(){
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
...
}
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
...
if (view != null) {
(activity as MainActivity).klarnaHybridSDK.newPageLoad(view)
}
...
}
override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) {
...
}
override fun onReceivedHttpError(view: WebView?, request: WebResourceRequest?, errorResponse: WebResourceResponse?) {
super.onReceivedHttpError(view, request, errorResponse)
...
//Added to prevent Klarna's address search failing to cause the app to show the error screen.
if (request?.url.toString().contains("klarna.com/eu/address")) {
Timber.i("${receivedError}")
Timber.i("Ignoring error since it's simply Klarna's address search failing")
}
...
}
@TargetApi(21)
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
val url = request?.url
...
return (activity as MainActivity).klarnaHybridSDK.shouldFollowNavigation(url.toString())
}
/*To handle cases where the target API < 21*/
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
return (activity as MainActivity).klarnaHybridSDK.shouldFollowNavigation(url.toString())
}
}
val url = "myAppUrl.php"
val postData = "data=" + URLEncoder.encode(data, "UTF-8").toString()
myWebView.postUrl(url, postData.toByteArray())
return root
}
...
}
重现步骤
启动应用程序
在 Chrome.
中打开了 chrome://inspect/#devices
找到相关网页
按下 "Inspect"
预期行为
不会被控制台中的消息淹没。
设备和版本:
设备:三星A10
OS版本:Android9
Klarna In-App SDK版本:com.klarna.mobile:sdk:2.0.16
首先,感谢您报告此问题。幸运的是,这个问题将在未来的版本中得到解决。
为了让您更深入地了解,当 SDK 尝试在没有内容的 WebView 中通知 Klarna 组件时,这是当前的一个问题;从而导致名为“无标题”的页面包含过多的日志。
暂时可以通过在加载页面时将 WebView 添加到 Hybrid SDK 来避免这种情况。
编辑:@Myalakin 我们想通知您,此问题已由我们最新版本的 SDK 修复。
问题描述
在 chrome://inspect/#devices 中或直接在 android studio 中调试我的应用程序的 Web 部分时,我可以看到我被消息淹没了 "Notifying Native Hook"。 当使用 chrome://inspect/#devices 检查应用程序中使用的网页时,我看到该消息具有来源 VM298:5(很确定我也看到了另一个数字虚拟机之后)。单击源时,我发现以下函数:
(function () {
if (document.readyState === "complete") {
var foundMessageBridge = false
function notifyMessageBridge() {
console.log("Notifying Native Hook")
if (window.__KlarnaNativeHook != null) {
console.log("Klarna Native Hook was notified")
window.__KlarnaNativeHook.setNativeReady()
foundMessageBridge = true
} else {
window.setTimeout(notifyMessageBridge, 500)
}
}
notifyMessageBridge()
}
window.addEventListener('load', () => {
let interval = null
let notifyMessageBridge = () => {
if (window.__KlarnaNativeHook != null) {
window.__KlarnaNativeHook.setNativeReady()
clearInterval(interval)
}
}
interval = setInterval(notifyMessageBridge, 500)
})
}())
这最终让我意识到它与 Klarna 有关。因为我被这个淹没了,所以我想我的应用程序没有进入 if (window.__KlarnaNativeHook != null)。
经过一些调试后,我注意到以下内容:
- 当我开始与 Klarna 交互时,我的应用程序输入 if (window.__KlarnaNativeHook != null)。但只有一次,之后垃圾邮件会恢复。
- 如果我删除行
(activity as MainActivity).klarnaHybridSDK.addWebView(myWebView)
,消息淹没就会停止。我想这并不奇怪。 - 除了我的应用程序的网页之外,还生成了空的无标题网页,这些网页仅在控制台中打印出 "Notifying Native Hook",没有其他任何内容,没有代码,什么也没有。
- 除了卡顿之外,该应用程序与 Klarna 配合使用时运行良好。
我不确定这是SDK中的错误还是我的错误。
关于如何停止垃圾邮件的任何建议 "Notifying Native Hook"? (我知道如何过滤它,但是 120 messages/min *(我的应用程序的网页 + 生成的无标题)并且每次都这样做很烦人)
代码
MainActivity
class MainActivity : AppCompatActivity(), KlarnaHybridSDKCallback {
...
lateinit var klarnaHybridSDK : KlarnaHybridSDK
override fun onCreate(savedInstanceState: Bundle?) {
klarnaHybridSDK = KlarnaHybridSDK("myapp://", this)
...
}
override fun didHideFullscreenContent(webView: WebView, completion: OnCompletion) {
Timber.i("Klarna didHideFullscreenContent called")
completion.run()
}
override fun didShowFullscreenContent(webView: WebView, completion: OnCompletion) {
Timber.i("Klarna didShowFullscreenContent called")
completion.run()
}
override fun onErrorOccurred(webView: WebView, error: KlarnaMobileSDKError) {
Timber.i("Klarna onErrorOccurred called")
Timber.i("Klarna Error: $error")
}
override fun willHideFullscreenContent(webView: WebView, completion: OnCompletion) {
Timber.i("Klarna willHideFullscreenContent called")
completion.run()
}
override fun willShowFullscreenContent(webView: WebView, completion: OnCompletion) {
Timber.i("Klarna willShowFullscreenContent called")
completion.run()
}
}
片段Class
class myFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
...
/*WEBVIEW*/
myWebView.settings.javaScriptEnabled = true
WebView.setWebContentsDebuggingEnabled(true)
val context = this.activity?.applicationContext
myWebView.addJavascriptInterface(
WebAppInterface(
myWebView, context
), "Android")
myWebView.settings.loadWithOverviewMode = true
myWebView.settings.useWideViewPort = true
myWebView.settings.domStorageEnabled = true
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
myWebView.settings.mixedContentMode = WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE
}
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT){
myWebView.settings.cacheMode = WebSettings.LOAD_NO_CACHE
if(this.activity?.applicationInfo?.flags != 0 && ApplicationInfo.FLAG_DEBUGGABLE != 0) {
WebView.setWebContentsDebuggingEnabled(true)
Timber.i("setWebContentsDebuggingEnabled")
}
}
ticketWebView.webViewClient = object: WebViewClient(){
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
...
}
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
...
if (view != null) {
(activity as MainActivity).klarnaHybridSDK.newPageLoad(view)
}
...
}
override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) {
...
}
override fun onReceivedHttpError(view: WebView?, request: WebResourceRequest?, errorResponse: WebResourceResponse?) {
super.onReceivedHttpError(view, request, errorResponse)
...
//Added to prevent Klarna's address search failing to cause the app to show the error screen.
if (request?.url.toString().contains("klarna.com/eu/address")) {
Timber.i("${receivedError}")
Timber.i("Ignoring error since it's simply Klarna's address search failing")
}
...
}
@TargetApi(21)
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
val url = request?.url
...
return (activity as MainActivity).klarnaHybridSDK.shouldFollowNavigation(url.toString())
}
/*To handle cases where the target API < 21*/
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
return (activity as MainActivity).klarnaHybridSDK.shouldFollowNavigation(url.toString())
}
}
val url = "myAppUrl.php"
val postData = "data=" + URLEncoder.encode(data, "UTF-8").toString()
myWebView.postUrl(url, postData.toByteArray())
return root
}
...
}
重现步骤
启动应用程序
在 Chrome.
中打开了 chrome://inspect/#devices
找到相关网页
按下 "Inspect"
预期行为
不会被控制台中的消息淹没。
设备和版本:
设备:三星A10
OS版本:Android9
Klarna In-App SDK版本:com.klarna.mobile:sdk:2.0.16
首先,感谢您报告此问题。幸运的是,这个问题将在未来的版本中得到解决。
为了让您更深入地了解,当 SDK 尝试在没有内容的 WebView 中通知 Klarna 组件时,这是当前的一个问题;从而导致名为“无标题”的页面包含过多的日志。
暂时可以通过在加载页面时将 WebView 添加到 Hybrid SDK 来避免这种情况。
编辑:@Myalakin 我们想通知您,此问题已由我们最新版本的 SDK 修复。