Ionic Capacitor Admob plugin loadAd null object reference - java

I am developing an Ionic Capacitor app and i am using the capacitor community ad plugin, but i am getting the following error in Android Studio:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)' on a null object reference
I am running the application on an android phone with the command ionic capacitor run android -l --external
This is how the error happens:
the app opens on the Welcome page
the app shows a banner ad
i click the login button
the app removes the banner ad
the app navigates to the login page
the app should show a banner ad, BUT the app crashes
Welcome page (login page has the same life cycle methods and calls):
ionViewWillLeave(): void {
this.removeAd();
}
ionViewWillEnter(): void {
this.showAd();
}
private async showAd(): Promise<void> {
await this.admobService.showBanner();
}
private async removeAd(): Promise<void> {
await this.admobService.removeBanner();
}
Admob service:
public async showBanner(): Promise<void> {
AdMob.addListener(BannerAdPluginEvents.FailedToLoad, (info) => {
//console.log(info);
});
const options: BannerAdOptions = {
adId: 'hidden',
adSize: BannerAdSize.BANNER,
position: BannerAdPosition.TOP_CENTER,
margin: 0,
isTesting: true,
};
AdMob.showBanner(options);
}
public async removeBanner(): Promise<void> {
await AdMob.removeBanner();
}
Admob service is provided in root, and maybe that could be the case? But i would not like to provide it in every page specifically

Probably, the banner destroys while it trying to show. You can try add a delay via setTimeout (a few seconds for check this idea) for this.showAd(); call:
ionViewWillEnter(): void {
setTimeout(() => { this.showAd(); }, 3000);
}

Related

inApp Update, download but not installed

I have a problem with inapp update. A new version window appears, when you press the "Update now" button, unfortunately nothing happens, the application downloads in the background, but nothing happens app is not updated. I would like to add that I am testing it on the "Open Test" in play store path.
private void checkForUpdate() {
appUpdateManager
.getAppUpdateInfo()
.addOnSuccessListener(
appUpdateInfo -> {
// Checks that the platform will allow the specified type of update.
if ((appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE)
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE))
{
// Request the update.
try {
appUpdateManager.startUpdateFlowForResult(
appUpdateInfo,
AppUpdateType.FLEXIBLE,
this,
REQUEST_APP_UPDATE);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
});
}
You are using the FLEXIBLE method for updating the App, which will download the app but will not auto-update it.
You have to monitor the flexible update progress and once the update is downloaded, you have to install it manually.
private val installStateUpdatedListener = InstallStateUpdatedListener { state ->
when (state.installStatus()) {
InstallStatus.DOWNLOADED -> completeUpdate()
InstallStatus.FAILED -> reportUpdateFailure(state.installErrorCode())
else -> {
// Right now we only care about download success / failed state
}
}
}
// Before starting an update, register a listener for updates.
appUpdateManager.registerListener(listener)
// Start an update.
// When status updates are no longer needed, unregister the listener.
appUpdateManager.unregisterListener(listener)
Once the update is downloaded, you have to call appUpdateManager.completeUpdate() to complete the update.
You can get more information here: https://developer.android.com/guide/playcore/in-app-updates/kotlin-java#flexible

Google Play Install Referrer Library - Referrer Generation

I'm developing an android application in Java where I need to pass the referer information to an URL. I'm getting the referrer information using Play Install Referrer Library.
Here is my code:
InstallReferrerClient referrerClient = InstallReferrerClient.newBuilder(this).build();
referrerClient.startConnection(new InstallReferrerStateListener() {
#Override
public void onInstallReferrerSetupFinished(int responseCode) {
switch (responseCode) {
case InstallReferrerClient.InstallReferrerResponse.OK:
try {
Log.v("TAG", "InstallReferrer conneceted");
ReferrerDetails response = referrerClient.getInstallReferrer();
System.out.println("referrerUrl ID: " + response);
referrerClient.endConnection();
} catch (RemoteException e) {
e.printStackTrace();
}
break;
case InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
Log.w("TAG", "InstallReferrer not supported");
break;
case InstallReferrerClient.InstallReferrerResponse.SERVICE_UNAVAILABLE:
Log.w("TAG", "Unable to connect to the service");
break;
default:
Log.w("TAG", "responseCode not found.");
}
}
#Override
public void onInstallReferrerServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
}
});
Code is working fine and currently, the above snippet is inside my activity's onCreate method. which means it will start the new connection every time the user opens the activity.
In the library documentation, they have written that
Caution: The install referrer information will be available for 90
days and won't change unless the application is reinstalled. To avoid
unnecessary API calls in your app, you should invoke the API only once
during the first execution after install.
This is where I'm stuck, should I just call this thing when the application starts the first time?
If yes, I can store this referrer in the shared preference, but then how will I able to know that 90 days have been passed and I need to trigger that action again? Or it there something else that should I need to implement? Kindly help me with this issue.

