Skip to content

Commit

Permalink
message from wechat
Browse files Browse the repository at this point in the history
  • Loading branch information
JarvanMo committed May 15, 2023
1 parent 7034b2f commit ba1f5ab
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 88 deletions.
20 changes: 20 additions & 0 deletions android/src/main/kotlin/com/jarvan/fluwx/FluwxPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry
import java.util.concurrent.atomic.AtomicBoolean


/** FluwxPlugin */
class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
Expand All @@ -47,6 +49,9 @@ class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
private var fluwxChannel: MethodChannel? = null

private var context: Context? = null
private val attemptToResumeMsgFromWxFlag = AtomicBoolean(false)

private var activityPluginBinding: ActivityPluginBinding? = null

private fun handelIntent(intent: Intent) {
intent.getStringExtra(KEY_FLUWX_REQUEST_INFO_EXT_MSG)?.let {
Expand Down Expand Up @@ -96,10 +101,22 @@ class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
call.method == "openWeChatInvoice" -> openWeChatInvoice(call, result)
call.method == "openUrl" -> openUrl(call, result)
call.method == "openRankList" -> openRankList(result)
call.method == "attemptToResumeMsgFromWx" -> attemptToResumeMsgFromWx(result)
else -> result.notImplemented()
}
}

private fun attemptToResumeMsgFromWx(result: Result) {
if (attemptToResumeMsgFromWxFlag.compareAndSet(false, true)) {
activityPluginBinding?.activity?.intent?.let {
FluwxRequestHandler.handleRequestInfoFromIntent(it)
}
result.success(null)
} else {
result.error("attemptToResumeMsgFromWx error", null, null)
}
}

private fun openWeChatInvoice(call: MethodCall, result: Result) {
if (WXAPiHandler.wxApi == null) {
result.error("Unassigned WxApi", "please config wxapi first", null)
Expand Down Expand Up @@ -127,6 +144,7 @@ class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
shareHandler?.onDestroy()
authHandler?.removeAllListeners()
activityPluginBinding = null
}

override fun onDetachedFromActivity() {
Expand All @@ -141,6 +159,7 @@ class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,

override fun onAttachedToActivity(binding: ActivityPluginBinding) {
// WXAPiHandler.setContext(binding.activity.applicationContext)
activityPluginBinding = binding
handelIntent(binding.activity.intent)
FluwxRequestHandler.handleRequestInfoFromIntent(binding.activity.intent)
shareHandler?.permissionHandler = PermissionHandler(binding.activity)
Expand Down Expand Up @@ -287,6 +306,7 @@ class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
result.success(false)
}
}

private fun openRankList(result: Result) {
val req = OpenRankList.Req()
WXAPiHandler.wxApi?.sendReq(req, SendReqCallback {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.jarvan.fluwx.utils.KEY_FLUWX_REQUEST_INFO_BUNDLE
import com.jarvan.fluwx.utils.KEY_FLUWX_REQUEST_INFO_EXT_MSG
import com.jarvan.fluwx.utils.startFlutterActivity
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelmsg.LaunchFromWX
import com.tencent.mm.opensdk.modelmsg.ShowMessageFromWX
import java.security.cert.Extension

Expand All @@ -42,52 +43,74 @@ object FluwxRequestHandler {
val type = getInt("_wxapi_command_type", -9999)
if (type == 4) {
handleShowMessageFromWXBundle(this)
} else if (type == 6) {
handleWXLaunchFromWXBundle(this)
}
}
}

private fun handleShowMessageFromWXBundle(bundle: Bundle) =
handleWXShowMessageFromWX(ShowMessageFromWX.Req(bundle))

private fun handleWXLaunchFromWXBundle(bundle: Bundle) =
handleWXLaunchFromWX(LaunchFromWX.Req(bundle))

private fun handleRequest(req: BaseReq) {
when (req) {
is ShowMessageFromWX.Req -> handleWXShowMessageFromWX(req)
is LaunchFromWX.Req -> handleWXLaunchFromWX(req)
}
}

private fun handleWXShowMessageFromWX(req: ShowMessageFromWX.Req) {
val result = mapOf(
"extMsg" to req.message.messageExt
"extMsg" to req.message.messageExt,
"messageAction" to req.message.messageAction,
"description" to req.message.description,
"lang" to req.lang,
"description" to req.country,
)
FluwxPlugin.extMsg = req.message.messageExt;

FluwxPlugin.extMsg = req.message.messageExt
FluwxPlugin.callingChannel?.invokeMethod("onWXShowMessageFromWX", result)
}

private fun handleWXLaunchFromWX(req: LaunchFromWX.Req) {
val result = mapOf(
"extMsg" to req.messageExt,
"messageAction" to req.messageAction,
"lang" to req.lang,
"country" to req.country,
)

FluwxPlugin.callingChannel?.invokeMethod("onWXLaunchFromWX", result)
}

private fun defaultOnReqDelegate(baseReq: BaseReq, activity: Activity) {
// FIXME: 可能是官方的Bug,从微信拉起APP的Intent类型不对,无法跳转回Flutter Activity
// 稳定复现场景:微信版本为7.0.5,小程序SDK为2.7.7
if (baseReq.type == 4) {
// com.tencent.mm.opensdk.constants.ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX = 4
if (!WXAPiHandler.coolBoot) {
handleRequest(baseReq)
activity.startFlutterActivity()
} else {
when (baseReq) {
is ShowMessageFromWX.Req -> {
activity.startFlutterActivity(
wxRequestBundle = Bundle().apply {
baseReq.toBundle(this)
},
bundle = Bundle().apply {
putString(
KEY_FLUWX_REQUEST_INFO_EXT_MSG,
baseReq.message.messageExt
)
})
WXAPiHandler.coolBoot = false
}

// com.tencent.mm.opensdk.constants.ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX = 4
if (!WXAPiHandler.coolBoot) {
handleRequest(baseReq)
activity.startFlutterActivity()
} else {
when (baseReq) {
is ShowMessageFromWX.Req -> {
activity.startFlutterActivity(
wxRequestBundle = Bundle().apply {
baseReq.toBundle(this)
},
bundle = Bundle().apply {
putString(
KEY_FLUWX_REQUEST_INFO_EXT_MSG,
baseReq.message.messageExt
)
})
WXAPiHandler.coolBoot = false
}
}

}
}

Expand Down
Loading

0 comments on commit ba1f5ab

Please sign in to comment.