Fetch and Display Inbox

This tutorial explains how to fetch and display an Inbox in your application.

Pre-requisites

Integrate and register the AEPMessaging extension in your app.

Fetch Inbox Content

To fetch the inbox content for the surfaces configured in Adobe Journey Optimizer campaigns, call the updatePropositionsForSurfaces API. You should batch requests for multiple surfaces in a single API call when possible.

data-slots=heading, code
data-repeat=1
data-languages=Swift

Swift

let inboxSurface = Surface(path: "inbox")
Messaging.updatePropositionsForSurfaces([inboxSurface])

Display Inbox

To display an Inbox, call getInboxUI with your configured surface. This API returns an InboxUI instance immediately, which manages its own state transitions — loading, loaded, and error — automatically.

data-variant=info
data-slots=text
Call updatePropositionsForSurfaces before or after calling getInboxUI. The Inbox will begin displaying content once the fetch completes.

SwiftUI Application

Use the InboxUI.view property to embed the Inbox directly into your SwiftUI view hierarchy.

data-slots=heading, code
data-repeat=1
data-languages=Swift

Swift

import SwiftUI
import AEPMessaging

struct InboxPage: View {

    let inboxUI: InboxUI

    init() {
        let inboxSurface = Surface(path: "inbox")
        inboxUI = Messaging.getInboxUI(for: inboxSurface)
    }

    var body: some View {
        NavigationView {
            inboxUI.view
                .navigationTitle("Inbox")
        }
        .onAppear {
            let inboxSurface = Surface(path: "inbox")
            Messaging.updatePropositionsForSurfaces([inboxSurface])
        }
    }
}

UIKit Application

To display an Inbox in UIKit, wrap the SwiftUI InboxUI.view using UIHostingController.

data-slots=heading, code
data-repeat=1
data-languages=Swift

Swift

import UIKit
import SwiftUI
import AEPMessaging

class InboxViewController: UIViewController {

    var inboxUI: InboxUI!

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "Inbox"

        let inboxSurface = Surface(path: "inbox")
        inboxUI = Messaging.getInboxUI(for: inboxSurface)

        let hostingController = UIHostingController(rootView: inboxUI.view)

        addChild(hostingController)
        view.addSubview(hostingController.view)
        hostingController.didMove(toParent: self)

        hostingController.view.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            hostingController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            hostingController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            hostingController.view.topAnchor.constraint(equalTo: view.topAnchor),
            hostingController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])

        Messaging.updatePropositionsForSurfaces([inboxSurface])
    }
}

Refreshing Inbox Data

The Inbox provides two ways to refresh content.

Pull-to-Refresh

Enable pull-to-refresh to let users refresh content by pulling down on the Inbox.

data-slots=heading, code
data-repeat=1
data-languages=Swift

Swift

let inboxSurface = Surface(path: "inbox")
let inboxUI = Messaging.getInboxUI(for: inboxSurface)

inboxUI.isPullToRefreshEnabled = true
data-variant=info
data-slots=text
Pull-to-refresh is disabled by default. Set isPullToRefreshEnabled = true to enable it.

Programmatic Refresh

Call refresh() to programmatically trigger a content refresh.

data-slots=heading, code
data-repeat=1
data-languages=Swift

Swift

inboxUI.refresh()

Best Practices

  1. Fetch Early: Call updatePropositionsForSurfaces when your app launches or when the user navigates to the Inbox screen to ensure fresh content is available.

  2. Surface Naming: Use descriptive surface paths that match your Adobe Journey Optimizer campaign configuration (e.g., Surface(path: "inbox"), Surface(path: "home_feed")).

  3. Reuse InboxUI: Keep the InboxUI instance alive as long as the Inbox view is visible. The InboxUI maintains state and efficiently updates when content changes.

  4. Handle Multiple Surfaces: If your app has multiple Inboxes, create separate InboxUI instances with different surfaces.

data-slots=heading, code
data-repeat=1
data-languages=Swift

Swift

let notificationsInboxUI = Messaging.getInboxUI(for: Surface(path: "notifications"))
let promotionsInboxUI = Messaging.getInboxUI(for: Surface(path: "promotions"))

Next Steps