If your app already has an app delegate
Keep your own app delegate and still get device tokens, counted taps and banners, by subclassing PushwrenAppDelegate or forwarding three calls.
PushwrenAppDelegate does three things:
- forwards the device token to Pushwren;
- becomes the notification center’s delegate, so taps are counted and appear on
Pushwren.opens; - shows notifications that arrive while the app is open as banners.
You can keep your own delegate in either of two ways.
Subclass it
The easiest way. Override what you need and call super:
final class AppDelegate: PushwrenAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
) -> Bool {
let result = super.application(application, didFinishLaunchingWithOptions: launchOptions)
// Your own launch setup.
return result
}
}
// In your App: @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
Or forward the calls yourself
You still call Pushwren.configure at launch, which asks for the device token for you. The SDK never swizzles, so every forward below matters:
final class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
) -> Bool {
UNUserNotificationCenter.current().delegate = self
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Pushwren.didRegister(deviceToken: deviceToken)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
Pushwren.didFailToRegister(error: error)
}
// Without this, taps aren't counted as opens and never reach Pushwren.opens.
nonisolated func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
Pushwren.didReceive(response: response)
completionHandler()
}
// Optional: show notifications while the app is open. Without it, iOS shows nothing in the foreground.
nonisolated func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
) {
completionHandler([.banner, .list, .sound])
}
}
Only one object can be the notification center’s delegate. If another library sets it after you, Pushwren stops seeing taps.
Opening the right screen
Every tap, including the one that launched the app, arrives on Pushwren.opens:
for await opened in Pushwren.opens {
router.handle(opened.data)
}
opened.data is your push’s data as [String: Any]; opened.notificationID and opened.actionIdentifier come with it. A tap that launches the app is kept until something reads Pushwren.opens, so start listening early, such as in a .task on your root view.