> For the complete documentation index, see [llms.txt](https://user.netmera.com/netmera-developer-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://user.netmera.com/netmera-developer-guide/platforms/ios/new-ios-swift/push-notifications/delegate-methods.md).

# Delegate Methods

### Default Handling of Remote Notifications

Netmera automatically handles all `UIApplicationDelegate` methods related to remote notifications. You don’t need to implement these methods in your **App Delegate** unless your app has special use cases that require custom logic.

### Custom Implementation (If Needed)

If your app needs custom handling for remote notification delegate methods, you can implement them in your `UIApplicationDelegate`. You can then add your specific logic inside those methods. For more details on the delegate methods related to push notifications, check the:  [**UI Application Delegate Protocol Reference**](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html#//apple_ref/doc/uid/TP40006786-CH3-SW17)**.**

### Accessing Notification Data

To retrieve the remote notification payload, use the `[Netmera recentPushObject]` or `Netmera.recentPushObject()` method. This method returns a `NetmeraPushObject` instance, which contains the notification data. You can call this method within your `UIApplicationDelegate` methods to access the data of the received notification.

## Push Delegate Methods

Netmera iOS SDK provides delegate protocols to customize and observe push notification behavior.

There are two delegate protocols:

* `NetmeraPushDelegate` → Customize how push notifications are presented and handled
* `NetmeraPushLifecycleDelegate` → Listen to push lifecycle events
* `NetmeraInAppContentDelegate` → Observe lifecycle events for in-app content.

To enable these callbacks, set the delegates in your `AppDelegate`:

```swift
Netmera.setPushDelegate(self)
Netmera.setPushLifecycleDelegate(self)
Netmera.setInAppContentDelegate(self) 
```

***

### NetmeraPushDelegate

{% hint style="warning" %}
**Important:** To use the `NetmeraPushDelegate` method, make sure you are using Swift SDK v4.16.0 or later.
{% endhint %}

`NetmeraPushDelegate` allows you to customize how push notifications are **presented and handled** in your app.

With this delegate you can control:

* Web view presentation
* Foreground notification presentation
* URL opening behavior

```swift
extension AppDelegate: NetmeraPushDelegate {

  /// Returns who will present the in-app web view (or widget) for this push.
  /// Return `.appHandles` only if you implement `presentWebView(for:)`; otherwise return `.sdkHandles`.
  func webViewPresentationDecision(for push: NetmeraBasePush) -> PushDelegateDecision {
    UserDefaults.standard.bool(forKey: NotificationDelegateSetting.webViewHandling.rawValue) ? .appHandles : .sdkHandles
  }

  /// Called when the delegate has indicated it will present the web view.
  /// Present your own web view or widget UI for the given push. Invoked on the main queue.
  func presentWebView(for push: NetmeraBasePush) {
    guard let top = UIApplication.topViewController() else { return }
    let popup = PushContentPopupViewController()
    let nav = UINavigationController(rootViewController: popup)
    nav.modalPresentationStyle = .pageSheet

    if #available(iOS 15.0, *) {
      if let sheet = nav.sheetPresentationController {
        sheet.detents = [.medium(), .large()]
        sheet.prefersGrabberVisible = true
      }
    }

    top.present(nav, animated: true) {
      Netmera.displayContent(for: push, in: popup.webView) {
        DispatchQueue.main.async {
          nav.dismiss(animated: true)
        }
      }
    }
  }

  /// Returns who will present this notification while the app is in the foreground.
  /// Return `.appHandles` only if you implement `presentNotification(for:)`; otherwise return `.sdkHandles`.
  func notificationPresentationDecision(for push: NetmeraBasePush) -> PushDelegateDecision {
    UserDefaults.standard.bool(forKey: NotificationDelegateSetting.presentationHandling.rawValue) ? .appHandles : .sdkHandles
  }

  /// Called when the delegate has indicated it will present the foreground notification.
  /// Implement custom in-foreground behavior (e.g. in-app UI). The system will not show the notification.
  func presentNotification(for push: NetmeraBasePush) {
    print("presentNotification")
    showAlertInIndependentWindow(title: "Presenting Notification", message: push.jsonString)
  }

  /// Returns who will open the given URL in a push context (e.g. link from web view content).
  /// Return `.appHandles` only if you implement `openURL(_:for:)`; otherwise return `.sdkHandles`.
  func urlOpeningDecision(for url: URL, push: NetmeraBasePush) -> PushDelegateDecision {
    if url.host == "your_domain" || UserDefaults.standard.bool(forKey: NotificationDelegateSetting.deeplinkHandling.rawValue) {
      return .appHandles
    }
    return .sdkHandles
  }

  /// Called when the delegate has indicated it will open the URL.
  /// Open the URL in your preferred way (e.g. in-app browser or deeplink).
  func openURL(_ url: URL, for push: NetmeraBasePush) {
    print("openURL \(url)")
    showAlertInIndependentWindow(title: "App handling link", message: url.absoluteString)
  }
}
```

***

### NetmeraPushLifecycleDelegate

{% hint style="warning" %}
**Important:** To use the `NetmeraPushLifecycleDelegate` method, make sure you are using Swift SDK v4.18.0 or later.
{% endhint %}

`NetmeraPushLifecycleDelegate` allows you to observe **push lifecycle events**, such as receiving or opening a push notification.

```swift
extension AppDelegate: NetmeraPushLifecycleDelegate {

  /// Called when the device token has been registered with APNs. Use this to forward the token to your server if needed.
  func didRegisterDeviceToken(_ deviceToken: String) {
    print("Device Token: \(deviceToken)")
  }

  /// Called when the app has received a push payload and converted it to a push object.
  /// Invoked when a remote notification is delivered (e.g. background or app launch) or when a user-visible
  /// notification is about to be presented in the foreground (payload with `content-available` 0 or absent).
  /// Use for analytics, logging, or updating app state when a push arrives.
  public func didReceive(_ push: NetmeraBasePush) {
    print("Received push: \(push)")
  }

  /// Called when the user opened the app from this push (e.g. tapped the notification). Use for analytics or navigation.
  public func didOpen(_ push: NetmeraBasePush) {
    print("Opened push: \(push)")
  }

  /// Called when the user took an action on a push — either by tapping an interactive action button or a carousel item.
  /// - Parameters:
  ///   - action: The action model the user triggered. May be `nil` if the source could not be resolved.
  ///   - source: Where the action originated (interactive button vs. carousel item).
  ///   - push: The push object the action belongs to.
  func didTakeAction(_ action: NetmeraBaseAction?, from source: NetmeraActionSource, for push: NetmeraBasePush) {
    switch source {
    case .interactiveButton:
      print("Did Click Interactive Button with action \(action) from \(source) push: \(push)")
      break
    case .carouselItem:
      print("Did Click Carousel Item with action \(action) from \(source) push: \(push)")
      break
    }
  }
}
```

### NetmeraInAppContentDelegate

{% hint style="warning" %}
**Important:** To use the `NetmeraInAppContentDelegate` method, make sure you are using Swift SDK v4.17.0 or later.
{% endhint %}

This optional delegate is used to observe lifecycle events for in-app content presented by the Netmera SDK.

```swift
extension AppDelegate: NetmeraInAppContentDelegate {

  /// Called when an in-app content is displayed to the user.
  func didShowContent(_ content: NetmeraInApp.Content) {
    // Netmera showed an in-app content
    print("In-app content displayed")
  }

  /// Called when an in-app content is dismissed by the user or removed from the screen.
  func didDismissContent(_ content: NetmeraInApp.Content) {
    // Netmera dismissed an in-app content
    print("In-app content dismissed")
  }
}
```

### **NetmeraInApp.Content Model**

The `NetmeraInApp.Content` enum represents the type of content being handled:

```swift
public enum NetmeraInApp {
  /// Represents a piece of in-app content that can be presented to the user.
  public enum Content {
    /// Web-based content loaded from a URL.
    case web(url: URL)

    /// Web-based content loaded via html template.
    case template

    /// Native banner content.
    case banner(banner: NetmeraBannerPush)
  }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://user.netmera.com/netmera-developer-guide/platforms/ios/new-ios-swift/push-notifications/delegate-methods.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
