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

Added support for app open ads #44

Merged
merged 9 commits into from
Jul 2, 2024
Merged

Added support for app open ads #44

merged 9 commits into from
Jul 2, 2024

Conversation

britzl
Copy link
Contributor

@britzl britzl commented Jun 15, 2024

Fixes #23

@britzl
Copy link
Contributor Author

britzl commented Jun 15, 2024

The problem with the current solution is that it doesn't hook in before the DefoldActivity is shown the first time. Once the app is running and put in the background and then back to the foreground it works though.

In order to also show an add on app start we also need to extend the Application class (https://developers.google.com/admob/android/app-open#extend)

Note: A small Defold game that loads quickly would really not benefit from delaying gameplay while waiting for an ad to load and then show it. You can of course load the ad and then show it anyway even if the DefoldActivity has started and the engine has rendered for a second or two.

Thought: What if we don't do all of this stuff automatically in the extension and instead do it from Lua in window.set_listener() when we get a window.WINDOW_EVENT_FOCUS_GAINED event? And have an admob.load_appopen() and admob.show_appopen()?

@britzl britzl marked this pull request as draft June 15, 2024 09:40
@AGulev
Copy link
Contributor

AGulev commented Jun 15, 2024

In order to also show an add on app start we also need to extend the Application class (https://developers.google.com/admob/android/app-open#extend)

I think it's possible to avoid:

static bool g_IsAppInitialized = false;

static void OnCreateApp(JNIEnv* env, jobject activity)
{
  // just to make sure it's ccalled only once
  // there is an issue with UnregisterOnActivityCreateListener
  if (!g_IsAppInitialized)
  {
    // Don't usee dmAndroid::LoadClass on OnCreate on some android versions g_AndroidApp->activity->clazz
    // isn't initialized yet!
    jclass na_class = env->FindClass("android/app/NativeActivity");
    jmethodID get_class_loader = env->GetMethodID(na_class,"getClassLoader", "()Ljava/lang/ClassLoader;");
    jobject cls = env->CallObjectMethod(activity, get_class_loader);
    jclass class_loader = env->FindClass("java/lang/ClassLoader");
    jmethodID find_class = env->GetMethodID(class_loader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
    jstring str_class_name = env->NewStringUTF("com/defold/admob/AdmobJNI");
    jclass admob_cls = (jclass)env->CallObjectMethod(cls, find_class, str_class_name);
    env->DeleteLocalRef(str_class_name);
    jmethodID method = env->GetStaticMethodID(admob_cls, "AdMob_OnCreate", "(Landroid/app/Activity;)V");
    env->CallStaticVoidMethod(admob_cls, method, activity);
    g_IsAppInitialized = true;
  }
}

struct AdMobStaticInit
{
  AdMobStaticInit()
  {
    dmAndroid::RegisterOnActivityCreateListener(OnCreateApp);
  }
} g_AdMobStaticInit;
public static void AdMob_OnCreate(final Activity appActivity) {
// your OnCreate logic here
}

It should work

@britzl britzl marked this pull request as ready for review July 1, 2024 19:18
Copy link
Contributor

@AGulev AGulev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Android can't be compiled, it seems like wrong function used.

Should it be shown on iOS right after I run app? Because now it's shown only when I lock unclock phone

extension-admob/ext.manifest Outdated Show resolved Hide resolved
extension-admob/src/admob_android.cpp Outdated Show resolved Hide resolved
@britzl
Copy link
Contributor Author

britzl commented Jul 2, 2024

Should it be shown on iOS right after I run app? Because now it's shown only when I lock unclock phone

Let me have a look!

@britzl
Copy link
Contributor Author

britzl commented Jul 2, 2024

Should it be shown on iOS right after I run app? Because now it's shown only when I lock unclock phone

Let me have a look!

Ok, it works now.

@britzl britzl requested a review from AGulev July 2, 2024 09:56
@AGulev
Copy link
Contributor

AGulev commented Jul 2, 2024

Great!

Copy link
Contributor

@AGulev AGulev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is great addition!!!

@britzl britzl merged commit d11bf56 into master Jul 2, 2024
1 check passed
@britzl britzl deleted the Issue-23-app-open-ads branch July 2, 2024 12:46
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.

Add new ad format App Open Ads
2 participants