You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ionic应用Android端ng-recaptcha集成异常求助

Fixing reCAPTCHA "Domain not valid" & Connection Issues in Ionic Android Apps

Hey there, let's tackle this reCAPTCHA issue you're facing in your Ionic Android app. Those two error messages ("Cannot contact reCAPTCHA service" and "Domain not valid for the sitekey") give us clear clues to work through. Here's how to resolve this step by step:

1. Double-Check Your reCAPTCHA Key Type

You mentioned using an Android-specific public key, but let's confirm you didn't mix up key types:

  • Android reCAPTCHA keys are tied directly to your app's package name and SHA-1 signature — they're not the same as web-based reCAPTCHA v2/v3 keys. If you created a "reCAPTCHA v2 (Checkbox)" key in the Google reCAPTCHA console, even if you added a package name, it'll throw the "domain not valid" error.
  • Head to your Google reCAPTCHA admin dashboard and verify your key is explicitly labeled reCAPTCHA Android. Also, make sure the package name listed matches your app's exactly (case matters — com.myapp.app vs com.MyApp.App are treated as different).

2. Verify Your App's Actual Package Name

Sometimes the package name in your config files doesn't match what's actually built into the APK:

  • For Capacitor projects: Check the appId field in capacitor.config.ts and cross-reference it with platforms/android/app/src/main/AndroidManifest.xml (look for the package attribute).
  • For Cordova projects: Confirm the id attribute in config.xml and the package value in the same AndroidManifest file are identical to what's in your reCAPTCHA settings.

3. Resolve Network Connection Blockages

The "Cannot contact reCAPTCHA service" error often points to network restrictions:

  • Test with a different network (switch from WiFi to mobile data, or vice versa) — some networks block Google's reCAPTCHA endpoints.
  • Ensure your AndroidManifest.xml includes the internet permission (it's easy to miss!):
    <uses-permission android:name="android.permission.INTERNET" />
    

4. Adjust ng-recaptcha for Ionic's WebView

ng-recaptcha is built for web environments, so it needs a small tweak to work reliably in Ionic's WebView:

  • Add navigation permissions for reCAPTCHA's domains in your config.xml to prevent the WebView from blocking requests:
    <allow-navigation href="https://www.google.com/recaptcha/**" />
    <allow-intent href="https://www.google.com/recaptcha/**" />
    
  • Double-check that you've imported RecaptchaModule correctly in your Ionic module and that you're passing the Android-specific site key when initializing the component.

5. Don't Forget the SHA-1 Signature

Android reCAPTCHA requires both the package name and your app's signing SHA-1 fingerprint — this is a super common oversight:

  • For debug builds: Use this command to get your debug keystore's SHA-1 and add it to your reCAPTCHA dashboard:
    keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
    
  • For production builds: Generate the SHA-1 from your production keystore file and add that to the reCAPTCHA settings as well.

If you're still stuck, try enabling debug mode temporarily (connect the device to Chrome and use remote debugging to check the WebView console logs) — you'll get more detailed error messages that can pinpoint exactly where the issue is.

内容的提问来源于stack exchange,提问作者Tim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:23:15