Open shinayser opened 4 years ago
Maybe related to iOS 14, did you add the UsageDescription?
<key>NSUserTrackingUsageDescription</key>
<string>This identifier will be used to deliver personalized ads to you.</string>
Nope, it's Android!
Nope, it's Android! @shinayser sorry about that.
I guess isLoaded is false here:
if (await rewardAd.isLoaded) {
rewardAd.show();
}
I recommend you try to show the ad rewardAd.show();
in the listener instead of after check isLoaded immediately if you want to show after it loaded because the isLoaded computed variable seems not waiting for the ad finish it's fetching process.
class TestAds extends StatefulWidget {
@override
_TestAdsState createState() => _TestAdsState();
}
class _TestAdsState extends State<TestAds> {
AdmobReward rewardAd;
@override
void initState() {
rewardAd = AdmobReward(
adUnitId: testRewardedId,
listener: (event, args) {
print('GOT EVENT $event com args: $args');
if (event == AdmobAdEvent.loaded) {
print('Ad Loaded!');
rewardAd.show();
}
},
);
rewardAd.load();
super.initState();
}
@override
Widget build(BuildContext context) {
return Container();
}
}
@ming-chu Jsut tried your solution and nope, it also doesnt work =(
@ming-chu Jsut tried your solution and nope, it also doesnt work =(
@shinayser Would you please post the code after you modified? Also, you may want to check the ad unit id used is for AdmobReward or not.
Ad format | Sample ad unit ID
Rewarded Video | ca-app-pub-3940256099942544/5224354917
I'm also having the same problem. I've tried to use the test id from Google, but the ad is still not showing. I've changed the algorithm, it's still not showing.
I'm also having the same problem. I've tried to use the test id from Google, but the ad is still not showing. I've changed the algorithm, it's still not showing.
@Arie290698 Would you please provide the code that can reproduce the problem? That will be easier to check and discuss. :)
Sorry for the late response. Here it goes:
Future<int> _showRewardedAlt() async {
var completer = Completer<int>();
AdmobReward rewardAd;
rewardAd = AdmobReward(
adUnitId: 'ca-app-pub-3940256099942544/5224354917',
listener: (event, args) {
print('EVENT ARGS $args');
switch (event) {
case AdmobAdEvent.rewarded:
completer.complete(args["rewardAmount"]);
break;
case AdmobAdEvent.clicked:
case AdmobAdEvent.impression:
case AdmobAdEvent.opened:
case AdmobAdEvent.started:
case AdmobAdEvent.completed:
print('New reward event: $event');
break;
case AdmobAdEvent.failedToLoad:
case AdmobAdEvent.leftApplication:
case AdmobAdEvent.closed:
print('Rewarded event ERROR with $event');
completer.completeError(event);
break;
case AdmobAdEvent.loaded:
print('LOADED');
rewardAd.show();
break;
}
},
);
rewardAd.load();
return completer.future;
}
Sorry for the late response. Here it goes:
Future<int> _showRewardedAlt() async { var completer = Completer<int>(); AdmobReward rewardAd; rewardAd = AdmobReward( adUnitId: 'ca-app-pub-3940256099942544/5224354917', listener: (event, args) { print('EVENT ARGS $args'); switch (event) { case AdmobAdEvent.rewarded: completer.complete(args["rewardAmount"]); break; case AdmobAdEvent.clicked: case AdmobAdEvent.impression: case AdmobAdEvent.opened: case AdmobAdEvent.started: case AdmobAdEvent.completed: print('New reward event: $event'); break; case AdmobAdEvent.failedToLoad: case AdmobAdEvent.leftApplication: case AdmobAdEvent.closed: print('Rewarded event ERROR with $event'); completer.completeError(event); break; case AdmobAdEvent.loaded: print('CARREGOU'); rewardAd.show(); break; } }, ); rewardAd.load(); return completer.future; }
@shinayser I guess the instance of AdmobReward does not exist after the function is finished before the ad loaded.
you should define the variable AdmobReward rewardAd;
outside the function
@shinayser I tried using a new project with the code below and it works.
pubspec.yaml: admob_flutter: ^1.0.0-beta.7
import 'package:admob_flutter/admob_flutter.dart';
import 'package:flutter/material.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
Admob.initialize();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
AdmobReward rewardAd;
@override
void initState() {
rewardAd = AdmobReward(
adUnitId: 'ca-app-pub-3940256099942544/5224354917',
listener: (event, args) {
print('GOT EVENT $event, args: $args');
if (event == AdmobAdEvent.loaded) {
print('Ad Loaded!');
rewardAd.show();
} else if (event == AdmobAdEvent.closed) {
rewardAd.load();
}
},
);
rewardAd.load();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Demo the AdmobReward'),
RaisedButton(
child: Text('Show Ad!'),
onPressed: () {
print('Show ad by clicking button!');
rewardAd.isLoaded.then((isLoaded) {
print('isLoaded: $isLoaded');
if (isLoaded) {
rewardAd.show();
}
});
},
),
],
),
),
);
}
}
@shinayser I guess the instance of AdmobReward does not exist after the function is finished before the ad loaded. you should define the variable
AdmobReward rewardAd;
outside the function
This is not true. The variable exists, otherwise it would throw a null pointer exception. The purpose of my function is to provide a streaightfoward way to show and ad and get its result in a single call.
@shinayser I tried your code, but the listener of AdmobReward
never called. What I guess is the instant was destroyed before the listener is called.
@ming-chu There isn't much difference from the code you posted. You doign almost the same thing as I, except that I am doing it via future Completer. Perhaps it is another bug on the library?
@shinayser I guess the instance of AdmobReward does not exist after the function is finished before the ad loaded. you should define the variable
AdmobReward rewardAd;
outside the functionThis is not true. The variable exists, otherwise it would throw a null pointer exception. The purpose of my function is to provide a streaightfoward way to show and ad and get its result in a single call.
@shinayser You are right!
After some tests, I suspect that is the problem at the listener, I move the listener out and it works. please try the code below.
Future<int> _showRewardedAlt() async {
var completer = Completer<int>();
AdmobReward rewardAd;
void listener(AdmobAdEvent event, Map<String, dynamic> args) {
print('>>>>EVENT ARGS $args');
switch (event) {
case AdmobAdEvent.rewarded:
completer.complete(args["rewardAmount"]);
break;
case AdmobAdEvent.clicked:
case AdmobAdEvent.impression:
case AdmobAdEvent.opened:
case AdmobAdEvent.started:
case AdmobAdEvent.completed:
print('New reward event: $event');
break;
case AdmobAdEvent.failedToLoad:
case AdmobAdEvent.leftApplication:
case AdmobAdEvent.closed:
print('Rewarded event ERROR with $event');
completer.completeError(event);
break;
case AdmobAdEvent.loaded:
print('CARREGOU');
rewardAd.show();
break;
}
}
rewardAd = AdmobReward(
adUnitId: 'ca-app-pub-3940256099942544/5224354917',
listener: listener,
);
rewardAd.load();
return completer.future;
}
void listener(AdmobAdEvent event, Map<String, dynamic> args) {
I tried exactly your code and my rewarded ads don't show up =(
@Arie290698 Would you please provide the code that can reproduce the problem? That will be easier to check and discuss. :)
It's okay. I've solved the problem.
@shinayser That's so weird, I try to use your original code and it works too. What I did is just stop the app and start, don't use hot reload / hot restart. Hope this can help.
@Arie290698 Would you please provide the code that can reproduce the problem? That will be easier to check and discuss. :)
It's okay. I've solved the problem.
Can you share your solution?
@shinayser Add this to initState ()
rewardAd = AdmobReward (
adUnitId: rewardId,
listener: (AdmobAdEvent event, Map <String, dynamic> args) {
if (event == AdmobAdEvent.rewarded) {
rewardAd.load ();
}
if (event == AdmobAdEvent.closed) {
rewardAd.load ();
}
},
);
loadReward();
Then add a new function loadReward()
loadReward() async{
if(await rewardAd.isLoaded == false){
rewardAd.load();
}
}
Then add this code to onClick on the button as the trigger
if(await rewardAd.isLoaded == true){
rewardAd.show();
}else{
//do something if reward not loaded
}
Run flutter clean and try rebuilding your app
If it's still not loading, try using your app's app_id and unit_id. But don't forget to add Admob.initialize (testDeviceIds: ['test_device']); Because when I try to use Google's app_id and unit_id, the ad doesn't show.
@shinayser This is my little testing app, it works with your code perfectly.
https://github.com/ming-chu/admob-flutter-reward-test https://github.com/ming-chu/admob-flutter-reward-test/blob/master/lib/main.dart
I also faced this problem too. Listener is not firing and ad is not showing.
Don't use admob_flutter. Just use firebase_admob, and it is reliable.
The listener function was not being called,
static Future<bool> showRewardVideo({String adType = 'Reward'}) {
Completer<bool> completer = Completer<bool>();
var rewarded = false;
rewardAd = AdmobReward(
adUnitId: getRewardBasedVideoAdUnitId(),
listener: (AdmobAdEvent event, Map<String, dynamic> args) {
if (event == AdmobAdEvent.closed) {}
switch (event) {
case AdmobAdEvent.loaded:
print('New Admob $adType Ad loaded!');
rewardAd.show();
break;
case AdmobAdEvent.opened:
print('Admob $adType Ad opened!');
break;
case AdmobAdEvent.closed:
print('Admob $adType Ad closed!');
if (rewarded) {
completer.complete(true);
} else {
completer.complete(false);
}
rewardAd.dispose();
break;
case AdmobAdEvent.failedToLoad:
print('Admob $adType failed to load. :(');
completer.completeError(false);
rewardAd.dispose();
break;
case AdmobAdEvent.rewarded:
print('!!!!rewarded');
rewarded = true;
break;
default:
}
},
);
rewardAd.load();
return completer.future;
}
AdsHelper.showRewardVideo().then((value) {
if (!value) {
// no reward
return;
}
//Rewarded...
}).catchError((error) {
//error loading or showing video
});
for purpose of the test, use AdmobReward.testAdUnitId to make sure it is not an AdMob setup issue.
where can I call this function? onPress or initState method?
Hello!
The banners are working fine, but the rewarded, not showing up. This is my test code:
My Log:
As you can see, the ads is being loaded up but not showing =( I am using
admob_flutter: ^1.0.0-beta.7