Android webRTC video call inside a Background Service

Forgive me if this question was already asked, I couldn't find an answer for my case.
So, I have an Android app with Voice & Video call feature. I used webRTC for this.
I was able to make both Voice and Video call working perfectly inside an Activity, but now I want to keep the call running while the user exit the CallActivity and go back to the ChatActivity (to send a file/link/photo for example).
I managed to make the Voice call run perfectly inside a Background Service, but video call won't work as expected.
The remote video won't be displayed even though the audio from the video track is playing.
here is my Background Service code :
#Override
public void onAddStream(MediaStream mediaStream) {
if (mediaStream.videoTracks.size() > Constants.ONE || mediaStream.audioTracks.size() > Constants.ONE) {
return;
}
//check for video track, means this is a video call
if (!isAudioCall && mediaStream.videoTracks.size() > Constants.ZERO) {
remoteVideoTrack = mediaStream.videoTracks.get(Constants.ZERO);
CallActivityNew.remoteVideoTrack = remoteVideoTrack;
try {
localAudioTrack.setEnabled(true);
//Now ask the UI to display the video track
sendOrderToActivity(Constants.START_REMOTE_VIDEO, null);
} catch (Exception ignored) {}
} else if (mediaStream.audioTracks.size() > Constants.ZERO) {
//Means this is a Voice call, only audio tracks available
remoteAudioTrack = mediaStream.audioTracks.get(Constants.ZERO);
try {
localAudioTrack.setEnabled(true);
remoteAudioTrack.setEnabled(true);
} catch (Exception ignored) {}
}
}
and below my CallActivity code :
case Constants.START_REMOTE_VIDEO: {
if (remoteVideoView == null) {
remoteVideoView = findViewById(R.id.remote_gl_surface_view);
}
remoteVideoView.init(eglBaseContext, null);
remoteVideoView.setEnableHardwareScaler(true);
remoteVideoView.setMirror(true);
remoteVideoView.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT);
remoteVideoView.setZOrderMediaOverlay(true);
//Apply video track to the Surface View in order to display it
remoteVideoTrack.addSink(remoteVideoView);
//now enable local video track
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//now enable local video track
remoteVideoTrack.setEnabled(true);
}
}, Constants.TIME_THREE_HUNDRED_MILLIS);
setSpeakerphoneOn(false);
break;
}
I am sending orders from Service to Activity, the "case Constants.START_REMOTE_VIDEO" work after receiving the order from Service.
I don't see where the problem, why am I only hearing sound but the remote video won't start display !!
Thank you in advance for helping.
After testing for long hours, I found that my code works just fine, I just forget to change the view visibility from "GONE" to "VISIBLE".
Yeah that was the solution, i swear xD

Apply Deep linking in flutter app for android and ios

