Skip to main content

Push Notifications

The SDK uses Firebase Cloud Messaging (FCM) to deliver push notifications. Firebase is bundled - you only need GoogleService-Info.plist in your project, as described in the Installation guide.


Enable push notifications​

Call registerForRemoteNotifications to prompt the contact for permission and register the device with FCM. A good place for this is right after SDK initialization, or triggered by a relevant contact action.

UserSDK.default?.registerForRemoteNotifications(options: [.alert, .badge, .sound])

You can pass any combination of UNAuthorizationOptions. The system shows the permission dialog only if the contact hasn't been asked before.


React to notification taps​

Implement RemoteNotificationDelegate to be notified when a contact taps a push notification. This fires for Positive User banner notifications and for any other FCM notifications your app receives.

extension AppDelegate: RemoteNotificationDelegate {
func didReceiveRemoteNotification(with id: String, data: [AnyHashable: Any]) {
// id - Positive User notification ID (empty string for non-Positive User notifications)
// data - full notification payload
}
}

Register the delegate when calling registerForRemoteNotifications, or assign it directly at any time:

// Pass it during registration
UserSDK.default?.registerForRemoteNotifications(
options: [.alert, .badge, .sound],
notificationDelegate: self
)

// Or assign it directly
UserSDK.default?.notificationDelegate = self

The SDK only intercepts notifications tagged by Positive User. Everything else is forwarded to your delegate untouched.


Display images in push notifications​

To show an image attached to a push notification, you need a Notification Service Extension in your app. The extension runs in the background when a notification arrives and gives you a chance to modify its content before it appears.

1. Add a Notification Service Extension​

In Xcode, go to File → New → Target and choose Notification Service Extension. Name it - for example, YourApp-NotificationService.

2. Add the SDK to the extension target​

In your Package.swift or via File → Add Package Dependencies, make sure UserComSDK is added to the extension target. The extension only handles image downloads, so Firebase and Gifu are not required there - but verify this against your specific setup.

CocoaPods (legacy)

Add the extension target to your Podfile:

target 'YourApp-NotificationService' do
pod 'UserSDK', :git => 'https://github.com/UserEngage/iOS-SDK'
end

3. Implement the extension​

Replace the generated NotificationService.swift with the following:

import UserNotifications
import UserSDK

class NotificationService: UNNotificationServiceExtension {

var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?

override func didReceive(
_ request: UNNotificationRequest,
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void
) {
self.contentHandler = contentHandler
bestAttemptContent = request.content.mutableCopy() as? UNMutableNotificationContent

NotificationExtensionHelper.shared.fillNotificationContent(
bestAttemptContent,
contentHandler: contentHandler
)
}

override func serviceExtensionTimeWillExpire() {
if let contentHandler, let bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}

NotificationExtensionHelper downloads the image from the notification payload and attaches it. If the image can't be fetched in time, the notification appears without it.