UserContext की मदद से, कई विज्ञापन अनुरोधों को मैनेज करना

IMA SDK के ज़्यादातर इस्तेमाल के लिए, एक समय में सिर्फ़ एक विज्ञापन अनुरोध को मैनेज करना ज़रूरी होता है. हालांकि, कुछ ऐसे मामलों में एक साथ कई अनुरोध करने पड़ सकते हैं. जैसे, उपयोगकर्ता के वीडियो चुनने से पहले विज्ञापन डेटा को पहले से लोड करना. विज्ञापन अनुरोध एसिंक्रोनस रूप से किए जाते हैं, इसलिए यह पक्का करना मुश्किल काम हो सकता है कि सही विज्ञापन मैनेजर का संबंध सही कॉन्टेक्स्ट से जुड़ा हो.

एक से ज़्यादा विज्ञापन मैनेजर के बीच अंतर करने की प्रक्रिया को आसान बनाने के लिए, Android के लिए IMA SDK की मदद से पब्लिशर किसी भी विज्ञापन अनुरोध के UserRequestContext फ़ील्ड में कोई भी वैल्यू या ऑब्जेक्ट पास कर सकते हैं. इसके बाद, getUserRequestContext() तरीके का इस्तेमाल करके, इस वैल्यू या ऑब्जेक्ट को AdsManagerLoadedEvent हैंडलर में वापस लाया जा सकता है.

उदाहरण

...

adsLoader = sdkFactory.createAdsLoader(context, imaSdkSettings, adDisplayContainer);

Map<String, String> userContextA = new HashMap<String, String>();
Map<String, String> userContextB = new HashMap<String, String>();
userContextA.put("id", "Request A");
userContextB.put("id", "Request B");
userContextA.put("element", "videoElementA");
userContextB.put("element", "videoElementB");
adRequestA.setUserRequestContext(userContextA);
adRequestB.setUserRequestContext(userContextB);

adsLoader.addAdsLoadedListener(
    new AdsLoader.AdsLoadedListener() {
      @Override
      public void onAdsManagerLoaded(AdsManagerLoadedEvent adsManagerLoadedEvent) {
        Map<String, String> context = adsManagerLoadedEvent.getUserRequestContext();
        adsManager = adsManagerLoadedEvent.getAdsManager();
        Log.i("ImaExample", "Successfully loaded ID: " + context.get("id"));
      }
    });

adsLoader.addAdErrorListener(
    new AdErrorEvent.AdErrorListener() {
      @Override
      public void onAdError(AdErrorEvent adErrorEvent) {
        Map<String, String> context = adErrorEvent.getUserRequestContext();
        Log.i("ImaExample", "Error with AdRequest. ID: " + context.get("id"));
        Log.i("ImaExample", "Ad Error: " + adErrorEvent.getError().getMessage());
      }
    });

adsLoader.requestAds(adRequestA);
adsLoader.requestAds(adRequestB);

...