Apply Deep Linking in flutter app to open
a specific page in another app or same app
i want to know to how implement deep links in flutter
or open channel with android native and ios native ?
?
I think it would be the same as in a normal android app. Deeplinking is a configuration thing rather than code.
You need to write some things in your android manifest.
Have a look at the firebase manual for deep linking:
Firebase deeplinking manual
You can use firebase dynamic links for deep linking in a flutter. Refer this link for full implement steps and create and receive a link, https://medium.com/better-programming/deep-linking-in-flutter-with-firebase-dynamic-links-8a4b1981e1eb.
Here is a sample code for receiving a link inside the app and open a new screen.
class MainWidgetState extends State<MainWidget> {
#override
void initState() {
super.initState();
this.initDynamicLinks();
}
initDynamicLinks(BuildContext context) async {
await Future.delayed(Duration(seconds: 3));
var data = await FirebaseDynamicLinks.instance.getInitialLink();
var deepLink = data?.link;
final queryParams = deepLink.queryParameters;
if (queryParams.length > 0) {
var userName = queryParams['userId'];
openNewScreen(userName);
}
FirebaseDynamicLinks.instance.onLink(onSuccess: (dynamicLink)
async {
var deepLink = dynamicLink?.link;
final queryParams = deepLink.queryParameters;
if (queryParams.length > 0) {
var userName = queryParams['userId'];
openNewScreen(userName);
}
debugPrint('DynamicLinks onLink $deepLink');
}, onError: (e) async {
debugPrint('DynamicLinks onError $e');
});
}
openNewScreen(String userName){
Navigator.of(context).pushNamed("routeFormScreen", arguments: {"name": userName});
}
}

Handling Advertisements in Native app using Selenium Java

I am automating Android Native Gaming App, our app renders ads from different ad networks. After submitting each game you may see static ad and video ad or you may not see an ad at all. If video ad is found it may vary from 30 secs to 1 min.
Important thing, when i use Appium inspector to spy various video ad screens shall be identified only by Class(android.webkit.WebView, android.widget.VideoView, android.widget.Button, android.view.View, android.widget.Image & android.widget.ImageView.). After Ad complete playing we need to click on device back button and play next game.
Can you suggest any good approach to automate this kind of app? Any sample code is much appreciated.
Option I: you have is to ask your developers to create a version of the application without ads.
Advantages - No ads.
Disadvantages - You will not be testing the exact same code as you plan to release.
You can disable only fullscreen ad.
I think there is no best way to do this. Stable automate checks or checking the exact same code as you plan to release.
Option II: is to catch if the ad is visible, and press back button.
For example (example fo Android):
protected boolean checkAdvert(AppiumDriver<WebElement> driver, int timeout) {
By adTree = By.xpath("//hierarchy/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]");
Map<String, Object> adParams = new HashMap<>();
//trying to wait for the ad to come up and then click the Expense button
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
driver.context("NATIVE_APP");
FluentWait<WebDriver> await = new FluentWait<WebDriver> (driver)
.withTimeout(timeout, TimeUnit.SECONDS)
.pollingEvery(500, TimeUnit.MILLISECONDS)
.ignoring(NoSuchElementException.class);
try {
await.until (ExpectedConditions.visibilityOf(driver.findElement(adTree)));
// go BACK to eliminate the popup
adParams.clear();
adParams.put("keySequence", "BACK");
driver.executeScript("mobile:presskey", adParams);
System.out.println("Press the back button to get out of ad");
return true;
} catch (Exception t) {
System.out.println("no ad showed up");
return false;
}
}
and use this in page object class:
public void addExp(String desc, String amount) {
do {
try {
driver.context("WEBVIEW");
driver.findElement(expDesc).sendKeys(desc);
driver.findElement(expAmnt).sendKeys(amount);
adClick = false;
} catch (NoSuchElementException ne) {
adClick = checkAdvert(driver, 1);
if (!adClick) throw ne;
}
} while (adClick);
}
But you must remember that ads may be different, you can try to find a universal selector. But I think it will hard to cover all cases.
adTree = By.xpath("//hierarchy/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]");

Categories