Skip to content

Commit

Permalink
跑起来
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangruiyu committed Dec 12, 2024
1 parent eaa2cc6 commit ab37677
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 83 deletions.
4 changes: 2 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ class _IndexPageState extends State<IndexPage> {
androidAppId: "5098580",
//穿山甲广告 ios appid 必填
iosAppId: "5098580",
ohosAppId: "5638354",
//appname 必填
appName: "unionad_test",
//使用聚合功能一定要打开此开关,否则不会请求聚合广告,默认这个值为false
Expand Down Expand Up @@ -221,9 +222,7 @@ class _IndexPageState extends State<IndexPage> {
));
print("sdk初始化 $_init");
_version = await FlutterUnionad.getSDKVersion();
print("sdk初始化 1");
_themeStatus = await FlutterUnionad.getThemeStatus();
print("sdk初始化 12");
setState(() {});
}

Expand Down Expand Up @@ -347,6 +346,7 @@ class _IndexPageState extends State<IndexPage> {
androidCodeId: "102733764",
//Android 激励视频广告id 必填
iosCodeId: "102733764",
ohosCodeId: '962519282',
//ios 激励视频广告id 必填
rewardName: "200金币",
//奖励名称 选填
Expand Down
12 changes: 0 additions & 12 deletions example/ohos/build-profile.json5
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,6 @@
]
}
]
},
{
"name": "flutter_unionad",
"srcPath": "../../ohos",
"targets": [
{
"name": "default",
"applyToProducts": [
"default"
]
}
]
}
]
}
2 changes: 1 addition & 1 deletion example/ohos/oh-package.json5
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"overrides": {
"@ohos/flutter_ohos": "file:./har/flutter.har",
"flutter_unionad": "../../ohos",
"flutter_unionad": "file:./har/flutter_unionad.har",
"@ohos/flutter_module": "file:./entry"
}
}
4 changes: 4 additions & 0 deletions lib/flutter_unionad.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class FlutterUnionad {
static Future<bool> register({
required String iosAppId,
required String androidAppId,
String? ohosAppId,
String? appName,
bool? useMediation,
bool? paid,
Expand All @@ -75,6 +76,7 @@ class FlutterUnionad {
return await _channel.invokeMethod("register", {
"iosAppId": iosAppId,
"androidAppId": androidAppId,
"ohosAppId": ohosAppId,
"appName": appName ?? "",
"paid": paid ?? false,
"useMediation": useMediation ?? true,
Expand Down Expand Up @@ -274,6 +276,7 @@ class FlutterUnionad {
static Future<bool> loadRewardVideoAd({
required String androidCodeId,
required String iosCodeId,
String? ohosCodeId,
required String rewardName,
required int rewardAmount,
required String userID,
Expand All @@ -283,6 +286,7 @@ class FlutterUnionad {
return await _channel.invokeMethod("loadRewardVideoAd", {
"androidCodeId": androidCodeId,
"iosCodeId": iosCodeId,
"ohosCodeId": ohosCodeId,
"rewardName": rewardName,
"rewardAmount": rewardAmount,
"userID": userID,
Expand Down
173 changes: 105 additions & 68 deletions ohos/src/main/ets/components/plugin/FlutterUnionadPlugin.ets
Original file line number Diff line number Diff line change
@@ -1,86 +1,123 @@
import {
FlutterPlugin,
FlutterPluginBinding,
MethodCall,
MethodCallHandler,
MethodChannel,
MethodResult,
AbilityAware,
AbilityPluginBinding,
FlutterPlugin,
FlutterPluginBinding,
MethodCall,
MethodCallHandler,
MethodChannel,
MethodResult,
} from '@ohos/flutter_ohos';
import {
AdLoadType,
AdSlotBuilder, CSJAdSdk,
CSJRewardAd,
RewardAdLoadListener, SDKConfig,
SDKConfigBuilder
AdLoadType,
AdSlotBuilder,
CSJAdSdk,
CSJRewardAd,
RewardAdInteractionListener,
RewardAdLoadListener,
SDKConfig,
SDKConfigBuilder
} from '@csj/openadsdk';
import { UIAbility } from '@kit.AbilityKit';

/** FlutterUnionadPlugin **/
export default class FlutterUnionadPlugin implements FlutterPlugin, MethodCallHandler {
private channel: MethodChannel | null = null;
private applicationContext: Context | null = null;
private adCreator = CSJAdSdk.getAdCreator();
constructor() {
}
export default class FlutterUnionadPlugin implements FlutterPlugin, MethodCallHandler, AbilityAware {
private channel: MethodChannel | null = null;
private applicationContext: Context | null = null;
private uiAbility: UIAbility | null = null;
private adCreator = CSJAdSdk.getAdCreator();

getUniqueClassName(): string {
return "FlutterUnionadPlugin"
}
constructor() {
}

onAttachedToEngine(binding: FlutterPluginBinding): void {
this.channel = new MethodChannel(binding.getBinaryMessenger(), "flutter_unionad");
this.channel.setMethodCallHandler(this)
this.applicationContext = binding.getApplicationContext();
}
getUniqueClassName(): string {
return "FlutterUnionadPlugin"
}

onDetachedFromEngine(binding: FlutterPluginBinding): void {
if (this.channel != null) {
this.channel.setMethodCallHandler(null)
}
onAttachedToEngine(binding: FlutterPluginBinding): void {
this.channel = new MethodChannel(binding.getBinaryMessenger(), "flutter_unionad");
this.channel.setMethodCallHandler(this)
this.applicationContext = binding.getApplicationContext();
}

onDetachedFromEngine(binding: FlutterPluginBinding): void {
if (this.channel != null) {
this.channel.setMethodCallHandler(null)
}
}

onMethodCall(call: MethodCall, result: MethodResult): void {
if (call.method == "register") {
onAttachedToAbility(binding: AbilityPluginBinding): void {
this.uiAbility = binding.getAbility()
}

// 创建SDKConfig对象
let adConfigBuilder = new SDKConfigBuilder()
let config: SDKConfig = adConfigBuilder
.appId("5638354")
.appName("穿山甲Demo")
.allowShowNotify(true)
.debug(true)
.build()
onDetachedFromAbility(): void {
this.uiAbility = null
}

// 初始化SDK
CSJAdSdk.init(this.applicationContext, config) //context类型必须为UIAbility,否则可能存在展示异常场景
onMethodCall(call: MethodCall, result: MethodResult): void {
if (call.method == "register") {
let ohosAppId: string = call.args.get("ohosAppId");
let appName: string = call.args.get("appName");
let debug: boolean = call.args.get("debug");
// 创建SDKConfig对象
let adConfigBuilder = new SDKConfigBuilder()
let config: SDKConfig = adConfigBuilder
.appId(ohosAppId)
.appName(appName)
.allowShowNotify(true)
.debug(debug)
.build()

// 启动SDK
CSJAdSdk.start();
result.success(true)
} else if (call.method == "getSDKVersion") {
result.success(CSJAdSdk.getSDKVersion())
} else if (call.method == "getThemeStatus") {
result.success(0)
} else if (call.method == "loadRewardVideoAd") {
// 初始化SDK
CSJAdSdk.init(this.applicationContext, config) //context类型必须为UIAbility,否则可能存在展示异常场景

let adSlot = new AdSlotBuilder()
.setCodeId("codeId")
.setAdLoadType(AdLoadType.LOAD)
// .setMediaExtra(mediaExtra)
.build()
let mLoadListener: RewardAdLoadListener = {
onAdLoaded: (rewardAd: CSJRewardAd) => {
// 广告基础信息加载完成
},
onAdCached: (rewardAd: CSJRewardAd) => {
// 广告基础信息与素材缓存完成
},
onError: (code: number, message: string) => {
}
}
this.adCreator.loadRewardAd(adSlot, mLoadListener)
result.success(true)
} else {
result.notImplemented()
// 启动SDK
CSJAdSdk.start();
result.success(true)
} else if (call.method == "getSDKVersion") {
result.success(CSJAdSdk.getSDKVersion())
} else if (call.method == "getThemeStatus") {
//todo 没做
result.success(0)
} else if (call.method == "loadRewardVideoAd") {
let ohosCodeId: string = call.args.get("ohosCodeId");
let adSlot = new AdSlotBuilder()
.setCodeId(ohosCodeId)
.setAdLoadType(AdLoadType.LOAD)// .setMediaExtra(mediaExtra)
.build()
let adInteractionListener: RewardAdInteractionListener = {
onShow: () => {
},
onSkip: () => {
},
onClick: () => {
},
onComplete: () => {
},
onClose: () => {
},
onRewardArrived: (isVerify, rewardType, extraInfo) => {
}
}
let mLoadListener: RewardAdLoadListener = {
onAdLoaded: (rewardAd: CSJRewardAd) => {
// 广告基础信息加载完成
if (this.uiAbility != null) {
rewardAd.setInteractionListener(adInteractionListener)
rewardAd.showRewardAd(this.uiAbility.context.windowStage)
}
},
onAdCached: (rewardAd: CSJRewardAd) => {
// 广告基础信息与素材缓存完成
},
onError: (code: number, message: string) => {
console.log("onError,code:"+code+",message:"+message)
}
}
this.adCreator.loadRewardAd(adSlot, mLoadListener)
result.success(true)
} else {
result.notImplemented()
}
}
}

0 comments on commit ab37677

Please sign in to comment.