📱 Send Mobile Push Notifications with iOS
This guide will walk you through sending push notifications to your iOS application using NotificationAPI and Apple Push Notification service (APNs).
Overview
Sending push notifications to iOS devices is exclusively handled by Apple’s APNs. Implementing this from scratch requires significant backend infrastructure to manage device tokens and communicate with APNs.
NotificationAPI simplifies this process by managing the entire backend complexity for you. Our iOS SDK handles device token registration, and our service sends the notifications through APNs on your behalf.
The setup process is as follows:
- Set up APNs
- Configure your Xcode project
- Install and initialize the NotificationAPI iOS SDK
- Send a notification
1. Set up APNs
To send a push notification to your mobile app on an Apple device, we need some credentials from your Apple Developer account. Below are the required steps to generate and save APNs credentials.
- Go to your Apple Developer Account
- Click “Certificates”
- Click “Keys”
- Click the ”+” button
- Name the Key
- Click “Enable” on “Apple Push Notifications Service (APNs)”
- Click “Continue”
- Click “Register”
- Click “Download”
- Go to the APNs Provider Configuration
- Enter the required information
- Click “Save”
2. Configure your Xcode project
In your Xcode project settings, go to “Signing & Capabilities”, and click ”+ Capability” to add the “Push Notifications” capability.
3. Install and initialize the NotificationAPI iOS SDK
Our iOS SDK makes it easy to register the device for push notifications.
You can follow Apple’s instructions for installing a package dependency here.
The URL is https://github.com/notificationapi-com/notificationapi-ios-sdk.git
Then, initialize the SDK in your AppDelegate.swift
file when your app starts. We highly recommend to extend from NotificationApiAppDelegate.swift
and use that class as your app’s delegate.
import NotificationAPI
class AppDelegate: NotificationApiAppDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
NotificationApi.shared.configure(withCredentials: NotificationApiCredentials(clientId: "YOUR_CLIENT_ID", userId: "YOUR_USER_ID"))
NotificationApi.shared.requestAuthorization { granted, _ in
print("Is authorized?: \(granted)")
}
return true
}
}
This will automatically handle requesting push permissions and registering the device token with NotificationAPI.
Parameters
Parameter | Type | Description |
---|---|---|
clientId* | string | Your NotificationAPI account clientId. You can get it from here. |
userId* | string | The unique ID of the user in your system. |
4. Send a notification
With the SDK initialized, you can now trigger notifications from your backend using something like cURL, or one of our backend SDKs. The userId
must match the one used to initialize the SDK.
Schematic Diagram
This diagram illustrates the entire flow, from app initialization to receiving a notification.
Frequently Asked Questions (FAQs)
Why are my notifications not arriving?
There can be several reasons:
- APNs Setup: Double-check that your APNs key is correctly uploaded to the NotificationAPI dashboard.
- User ID Mismatch: Ensure the
userId
in your iOS app is identical to the one you’re sending to in the backend API call. - Device/Emulator Issues: Ensure your device is connected to the internet. Push notifications do not work on simulators.
- App State: On some iOS versions, notification delivery behavior can change if the app is in the foreground, background, or terminated. Check Apple’s documentation for details.