Docs Add PusherHub to your app
iOS push notifications (Swift): add the SDK step by step
Every step with a picture from a real SwiftUI app: the Firebase package, where the SDK file goes, push notifications in Xcode and the code in AppDelegate.
10 min read
All guides
- Get started with PusherHub
- Create a Firebase project
- Get your Firebase service account file
- Find your Sender ID
- iOS push notifications: add your Apple push key to Firebase
- Add push notifications to your app
- Flutter
- Android (Kotlin)
- Java
- React Native
- iOS (Swift)
- Unity
- Send a test push notification
- Send your first push notification
Download PusherHub.swift
In the dashboard, open
Applications
your app
SDK Integration
and pick iOS (Swift) ①. Click Download PusherHub.swift ②. It goes in a PusherHub folder inside your app's own folder ③.
Every step below is on this tab too, with your own keys already filled in — copy the code from there.
Add Firebase with Swift Package Manager
PusherHub sends to iPhones through Firebase Cloud Messaging, so your app needs Firebase's two packages. In Xcode:
- Choose File Add Package Dependencies… .
- Paste
https://github.com/firebase/firebase-ios-sdkin the search box, and keep Up to Next Major Version from 12.0.0. - Click Add Package, then add FirebaseCore and FirebaseMessaging to your app target.
Firebase then shows under Package Dependencies in the file list (③ in the next picture).
Put the files in your project
GoogleService-Info.plist① — drag it into your app's folder in Xcode and tick your app target.PusherHub.swift② — make a PusherHub folder inside your app's folder and put the file in it. Xcode 16 and later picks it up by itself; in an older project, add it to your app target.- Firebase ③ is there from step 2.
NotificationService④ comes in step 7.
Turn on push notifications
Select your app target and open Signing & Capabilities ①. Choose your paid Apple Developer Team, click + Capability ② and add Push Notifications ④. (The picture's target is called Runner — yours has your app's name.) Background Modes ③ isn't needed for the Swift SDK.
Start PusherHub in AppDelegate.swift
A SwiftUI app doesn't have an app delegate yet: add a new Swift file called AppDelegate.swift with the code from step 4 on your SDK Integration tab. A UIKit app already has one — add these lines to it.
PusherHub.initialize(…)① with your App key, Public key and server. (In the picture they're placeholders.)PusherHub.registerDevice { … }② asks the user to allow notifications, gets the push token and adds the phone to PusherHub.collectDeviceMetadata()inside it is optional: it adds the phone's model, language and country.didRegisterForRemoteNotificationsWithDeviceToken③ hands Apple's push token to PusherHub.didFailToRegisterForRemoteNotificationsWithError④ reports it when Apple refuses one.
Connect it to your app
Open your app's main file (the one with @main, here MyApp.swift):
@UIApplicationDelegateAdaptor① connects theAppDelegatefrom step 5..pusherHubMessageHost()② — optional: shows your in-app messages when the app opens and every time it comes back. Leave it out if you only send notifications.
In a UIKit app, skip the adaptor and call PusherHub.checkForInAppMessages() from your SceneDelegate's sceneDidBecomeActive(_:) instead.
Show pictures in notifications (recommended)
An iPhone doesn't download a notification's image by itself — without this step it shows only the title and text.
-
File
New
Target…
, pick Notification Service Extension, name it
NotificationService, click Finish, then Don't Activate. - On the NotificationService target's General tab, set Minimum Deployments to the same iOS version as your app. Xcode picks the newest one, and an iPhone on anything older silently skips the extension.
- Replace everything in
NotificationService/NotificationService.swiftwith the file on your SDK Integration tab.
It needs no Firebase package, App Group or keys. The simulator can't draw notification pictures, so check this on a real iPhone.
Optional: GPS state and city
PusherHub already estimates each phone's state from its internet address. For GPS instead, add this to your target's Info tab (or Info.plist), then call PusherHub.collectDeviceMetadata(resolveLocation: true) in step 5. The app asks for location once; if the user says no, only state and city are skipped.
Info.plist
<key>NSLocationWhenInUseUsageDescription</key> <string>Used to resolve your approximate state/region so notifications can be targeted correctly.</string>
Optional: log the user in
If your app has its own sign-in, tell PusherHub who's using the phone. Then you can send to one person by your own user ID. It's remembered, even after the app restarts, until logout().
// When someone signs in: PusherHub.login(externalId: currentUserId) // When they sign out: PusherHub.logout()
Optional: open a screen when a notification is tapped
Put your own data in Custom Data when you create a notification — say {"campaign_id": "232"} — and your app gets it back whenever someone taps that notification, whether it was open, in the background or closed. Add onNotificationOpened to the PusherHub.initialize(…) in AppDelegate.swift from step 5, and set something your views watch:
@MainActor final class NotificationRouter: ObservableObject {
static let shared = NotificationRouter()
@Published var campaignId: String?
}
PusherHub.initialize(
appKey: "YOUR_APP_KEY",
publicKey: "YOUR_PUBLIC_KEY",
baseUrl: "YOUR_SERVER",
onNotificationOpened: { data in
NotificationRouter.shared.campaignId = data["campaign_id"]
}
)
Then open the campaign from your views when campaignId changes — a tap that opened a closed app can arrive before your first view is up. Every value arrives as text ("232"). This needs PusherHub.swift from 24 October 2026 or later — download it again if yours is older.
Run the app on an iPhone
Choose a real iPhone at the top of Xcode — the simulator can't get push notifications — and click Run ▶. Tap Allow when it asks about notifications. Within a few seconds the phone shows up in PusherHub under Devices.