import 'dart:convert';
|
import 'dart:io';
|
import 'package:flutter/material.dart';
|
import 'package:flutter_unionad/flutter_unionad.dart';
|
import 'package:locations/model/common/adinfo_model.dart';
|
import 'package:locations/utils/config_util.dart';
|
import 'package:locations/utils/string_util.dart';
|
import 'package:flutter_tencentad/flutter_tencentad.dart';
|
|
//紧急联系人输入框确定事件
|
typedef OnAdCallback = void Function(bool success, String msg);
|
|
class AdUtil {
|
static Future init() async {
|
await CSJAdUtil.init();
|
await GDTAdUtil.init();
|
}
|
|
static void showSplashAd(
|
double width, double height, OnAdCallback adCallback) async {
|
var csjAppId = await ConfigUtil.getConfig(ConfigKey.csjAppId);
|
var gdtAppId = await ConfigUtil.getConfig(ConfigKey.gdtAppId);
|
if (StringUtil.isNullOrEmpty(csjAppId) &&
|
StringUtil.isNullOrEmpty(gdtAppId)) {
|
adCallback(false, "appId为空");
|
return;
|
}
|
|
var adInfo = await getAdInfo(AdPosition.splash);
|
if (adInfo == null) {
|
adCallback(false, "appId为空");
|
return;
|
}
|
|
if (adInfo.type == "csj") {
|
CSJAdUtil.loadSplash(adInfo.pid!, width, height, (success, msg) {});
|
} else {
|
GDTAdUtil.loadSplash(adInfo.pid!, (success, msg) {});
|
}
|
|
//加载广告
|
}
|
|
static Future<AdinfoModel?> getAdInfo(String position) async {
|
var result = await ConfigUtil.getConfig(position);
|
if (result == null) {
|
return null;
|
}
|
AdinfoModel model = AdinfoModel.fromJson(jsonDecode(result));
|
if (StringUtil.isNullOrEmpty(model.type)) {
|
return null;
|
}
|
}
|
}
|
|
class CSJAdUtil {
|
static Future init() async {
|
var csjAppId = await ConfigUtil.getConfig(ConfigKey.csjAppId);
|
//初始化穿山甲
|
if (!StringUtil.isNullOrEmpty(csjAppId)) {
|
// if (Platform.isAndroid) {
|
// await FlutterUnionad.andridPrivacy(
|
// isCanUseLocation: false,
|
// //是否允许SDK主动使用地理位置信息 true可以获取,false禁止获取。默认为true
|
// lat: 1.0,
|
// //当isCanUseLocation=false时,可传入地理位置信息,穿山甲sdk使用您传入的地理位置信息lat
|
// lon: 1.0,
|
// //当isCanUseLocation=false时,可传入地理位置信息,穿山甲sdk使用您传入的地理位置信息lon
|
// isCanUsePhoneState: false,
|
// //是否允许SDK主动使用手机硬件参数,如:imei
|
// imei: "123",
|
// //当isCanUsePhoneState=false时,可传入imei信息,穿山甲sdk使用您传入的imei信息
|
// isCanUseWifiState: false,
|
// //是否允许SDK主动使用ACCESS_WIFI_STATE权限
|
// isCanUseWriteExternal: false,
|
// //是否允许SDK主动使用WRITE_EXTERNAL_STORAGE权限
|
// oaid: "111", //开发者可以传入oaid
|
// );
|
// }
|
|
await FlutterUnionad.register(
|
androidAppId: csjAppId!,
|
//穿山甲广告 Android appid 必填
|
iosAppId: "",
|
//穿山甲广告 ios appid 必填
|
useTextureView: false,
|
//使用TextureView控件播放视频,默认为SurfaceView,当有SurfaceView冲突的场景,可以使用TextureView 选填
|
appName: "定位追踪轨迹",
|
//appname 必填
|
allowShowNotify: true,
|
//是否允许sdk展示通知栏提示 选填
|
allowShowPageWhenScreenLock: true,
|
//是否在锁屏场景支持展示广告落地页 选填
|
debug: true,
|
//是否显示debug日志
|
supportMultiProcess: true,
|
//是否支持多进程,true支持 选填
|
directDownloadNetworkType: [
|
FlutterUnionadNetCode.NETWORK_STATE_4G,
|
FlutterUnionadNetCode.NETWORK_STATE_WIFI
|
]); //允许直接下载的网络状态集合 选填
|
}
|
}
|
|
static Widget? loadSplash(
|
String? pid, double width, double height, OnAdCallback adCallback) {
|
if (pid == null) {
|
adCallback(false, "pid为空");
|
return null;
|
}
|
return FlutterUnionad.splashAdView(
|
//是否使用个性化模版 设定widget宽高
|
mIsExpress: true,
|
//android 开屏广告广告id 必填
|
androidCodeId: pid,
|
//ios 开屏广告广告id 必填
|
iosCodeId: "",
|
//是否支持 DeepLink 选填
|
supportDeepLink: true,
|
// 期望view 宽度 dp 选填 mIsExpress=true必填
|
expressViewWidth: width,
|
//期望view高度 dp 选填 mIsExpress=true必填
|
expressViewHeight: height,
|
callBack: FlutterUnionadSplashCallBack(
|
onShow: () {
|
print("开屏广告显示");
|
},
|
onClick: () {
|
print("开屏广告点击");
|
},
|
onFail: (error) {
|
adCallback(false, error);
|
},
|
onFinish: () {
|
print("开屏广告倒计时结束");
|
adCallback(true, "");
|
},
|
onSkip: () {
|
print("开屏广告跳过");
|
adCallback(true, "");
|
},
|
onTimeOut: () {
|
print("开屏广告超时");
|
adCallback(false, "开屏广告超时");
|
},
|
),
|
);
|
}
|
|
static Widget loadBanner(
|
String pid, double width, double height, OnAdCallback callback) {
|
return FlutterUnionad.bannerAdView(
|
//andrrid banner广告id 必填
|
androidCodeId: pid,
|
//ios banner广告id 必填
|
iosCodeId: "",
|
//是否使用个性化模版
|
mIsExpress: true,
|
//是否支持 DeepLink 选填
|
supportDeepLink: true,
|
//一次请求广告数量 大于1小于3 必填
|
expressAdNum: 3,
|
//轮播间隔事件 30-120秒 选填
|
expressTime: 30,
|
// 期望view 宽度 dp 必填
|
expressViewWidth: width,
|
//期望view高度 dp 必填
|
expressViewHeight: height,
|
//广告事件回调 选填
|
callBack: FlutterUnionadBannerCallBack(onShow: () {
|
print("banner广告加载完成");
|
}, onDislike: (message) {
|
print("banner不感兴趣 $message");
|
callback(false, "banner不感兴趣 $message");
|
}, onFail: (error) {
|
print("banner广告加载失败 $error");
|
}, onClick: () {
|
print("banner广告点击");
|
}),
|
);
|
}
|
|
static Widget loadExpress(
|
String pid, double width, double height, OnAdCallback callback) {
|
return FlutterUnionad.nativeAdView(
|
androidCodeId: pid,
|
//android 信息流广告id 必填
|
iosCodeId: "",
|
//ios banner广告id 必填
|
supportDeepLink: true,
|
//是否支持 DeepLink 选填
|
expressViewWidth: width,
|
// 期望view 宽度 dp 必填
|
expressViewHeight: height,
|
//期望view高度 dp 必填
|
expressNum: 1,
|
mIsExpress: true,
|
//一次请求广告数量 大于1小于3 必填
|
callBack: FlutterUnionadNativeCallBack(
|
onShow: () {
|
print("信息流广告显示");
|
},
|
onFail: (error) {
|
print("信息流广告失败 $error");
|
},
|
onDislike: (message) {
|
print("信息流广告不感兴趣 $message");
|
callback(false, "信息流广告不感兴趣 $message");
|
},
|
onClick: () {
|
print("信息流广告点击");
|
},
|
),
|
);
|
}
|
|
static loadReward(String pid, OnAdCallback adCallback) async {
|
FlutterUnionad.loadRewardVideoAd(
|
mIsExpress: true,
|
//是否个性化 选填
|
androidCodeId: pid,
|
//Android 激励视频广告id 必填
|
iosCodeId: "",
|
//ios 激励视频广告id 必填
|
supportDeepLink: true,
|
//是否支持 DeepLink 选填
|
rewardName: "vip",
|
//奖励名称 选填
|
rewardAmount: 1,
|
//奖励数量 选填
|
userID: "",
|
// 用户id 选填
|
orientation: FlutterUnionadOrientation.VERTICAL,
|
//视屏方向 选填
|
mediaExtra: null, //扩展参数 选填
|
);
|
|
FlutterUnionadStream.initAdStream(
|
//激励广告
|
flutterUnionadRewardAdCallBack: FlutterUnionadRewardAdCallBack(
|
onShow: () {
|
print("激励广告显示");
|
},
|
onClick: () {
|
print("激励广告点击");
|
},
|
onFail: (error) {
|
print("激励广告失败 $error");
|
adCallback(false, "激励广告失败 $error");
|
},
|
onClose: () {
|
print("激励广告关闭");
|
},
|
onSkip: () {
|
print("激励广告跳过");
|
},
|
onVerify: (bool isVerify, int rewardAmount, String rewardName,
|
int errorCode, String message) {
|
adCallback(true, "获取激励成功");
|
},
|
onReady: () async {
|
print("激励广告预加载准备就绪");
|
//显示激励广告
|
await FlutterUnionad.showRewardVideoAd();
|
},
|
onUnReady: () {
|
print("激励广告预加载未准备就绪");
|
},
|
),
|
);
|
}
|
|
static loadInterstitial(String pid) async {
|
FlutterUnionad.loadFullScreenVideoAdInteraction(
|
androidCodeId: pid, //android 全屏广告id 必填
|
iosCodeId: "", //ios 全屏广告id 必填
|
supportDeepLink: true, //是否支持 DeepLink 选填
|
orientation: FlutterUnionadOrientation.VERTICAL, //视屏方向 选填
|
);
|
|
FlutterUnionadStream.initAdStream(
|
// 新模板渲染插屏广告回调
|
flutterUnionadNewInteractionCallBack:
|
FlutterUnionadNewInteractionCallBack(
|
onShow: () {
|
print("新模板渲染插屏广告显示");
|
},
|
onSkip: () {
|
print("新模板渲染插屏广告跳过");
|
},
|
onClick: () {
|
print("新模板渲染插屏广告点击");
|
},
|
onFinish: () {
|
print("新模板渲染插屏广告结束");
|
},
|
onFail: (error) {
|
print("新模板渲染插屏广告错误 $error");
|
},
|
onClose: () {
|
print("新模板渲染插屏广告关闭");
|
},
|
onReady: () async {
|
print("新模板渲染插屏广告预加载准备就绪");
|
//显示新模板渲染插屏
|
await FlutterUnionad.showFullScreenVideoAdInteraction();
|
},
|
onUnReady: () {
|
print("新模板渲染插屏广告预加载未准备就绪");
|
},
|
),
|
);
|
}
|
}
|
|
class GDTAdUtil {
|
static Future init() async {
|
var gdtAppId = await ConfigUtil.getConfig(ConfigKey.gdtAppId);
|
//初始化广点通
|
if (!StringUtil.isNullOrEmpty(gdtAppId)) {
|
await FlutterTencentad.register(
|
androidId: gdtAppId!, //androidId
|
iosId: "", //iosId
|
debug: true, //是否显示日志log
|
);
|
}
|
}
|
|
static loadSplash(String? pid, OnAdCallback adCallback) {
|
if (pid == null) {
|
adCallback(false, "pid为空");
|
return;
|
}
|
FlutterTencentad.splashAdView(
|
//android广告id
|
androidId: pid,
|
//ios广告id
|
iosId: "",
|
////设置开屏广告从请求到展示所花的最大时长(并不是指广告曝光时长),取值范围为[1500, 5000]ms
|
fetchDelay: 3500,
|
//广告回调
|
callBack: FlutterTencentadSplashCallBack(
|
onShow: () {
|
print("开屏广告显示");
|
},
|
onADTick: (time) {
|
print("开屏广告倒计时剩余时间 $time");
|
},
|
onClick: () {
|
print("开屏广告点击");
|
},
|
onClose: () {
|
print("开屏广告关闭");
|
adCallback(true, "");
|
},
|
onExpose: () {
|
print("开屏广告曝光");
|
},
|
onFail: (code, message) {
|
adCallback(true, message);
|
},
|
),
|
);
|
}
|
}
|
|
class AdPosition {
|
//开屏
|
static String splash = "splash";
|
|
//首页插屏
|
static String homeInterstitial = "homeInterstitial";
|
|
//我的页面原生
|
static String mineExpress = "mineExpress";
|
|
//搜索页面原生
|
static String searchExpress = "searchExpress";
|
|
//搜索结果banner
|
static String searchResultBanner = "searchResultBanner";
|
|
//轨迹分享页插屏
|
static String travelShareInterstitial = "travelShareInterstitial";
|
|
//会员激励视频
|
static String vipReward = "vipReward";
|
}
|