Overview

Skill Level: Any

On iOS, there can be multiple sources of push. For instance, the Acoustic SDK may send push notifications from your marketing team, but another party might send pushes to devices when files are updated. Unlike Android, all these push sources ultimately use the same push token when they register. If the other push source integrates manually with the app, integration should be relatively straightforward. SDKs which integrate by method swizzling can be more problematic and aren't addressed here.

Prerequisites

Not what you’re looking for? Check out all our available tutorials for mobile app messaging here.

Step-by-step

  1. Create a new NotificationDelegate class in the main target (SwiftSample in the sample app):

    class NotificationDelegate: NSObject, UNUserNotificationCenterDelegate {
        static let shared = NotificationDelegate()
    
        // This method processes the notification tap
        func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
            guard response.notification.request.content.userInfo["notification-action"] == nil else {
                MCENotificationDelegate.shared.userNotificationCenter(center, didReceive: response, withCompletionHandler: completionHandler)
                return
            }
            // handle other types of notifications here
        }
    
        // This method is used to determine if the notification should be shown to the user when the app is running
        func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
            guard notification.request.content.userInfo["notification-action"] == nil else {
                MCENotificationDelegate.shared.userNotificationCenter(center, willPresent: notification, withCompletionHandler: completionHandler)
                return
            }
    
            // handle other types of notifications here, typically via
            completionHandler([.alert, .sound, .badge])
        }
    
        // This method is used to open the settings screen in the app for push notification preferences, only implement if you provide one
        func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification?) {
            // show settings screen if available
        }
    }
    
  2. Update the NotificationDelegate class

    Update the NotificationDelegate class to pass control to other classes as appropriate when different (non-Acoustic) push messages are received.

  3. Update your app delegate

    Update your app delegate (SwiftSample.AppDelegate in the sample app) to make use of the NotificationDelegate class you just added by updating the UNUserNotificationCenter.current().delegate when the app finishes launching:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        UNUserNotificationCenter.current().delegate = NotificationDelegate.shared
    }
    

Expected outcome

Need more help? Check out all of our available tutorials for mobile app messaging here.

Join The Discussion

Your email address will not be published. Required fields are marked *