Skip to content

fix(ohos): bind WXApi event handler on registerApp so the first callback after cold start is not lost (#773) - #775

Merged
JarvanMo merged 1 commit into
OpenFlutter:mainfrom
Fury76:fix/ohos-bind-event-handler-on-register
Sep 23, 2026
Merged

JarvanMo merged 1 commit into
OpenFlutter:mainfrom
Fury76:fix/ohos-bind-event-handler-on-register

Conversation

@Fury76

@Fury76 Fury76 commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

Fixes #773.

Problem

On HarmonyOS, the first WeChat auth / pay after every cold start never gets a callback, so callers time out. Retrying in the same process works; killing the process brings it back. Reproduced with fluwx 5.7.7 and present on main, @tencent/wechat_open_sdk 1.0.15.

Root cause

In @tencent/wechat_open_sdk (WXAPIFactory.js):

  • responses are dispatched with this.eventHandler?.onResp(resp);
  • eventHandler is assigned only inside handleWant(want, eventHandler), the only public way to hand the SDK a handler (WXApi has no setter);
  • with current WeChat, sendReq goes through openLink, and the response comes back through the openLink completion callback (handleResult → handleWantInternal), not through onNewWant.

fluwx calls handleWant() only from onNewWant and attemptToResumeMsgFromWx. After a cold start neither has run, so eventHandler is null and the first response is dropped by the ?. with no log. WeChat's return also triggers onNewWant, which binds the handler, which is why every later attempt works.

Fix

  • registerApp binds the plugin as event handler by calling handleWant() with an empty Want. It carries no WeChat payload, so nothing is dispatched. The launch Want is deliberately not used here: that would deliver a cold-start message (e.g. launched from a mini program) before the Dart side has subscribed, and then again when the app calls attemptToResumeMsgFromWx().
  • registerApp re-binds on repeated calls instead of returning early, so the handler is always the current plugin instance.
  • attemptToResumeMsgFromWx now calls result.success(null). It never completed its MethodResult, so await fluwx.attemptToResumeMsgFromWx() hung forever on HarmonyOS, which also rules out the workaround suggested in [ohos] 冷启动后微信登录/支付回调丢失:eventHandler 仅在 onNewWant 中绑定,首次回调被静默吞掉 #773 when awaited.

No Dart API changes; Android / iOS untouched.

Testing

On a Huawei MatePad (HarmonyOS NEXT), app built with Flutter 3.27.5-ohos, fluwx 5.7.7 + this exact patch applied to its ohos/ sources (that code is identical on main), with all app-side workarounds removed:

  • 4 consecutive cold starts (app process killed each time), first WeChat login after each one: all succeeded. Before the patch, the first login after every cold start timed out and only the retry worked.
  • hilog shows the response arriving through the openLink callback, not through onNewWant, on each cold start:
[wxopensdk::WXApi] sendReq, use link
[wxopensdk::WXApi] handleResult, result = {"resultCode":0,"want":{..."parameters":{"wxopensdk_response":{"__type__":1,"authResult":true,"code":"<redacted>","errCode":0,...}}}}

The ArkTS changes also compile with flutter build hap.

🤖 Generated with Claude Code

https://claude.ai/code/session_01F7jis66xcRRD5gTkwH1uLe

…allback after cold start is not lost

The WeChat HarmonyOS SDK (@tencent/wechat_open_sdk 1.0.15) dispatches responses to the handler it got
in the most recent handleWant() call, and handleWant() is the only way to give it one. With current
WeChat versions the SDK sends requests through openLink and delivers the response from the openLink
completion callback via `this.eventHandler?.onResp(...)`, which never goes through onNewWant. fluwx only
called handleWant() from onNewWant / attemptToResumeMsgFromWx, so after every cold start the handler was
null and the first auth / pay response was dropped silently; later ones worked only because WeChat's
return had triggered onNewWant once.

- registerApp now binds the plugin as event handler through handleWant() with an empty Want. An empty
  Want carries no WeChat payload, so nothing is dispatched; the launch Want is still left to
  attemptToResumeMsgFromWx(), which apps call once their subscribers are ready (binding with the launch
  Want here would deliver a cold-start message before Dart listens, and again on attemptToResumeMsgFromWx).
- registerApp re-binds on repeated calls instead of returning early.
- attemptToResumeMsgFromWx now completes its MethodResult; previously `await attemptToResumeMsgFromWx()`
  never returned on HarmonyOS.

Fixes OpenFlutter#773

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F7jis66xcRRD5gTkwH1uLe
@JarvanMo

Copy link
Copy Markdown
Member

Thanks.

@JarvanMo
JarvanMo merged commit 3a2ffa4 into OpenFlutter:main Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ohos] 冷启动后微信登录/支付回调丢失:eventHandler 仅在 onNewWant 中绑定,首次回调被静默吞掉

2 participants