diff --git a/packages/_shared/ohos/src/main/ets/components/plugin/FluwxPlugin.ets b/packages/_shared/ohos/src/main/ets/components/plugin/FluwxPlugin.ets index a7996ad7..74bddda1 100644 --- a/packages/_shared/ohos/src/main/ets/components/plugin/FluwxPlugin.ets +++ b/packages/_shared/ohos/src/main/ets/components/plugin/FluwxPlugin.ets @@ -74,7 +74,7 @@ export default class FluwxPlugin implements FlutterPlugin, MethodCallHandler, Ab WXAPiHandler.checkWeChatInstallation(result); break; case "registerApp": - WXAPiHandler.registerApp(call, result); + WXAPiHandler.registerApp(call, result, this); break; case "sendAuth": this.authHandler?.sendAuth(call, result); @@ -262,6 +262,7 @@ export default class FluwxPlugin implements FlutterPlugin, MethodCallHandler, Ab attemptToResumeMsgFromWx(result: MethodResult) { WXAPiHandler.wxApi?.handleWant(this.binding?.getAbility().launchWant, this) + result.success(null) } async launchMiniProgram(call: MethodCall, result: MethodResult) { diff --git a/packages/_shared/ohos/src/main/ets/components/plugin/handlers/WXAPiHandler.ets b/packages/_shared/ohos/src/main/ets/components/plugin/handlers/WXAPiHandler.ets index ce351205..f24fb771 100644 --- a/packages/_shared/ohos/src/main/ets/components/plugin/handlers/WXAPiHandler.ets +++ b/packages/_shared/ohos/src/main/ets/components/plugin/handlers/WXAPiHandler.ets @@ -1,6 +1,6 @@ import * as wechatOpenSDK from "@tencent/wechat_open_sdk" import { MethodCall, MethodResult } from '@ohos/flutter_ohos' -import { bundleManager, common } from '@kit.AbilityKit' +import { bundleManager, common, Want } from '@kit.AbilityKit' export class WXAPiHandler { static wxApi: wechatOpenSDK.WXApi | null = null @@ -20,22 +20,33 @@ export class WXAPiHandler { WXAPiHandler.context = context } - static registerApp(call: MethodCall, result: MethodResult) { - if (WXAPiHandler.wxApi != null) { - result.success(true) - return + static registerApp(call: MethodCall, result: MethodResult, eventHandler: wechatOpenSDK.WXApiEventHandler) { + if (WXAPiHandler.wxApi == null) { + const appId: string | null = call.argument("appId") + if (!appId) { + result.error("invalid app id", "are you sure your app id is correct ?", appId) + return + } + WXAPiHandler.registerWxAPIInternal(appId) } - const appId: string | null = call.argument("appId") - if (!appId) { - result.error("invalid app id", "are you sure your app id is correct ?", appId) - return - } - - WXAPiHandler.registerWxAPIInternal(appId) + WXAPiHandler.bindEventHandler(eventHandler) result.success(WXAPiHandler.registered) } + // The SDK dispatches every response to the handler it received in the most recent handleWant() call, + // and handleWant() is the only way to hand it one. Responses coming back through the openLink channel + // (current WeChat versions) never go through onNewWant, so until something calls handleWant() the + // handler is null and the SDK drops them silently (`this.eventHandler?.onResp(...)`). That is what + // made the first auth / pay after every cold start time out (#773). + // + // An empty Want carries no WeChat payload, so this only binds the handler: nothing is dispatched and the + // launch Want is left for attemptToResumeMsgFromWx(), which apps call once their subscribers are ready. + private static bindEventHandler(eventHandler: wechatOpenSDK.WXApiEventHandler) { + const noPayload: Want = {} + WXAPiHandler.wxApi?.handleWant(noPayload, eventHandler) + } + static checkWeChatInstallation(result: MethodResult) { const isInstalled = bundleManager.canOpenLink("weixin://") || WXAPiHandler.wxApi?.isWXAppInstalled() === true; result.success(isInstalled);