In my project, I use Chrome Custom Tabs and expect to establish communication through requestPostMessageChannel, but when executing this line of code, an error occurs: java.lang.IllegalStateException: setPackageName must be called before bindSessionToPostMessageService. After tracing the code, the problem seems to arise when executing this.mRemote.transact(11, _data, _reply, 0). This error only occurs in this project; other projects run smoothly. I have tried many methods but cannot solve the issue.
My gradle version is 7.5, agp is 7.4.2, and browser is 1.8.0.
var mmsession : CustomTabsSession? = null
val connection: CustomTabsServiceConnection = object : CustomTabsServiceConnection() {
override fun onCustomTabsServiceConnected(
componentName: ComponentName,
customTabsClient: CustomTabsClient
) {
// 创建CustomTabsSession
mmsession = customTabsClient.newSession(object : CustomTabsCallback() {
override fun onMessageChannelReady(extras: Bundle?) {
super.onMessageChannelReady(extras)
Log.d("MainActivity", "onMessageChannelReady")
val result : Int? = mmsession?.postMessage("First message sent", Bundle().also {
it.putString("navigateTo","https://sayedelabady.github.io/")
})
Log.d("MainActivity", "Result : $result")
}
override fun onPostMessage(message: String, extras: Bundle?) {
Log.d("MainActivity","message : $message")
if (message.contains("navigation command")) {
val msg = "{"navigateTo":"","time":"${System.currentTimeMillis()}"}"
val suc = mmsession?.postMessage(msg,null)
Log.d("MainActivity","postmessage : $suc")
}
}
override fun onRelationshipValidationResult(
relation: Int,
requestedOrigin: Uri,
result: Boolean,
extras: Bundle?
) {
super.onRelationshipValidationResult(relation, requestedOrigin, result, extras)
Log.d("MainActivity","Relationship result: $result requestedOrigin: $requestedOrigin")
super.onRelationshipValidationResult(
relation,
requestedOrigin,
result,
extras
)
}
override fun onNavigationEvent(navigationEvent: Int, extras: Bundle?) {
Log.d(TAG,"NavigationEvent : $navigationEvent")
}
})
// 请求post message channel
Log.d("MainActivity","current s L $mmsession")
if (mmsession != null) {
mmsession!!.requestPostMessageChannel(Uri.parse("https://bp-web-newbie.singapore.ai-transsion.com/chat"))
}
Has anyone encountered the same problem and solved it?