This page references our older Objective-C SDK. Check out our new Swift SDK to integrate our latest Swift-first library.
複数のコンテンツカードフィードを使用する
コンテンツカードは、アプリ上で特定のカードだけを表示するようにフィルタリングすることができ、異なるユースケース (トランザクション用フィードとマーケティング用フィードを使用する場合のように) に対して複数のコンテンツカードフィードを持つことが可能となります。
以下のドキュメントは、特定の統合に合わせて変更可能な実装例を示しています。
ステップ1:カードにキーと値のペアを設定する
コンテンツカードキャンペーンを作成する際、各カードにキーと値のペアのデータを設定できます。フィルタリングロジックは、このキーと値のペアデータを使ってカードを分類します。
この例では、どのコンテンツカードフィードにカードを表示するかを指定する feed_type
キーでキーと値のペアを設定します。この値は、Transactional
、Marketing
などのように、カスタムフィードの値になります。
ステップ2:コンテンツカードのリスナーを設定する
次のコードスニペットを使って、コンテンツカードの更新をリッスンするオブザーバーを追加します。
1
2
3
4
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(contentCardsUpdatedNotificationReceived:)
name:ABKContentCardsProcessedNotification
object:nil];
1
2
3
NotificationCenter.default.addObserver(self, selector:
#selector(contentCardsUpdated),
name:NSNotification.Name.ABKContentCardsProcessed, object: nil)
オブザーバーからの更新に応答し、返されたカードをタイプ別にフィルターするために、以下のメソッドを追加します。
最初のメソッド contentCardsUpdatedNotificationReceived:
は、オブザーバーからの更新を処理します。2番目のメソッド getCardsForFeedType:
を希望するフィードタイプ (この場合、Transactional
) で呼び出します。
```objc - (void)contentCardsUpdatedNotificationReceived:(NSNotification *)notification { BOOL updateIsSuccessful = [notification.userInfo[ABKContentCardsProcessedIsSuccessfulKey] boolValue]; if (updateIsSuccessful) { // エクストラに “Transactional” フィードタイプが設定されているカードのみを含む配列を取得する。 NSArray<ABKContentCard *> *filteredArray = [self getCardsForFeedType:@”Transactional”]; NSLog(@”Got filtered array of length: %lu”, [filteredArray count]);
1
// Pass filteredArray to your UI layer for display. } }
-
(NSArray<ABKContentCard *> *)getCardsForFeedType:(NSString *)type { NSArray<ABKContentCard *> *cards = [Appboy.sharedInstance.contentCardsController getContentCards];
NSArray<ABKContentCard *> *filteredArray = [cards filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(ABKContentCard * card, NSDictionary *bindings) { NSDictionary *extras = [card extras]; if (extras != nil && [extras objectForKey:@”feed_type”] != nil && [[extras objectForKey:@”feed_type”] isEqualToString:type]) { NSLog(@”Got card: %@ “, card.idString); return YES; } return NO; }]];
return filteredArray; } ```
```swift @objc private func contentCardsUpdatedNotificationReceived(notification:NSNotification) { guard let updateSuccessful = notification.userInfo?[ABKContentCardsProcessedIsSuccessfulKey] as?Bool else { return } if updateSuccessful { // エクストラに “Transactional” フィードタイプが設定されているカードのみを含む配列を取得する。 let filteredArray = getCards(forFeedType:”Transactional”) NSLog(“Got filtered array of length: %@”,filteredArray?.count ??0)
1
2
// Pass filteredArray to your UI layer for display.
} }
func getCards(forFeedType type:String) -> [ABKContentCard]? { guard let allCards = Appboy.sharedInstance()?.contentCardsController.contentCards as? [ABKContentCard] else { return nil } // フィルタリングされたカードを返す return allCards.filter { if $0.extras?[“feed_type”] as? String == type { NSLog(“%@”,”Got card: ($0.idString)”) return true } else { return false } } } ```