flutter_unionad

repository·master·Indexed 18 days ago

https://github.com/gstory0404/flutter_unionad

A Flutter plugin providing direct access to ByteDance's Pangle (穿山甲) advertising SDKs for Android, iOS, and OpenHarmonyOS. It supports null safety and provides widgets and methods for implementing Splash, Banner, Native Feed, Rewarded Video, and Fullscreen/Interstitial ads. The plugin requires the use of GroMore for ad slot creation and supports theme management for day/night modes.

Tokens
17.4K
Snippets
54
Records
64
Agent score
62%

What's inside flutter_unionad

  1. Overview of flutter_unionad

    master
    flutter_unionad is a Flutter plugin that integrates the ByteDance Pangle (穿山甲) Android and iOS SDKs. It allows developers to call Pangle SDK methods directly within Flutter and supports null safety. For managing multiple advertising platforms through a unified interface, it is recommended to use it alongside GTAds.
  2. Important integration notes for Pangle Ads

    master

    Before integrating, please note the following critical requirements:

    • Ad Creation: Use GroMore to create ad slots and use the GroMore ID to display ads.
    • Version Compatibility: Version 2.0.0 introduced significant breaking changes due to incompatibility with the fusion SDK. Always check the plugin update notice after updating.
    • Aggregation: If you need to integrate other advertising SDKs, you must use the Adapter library that matches the version of the current plugin. Refer to the Offline SDK for details.
    • HarmonyOS Specifics: For Splash (开屏), Feed (信息流), and Banner (横幅) ads on HarmonyOS, you must use the code from the ohos branch.
  3. Migration Guide to v2.0.0

    master

    Version 2.0.0 introduces significant breaking changes and new components. Key updates include:

    • SDK Transition: Android and iOS have switched to the Fusion SDK.
    • Privacy Configuration: FlutterUnionad.register now uses androidPrivacy and iosPrivacy for privacy permission configuration. The old FlutterUnionad.andridPrivacy has been removed.
    • Ad View Replacements: Several old ad views are deprecated and will be removed. Use the new versions instead:
      • Use FlutterUnionadBannerView instead of FlutterUnionad.bannerAdView.
      • Use FlutterUnionadDrawFeedAdView instead of FlutterUnionad.drawFeedAdView.
      • Use FlutterUnionadNativeAdView instead of FlutterUnionad.nativeAdView.
      • Use FlutterUnionadSplashAdView instead of FlutterUnionad.splashAdView.
    • Full Screen Video: fullScreenVideoAd has been removed; use the new version of interstitial full-screen ads.
    • ADN Aggregation: Supports ADN aggregation. By default, the plugin only integrates Pangle (穿山甲). For other ADNs, refer to the official documentation.
  4. Use New Template Fullscreen/Interstitial Ads

    master

    These ads are divided into Fullscreen and Interstitial types. They also follow a preload-then-show pattern.

    1. Preload: Call FlutterUnionad.loadFullScreenVideoAdInteraction.
    2. Show: Call FlutterUnionad.showFullScreenVideoAdInteraction.
    3. Listen: Use FlutterUnionad.FlutterUnionadStream.initAdStream with flutterUnionadNewInteractionCallBack to handle lifecycle events like onReady and onFinish.
    // 1. Preload
    await FlutterUnionad.loadFullScreenVideoAdInteraction(
        androidCodeId: "102735530",
        iosCodeId: "102735530",
        orientation: FlutterUnionadOrientation.VERTICAL,
    );
    
    // 2. Listen
    FlutterUnionad.FlutterUnionadStream.initAdStream(
        flutterUnionadNewInteractionCallBack: FlutterUnionadNewInteractionCallBack(
              onShow: () => print("Ad shown"),
              onSkip: () => print("Ad skipped"),
              onClick: () => print("Ad clicked"),
              onFinish: () => print("Ad finished"),
              onFail: (error) => print("Ad error: $error"),
              onClose: () => print("Ad closed"),
              onReady: () async {
                  print("Ad ready, showing...");
                  await FlutterUnionad.showFullScreenVideoAdInteraction();
              },
              onUnReady: () => print("Ad not ready"),
        ),
    );
    
    // 3. Show
    await FlutterUnionad.showFullScreenVideoAdInteraction();
  5. Configure OpenHarmony (HarmonyOS Next) for flutter_unionad

    master

    To integrate with HarmonyOS Next, copy the .ohpmrc file from the plugin's example directory to your HarmonyOS project root.

    # Copy from plugin example to your project root
    cp example/ohos/.ohpmrc ./.ohpmrc
  6. Customize the iOS Launch Screen assets

    master

    To change the image displayed during the app's launch on iOS, you can either replace the image files directly in the example/ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory or use Xcode for a more visual approach.

    Using Xcode:

    1. Open the iOS workspace from your Flutter project root using: open ios/Runner.xcworkspace.
    2. In the Xcode Project Navigator, navigate to Runner/Assets.xcassets.
    3. Drag and drop your desired images into the asset catalog to replace the existing launch screen assets.
    open ios/Runner.xcworkspace
  7. Configure iOS AppDelegate for Splash Ads (v1.3.10+)

    master

    Starting from version 1.3.10, you must modify your AppDelegate in iOS to ensure splash ads (opening ads) are clickable. This involves initializing a UINavigationController and setting up the UIWindow manually. Failure to do this will result in splash ads being unclickable.

    @UIApplicationMain
    @objc class AppDelegate: FlutterAppDelegate {
        var navigationController : UINavigationController? = nil
        override func application(
            _ application: UIApplication,
            didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
        ) -> Bool {
            GeneratedPluginRegistrant.register(with: self)
            let controller = self.window.rootViewController as! FlutterViewController
            self.navigationController = UINavigationController.init(rootViewController: controller)
            self.window =  UIWindow.init(frame: UIScreen.main.bounds)
            self.window.rootViewController = self.navigationController;
            self.navigationController?.setNavigationBarHidden(true, animated: true)
            self.window.makeKeyAndVisible()
            return super.application(application, didFinishLaunchingWithOptions: launchOptions)
        }
    }
  8. Use Rewarded Video Ads

    master

    Rewarded video ads follow a two-step process: preloading and showing. You must listen to the initAdStream to handle the lifecycle and reward verification.

    1. Preload: Call FlutterUnionad.loadRewardVideoAd.
    2. Show: Call FlutterUnionad.showRewardVideoAd.
    3. Listen: Use FlutterUnionad.FlutterUnionadStream.initAdStream with flutterUnionadRewardAdCallBack to handle events like onVerify (for rewards) and onReady (to know when to show the ad).
    // 1. Preload
    await FlutterUnionad.loadRewardVideoAd(
        androidCodeId: "102733764",
        iosCodeId: "102733764",
        rewardName: "200 Gold",
        rewardAmount: 200,
        userID: "123",
        orientation: FlutterUnionadOrientation.VERTICAL,
    );
    
    // 2. Listen for events
    FlutterUnionad.FlutterUnionadStream.initAdStream(
        flutterUnionadRewardAdCallBack: FlutterUnionadRewardAdCallBack(
            onShow: () => print("Ad shown"),
            onClick: () => print("Ad clicked"),
            onFail: (error) => print("Ad failed: $error"),
            onClose: () => print("Ad closed"),
            onSkip: () => print("Ad skipped"),
            onVerify: (rewardVerify, rewardAmount, rewardName) {
                print("Reward: $rewardVerify $rewardAmount $rewardName");
            },
            onReady: () async {
                print("Ad ready, showing now...");
                await FlutterUnionad.showRewardVideoAd();
            },
            onCache: () async {
                print("Cache success. Recommended to show ad here.");
            },
            onUnReady: () => print("Ad not ready"),
            onRewardArrived: (rewardVerify, rewardType, rewardAmount, rewardName, errorCode, error, propose) {
                print("Reward arrived: $rewardVerify $rewardAmount $rewardName");
            },
        ),
    );
    
    // 3. Show
    await FlutterUnionad.showRewardVideoAd();
  9. Configure iOS for flutter_unionad

    master

    The Pangle SDK is pre-configured in the plugin. Because the plugin uses PlatformView, you must add the following keys to your ios/Runner/Info.plist to enable embedded views and allow arbitrary network loads.

    <key>io.flutter.embedded_views_preview</key>
        <true/>
    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
  10. Configure Android Permissions (v1.2.2+)

    master

    Since version 1.2.2, the plugin no longer integrates Android permissions by default. You must manually add the following permissions to your AndroidManifest.xml.

    <!--Necessary Permissions-->
    <uses-permission android:name="android.permission.INTERNET" />
    
    <!--Necessary to solve security risks; required for sending/registering broadcast events-->
    <permission android:name="${applicationId}.openadsdk.permission.TT_PANGOLIN"
            android:protectionLevel="signature" />
    <uses-permission android:name="${applicationId}.openadsdk.permission.TT_PANGOLIN" /> 
    
    <!--Optional Permissions-->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
    <uses-permission android:name="android.permission.GET_TASKS"/>
    
    <!--Location Permissions (Optional, helps with targeted ads)-->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
    <!--Required if video ads use textureView to prevent black screens-->
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    
    <!--Recommended for Android R to improve ad relevance-->
    <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>