I am facing this error on flutter native ads (android):
PlatformException(NativeAdError, Can't find NativeAdFactory with id: homeScreen, null, null)
google_mobile_ads: ^0.13.0
this is my HomeScreenNativeAd.kt file
class HomeScreenNativeAd(val context: Context) : GoogleMobileAdsPlugin.NativeAdFactory {
override fun createNativeAd(
nativeAd: NativeAd,
customOptions: MutableMap<String, Any>?
): NativeAdView {
val homeScreenNativeView = LayoutInflater.from(context)
.inflate(R.layout.home_screen_native_ad, null) as NativeAdView
with(homeScreenNativeView) {
val attributionViewSmall =
findViewById<TextView>(R.id.tv_home_item_native_ad_attribution_small)
val icon = findViewById<ImageView>(R.id.home_item_bg_image)
val image = nativeAd.icon
if (image != null) {
attributionViewSmall.visibility = View.VISIBLE
icon.setImageDrawable(image.drawable)
} else {
attributionViewSmall.visibility = View.INVISIBLE
}
this.iconView = iconView
val adV = findViewById<TextView>(R.id.adviser)
adV.text = nativeAd.advertiser
this.advertiserView =adV
val ctaButton = findViewById<TextView>(R.id.home_screen_cta_button)
ctaButton.text = nativeAd.callToAction
this.callToActionView = ctaButton
val adDesc = findViewById<TextView>(R.id.home_item_ad_desc)
adDesc.text = nativeAd.body
this.bodyView = ctaButton
val headlineView = findViewById<TextView>(R.id.home_item_ad_title)
headlineView.text = nativeAd.headline
this.headlineView = headlineView
setNativeAd(nativeAd)
}
return homeScreenNativeView
}
}
home_screen_native_ad.xml file
`
<com.google.android.gms.ads.nativead.NativeAdView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="160dp">
<FrameLayout
android:layout_width="match_parent"
android:background="#00ffffff"
android:layout_height="140dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="190dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:gravity="start"
android:orientation="horizontal">
<ImageView
android:id="@+id/home_item_bg_image"
android:layout_width="60dp"
android:layout_height="60dp"
android:scaleType="centerCrop"
android:layout_marginLeft="16dp"
/>
<TextView
android:id="@+id/home_item_ad_title"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/home_item_bg_image"
android:ellipsize="end"
android:lines="1"
android:maxLines="1"
android:textColor="#FFFFFFFF"
android:textSize="20sp"
tools:text="Headline" />
<TextView
android:id="@+id/adviser"
android:layout_width="229dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="32dp"
android:layout_toRightOf="@+id/home_item_bg_image"
android:ellipsize="end"
android:lines="1"
android:maxLines="1"
android:textColor="#FFFFFFFF"
android:textSize="14sp"
tools:text="Advertiser" />
<TextView
android:id="@+id/tv_home_item_native_ad_attribution_small"
android:layout_width="35dp"
android:layout_height="20dp"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/home_item_ad_title"
android:background="@color/softOrange"
android:gravity="center"
android:lines="1"
android:maxLines="1"
android:text="Ad"
android:textColor="#FFFF"
android:textSize="12sp" />
</RelativeLayout>
<TextView
android:id="@+id/home_item_ad_desc"
android:layout_width="270dp"
android:layout_height="40dp"
android:layout_marginTop="85dp"
android:ellipsize="end"
android:lines="2"
android:gravity="center_vertical"
android:maxLines="2"
android:textColor="#FFFFFFFF"
android:textSize="14sp"
android:layout_marginLeft="16dp"
tools:text="Headline" />
<TextView
android:id="@+id/home_screen_cta_button"
android:layout_width="wrap_content"
android:layout_marginLeft="10dp"
android:layout_height="30dp"
android:layout_toRightOf="@+id/home_item_ad_desc"
android:layout_marginTop="100dp"
android:text="INSTALL"
android:gravity="center"
android:ellipsize="end"
android:lines="1"
android:textColor="#FFF"
android:maxLines="1"
android:textSize="18sp"
/>
</RelativeLayout>
</FrameLayout>
</com.google.android.gms.ads.nativead.NativeAdView>`
MainActivity.kt
`class MainActivity : FlutterActivity() {
private val CHANNEL = "verified.cv/channel" //The channel name you set in your main.dart file
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
// Note: this method is invoked on the main thread.
call, result ->
if (call.method == "createNotificationChannel") {
val argData = call.arguments as java.util.HashMap<String, String>
val completed = createNotificationChannel(argData)
if (completed == true) {
result.success(completed)
} else {
result.error("Error Code", "Error Message", null)
}
} else {
result.notImplemented()
}
}
GoogleMobileAdsPlugin.registerNativeAdFactory(
flutterEngine, "listTile", ListTileNativeAdFactory(context))
GoogleMobileAdsPlugin.registerNativeAdFactory(
flutterEngine, "skillCard", SkillCardNativeFactory(context))
GoogleMobileAdsPlugin.registerNativeAdFactory(
flutterEngine, "homeScreen", HomeScreenNativeAd(context))
GoogleMobileAdsPlugin.registerNativeAdFactory(
flutterEngine, "feedItem", FeedItemNativeFactory(context))
}
override fun cleanUpFlutterEngine(flutterEngine: FlutterEngine) {
super.cleanUpFlutterEngine(flutterEngine)
// TODO: Unregister the ListTileNativeAdFactory
GoogleMobileAdsPlugin.unregisterNativeAdFactory(flutterEngine, "listTile")
GoogleMobileAdsPlugin.unregisterNativeAdFactory(flutterEngine, "skillCard")
GoogleMobileAdsPlugin.unregisterNativeAdFactory(flutterEngine, "homeScreen")
GoogleMobileAdsPlugin.unregisterNativeAdFactory(flutterEngine, "feedItem")
}
private fun createNotificationChannel(mapData: HashMap<String, String>): Boolean {
val completed: Boolean
if (VERSION.SDK_INT >= VERSION_CODES.O) {
// Create the NotificationChannel
val id = mapData["id"]
val name = mapData["name"]
val descriptionText = mapData["description"]
// val sound = "your_sweet_sound"
val importance = NotificationManager.IMPORTANCE_HIGH
val mChannel = NotificationChannel(id, name, importance)
mChannel.description = descriptionText
// val soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getApplicationContext().getPackageName() + "/raw/your_sweet_sound");
// val att = AudioAttributes.Builder()
// .setUsage(AudioAttributes.USAGE_NOTIFICATION)
// .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
// .build();
//mChannel.setSound(soundUri, att)
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(mChannel)
completed = true
} else {
completed = false
}
return completed
}
}
`
I/flutter ( 1409): ----------------FIREBASE CRASHLYTICS----------------
I/flutter ( 1409): PlatformException(NativeAdError, Can't find NativeAdFactory with id: homeScreen, null, null)
I/flutter ( 1409): #0 StandardMethodCodec.decodeEnvelope
package:flutter/…/services/message_codecs.dart:607
I/flutter ( 1409): #1 MethodChannel._invokeMethod
package:flutter/…/services/platform_channel.dart:177
I/flutter ( 1409): <asynchronous suspension>
I/flutter ( 1409): #2 NativeAd.load
package:google_mobile_ads/src/ad_containers.dart:1033
I/flutter ( 1409): <asynchronous suspension>
I/flutter ( 1409): ----------------------------------------------------
D/ACodec ( 1409): dataspace changed to 0x10c40000 (R:2(Limited), P:4(BT601_6_525), M:3(BT601_6), T:3(SMPTE170M)) (