fetchAdvertisementId static method

Future<Advertisement?> fetchAdvertisementId()

Implementation

static Future<Advertisement?> fetchAdvertisementId() async{
  Advertisement advertisement = Advertisement();
  // Platform messages may fail, so we use a try/catch PlatformException.

  if(defaultTargetPlatform == TargetPlatform.android) {
    try {
      advertisement.advertisingId = await AdvertisingId.id(true);
    } on PlatformException {
      advertisement.advertisingId = 'Failed to get platform version.';
    }
    try {
      advertisement.isLimitAdTrackingEnabled = await AdvertisingId.isLimitAdTrackingEnabled;
    } on PlatformException {
      advertisement.isLimitAdTrackingEnabled = false;
    }
  }else if(defaultTargetPlatform == TargetPlatform.iOS){
    final TrackingStatus trackingStatus = await AppTrackingTransparency.requestTrackingAuthorization();
    if (trackingStatus == TrackingStatus.authorized) {
      final adId = await AppTrackingTransparency.getAdvertisingIdentifier();
      // Logger("AdRequest").log("adId ${adId}");
      advertisement.advertisingId = adId;
      advertisement.isLimitAdTrackingEnabled = true;
    } else {
      var deviceInfo = DeviceInfoPlugin();
      var iosDeviceInfo = await deviceInfo.iosInfo;
      // Logger("AdRequest").log("uuid ${iosDeviceInfo.identifierForVendor!}");
      advertisement.advertisingId = iosDeviceInfo.identifierForVendor!;
      advertisement.isLimitAdTrackingEnabled = false;
    }

  }else if(kIsWeb){
    // advertisement.advertisingId = "5432";
    advertisement.advertisingId = uuid.createUuidGetter().getUuid();
  }

  return advertisement;
}