I have the following Kotlin code:
import android.os.Bundle
import com.google.android.ump.*
public class MainActivity extends AppCompatActivity {
private ConsentInformation consentInformation;
private ConsentForm consentForm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
consent();
}
public void consent() {
val debugSettings = ConsentDebugSettings.Builder(this)
.setDebugGeography(ConsentDebugSettings
.DebugGeography
.DEBUG_GEOGRAPHY_EEA)
.build()
ConsentRequestParameters params = new ConsentRequestParameters
.Builder()
.setConsentDebugSettings(debugSettings)
.setTagForUnderAgeOfConsent(false)
.build();
consentInformation = UserMessagingPlatform.getConsentInformation(this);
consentInformation.requestConsentInfoUpdate(this, params,
new ConsentInformation.OnConsentInfoUpdateSuccessListener () {
@Override
public void onConsentInfoUpdateSuccess() {
// The consent information state was updated.
// You are now ready to check if a form is available.
if (consentInformation.isConsentFormAvailable()) {
loadForm();
}
}
},
new ConsentInformation.OnConsentInfoUpdateFailureListener () {
@Override
public void onConsentInfoUpdateFailure(FormError formError) {
// Handle the error.
}
});
}
public void loadForm() {
// Loads a consent form. Must be called on the main thread.
UserMessagingPlatform.loadConsentForm(this,
new UserMessagingPlatform.OnConsentFormLoadSuccessListener () {
@Override
public void onConsentFormLoadSuccess(ConsentForm consentForm) {
MainActivity.this.consentForm = consentForm;
if (consentInformation.getConsentStatus() == ConsentInformation.ConsentStatus.REQUIRED) {
consentForm.show(MainActivity.this,
new ConsentForm.OnConsentFormDismissedListener () {
@Override
public void onConsentFormDismissed(FormError formError) {
if (consentInformation.getConsentStatus() == ConsentInformation.ConsentStatus.OBTAINED) {
MobileAds.initialize(this, initializationStatus -> {
});
}
// Handle dismissal by reloading form.
loadForm();
}
});
}
}
},
new UserMessagingPlatform . OnConsentFormLoadFailureListener () {
@Override
public void onConsentFormLoadFailure(FormError formError) {
loadForm();
}
}
);
}
}
In the above code, I need to create MethodChannel which I don't quite understand how to do. Then, I need to import the above code into Flutter. On the Flutter side, I will also need to create MethodChannel. This is for Admob UMP compliance.
I want to use Kotlin code in Flutter below:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
MobileAds.instance.initialize();
runApp(MyApp());
}
I know it is possible to import Kotlin code into Flutter since we can use a MethodChannel on Android: docs.flutter.dev/platform-integration/platform-channels?tab=type-mappings-kotlin-tab
Any help or advice will be appreciated.
Thank You
I think, the best way to do this is creating a plugin, you can follow the link below to see documentation. The MainActivity it is not the place to call method channel.
Edit:
If you create a plugin, using the default command:
flutter create --template=plugin
You will see a method called getPlatformVersion
that is the example method, you can modify and create anothers using it. You will see that in the Kotlin (or JAVA) code a line like this:
if (call.method == "getPlatformVersion") {
here is a conditional to know if the method is what you are calling, and if is, it will do and return what is necessary for the flutter method, that have the same name, in there you should see a instance of MethodChannel calling it.