Problem description
When debugging the web portion of my app either in chrome://inspect/#devices or directly in android studio I can see that I'm getting swamped with the message "Notifying Native Hook". When using chrome://inspect/#devices to inspect the web page used in the app I see that the message has the source VM298:5 (quite sure that I've seen it with another number as well after VM). When clicking on the source I find the following function:
(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)
})
}())
Which finally led me to that it has something to do with Klarna. Since I'm getting swamped with this I suppose that my app is not getting into the if (window.__KlarnaNativeHook != null).
After some debugging I noticed the following:
(activity as MainActivity).klarnaHybridSDK.addWebView(myWebView)
the message swamping stops. Which I suppose is not that surprising. I'm not sure if this is a bug in the SDK or an error of mine.
Any suggestions on how to stop the spamming "Notifying Native Hook"? (I know how to filter it, but 120 messages/min * (my app's webpage + the untitleds generated) and doing it every time is getting tiresome)
Code
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()
}
}
Fragment 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
}
...
}
Steps to reproduce
Started the app
Opened chrome://inspect/#devices in Chrome.
Located the relevant web page
Pressed "Inspect"
Expected behavior
Not getting swamped with the message in my console.
Device and version:
Device: Samsung A10
OS version: Android 9
Klarna In-App SDK version: com.klarna.mobile:sdk:2.0.16
Firstly, thanks for reporting this issue. Fortunately, this problem will be fixed with a future release.
To give you more insight, this currently is an issue when the SDK tries to notify Klarna components in a WebView with no content; thus resulting in a page called ‘untitled’ with excessive number of logs.
Temporarily, this can be avoided by adding the WebView to the Hybrid SDK when a page is loaded.
Edit: @Myalakin We would like to inform you that this issue has been fixed by the latest releases of our SDK.