Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Admob Ads Calling Mechanism while VPN is connected #30

Open
connect-hassan847 opened this issue Aug 8, 2024 · 2 comments
Open

Admob Ads Calling Mechanism while VPN is connected #30

connect-hassan847 opened this issue Aug 8, 2024 · 2 comments

Comments

@connect-hassan847
Copy link

When VPN is not connected ads request will be from my original API. And if VPN connect admob ads requests will be from different IP address. Google admob will detect and block the ads. How these SDKs are handling this issue ?

@connect-hassan847
Copy link
Author

Is there anyone to give the answer ?

@oneconnectapi
Copy link
Owner

Hello you need to handle by external code

You can use Android's ConnectivityManager to manage network traffic and route certain requests outside of the VPN, i.e., make the AdMob traffic go directly through the device's default network interface, bypassing the VPN.

Step 1: Get the Default Network (Outside VPN) Use ConnectivityManager to retrieve the default network, which isn’t affected by the VPN. This will allow you to send the AdMob requests through the default network, even if the VPN is connected.

java

ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Network defaultNetwork = connectivityManager.getActiveNetwork();

Step 2: Set Up Network-Specific Traffic With Android 10 (API level 29) and higher, you can use the bindProcessToNetwork() method to bind your process or specific ad requests to a particular network, such as the default network.

java

connectivityManager.bindProcessToNetwork(defaultNetwork);

This will route traffic through the network interface that is not using the VPN.

Step 3: Send AdMob Requests After binding to the default network, any HTTP requests or SDK calls (such as AdMob ad requests) will bypass the VPN. You can switch the network binding dynamically based on your needs.

After serving the ads, you can unbind from the network by calling:

java

connectivityManager.bindProcessToNetwork(null);
  1. Direct HTTP Connections to AdMob Using Java

If you are handling HTTP requests to AdMob directly, you can configure your HTTP client to use the default network.

Step 1: Use a Custom HttpURLConnection: When making HTTP requests, you can configure the URL connection to use the default network interface. Below is an example of making a URL connection via the default network.

java

URL url = new URL("https://googleads.g.doubleclick.net/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
if (defaultNetwork != null) {
defaultNetwork.bindSocket(urlConnection.getSocket());
}
InputStream in = new BufferedInputStream(urlConnection.getInputStream());

Step 2: Use Retrofit or OkHttp (Optional): If you’re using a network library like Retrofit or OkHttp, you can modify them to bind to a specific network when sending HTTP requests.

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

No branches or pull requests

2 participants