📱 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:

  1. Set up APNs
  2. Configure your Xcode project
  3. Install and initialize the NotificationAPI iOS SDK
  4. 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.

  1. Go to your Apple Developer Account
  2. Click “Certificates”
  3. Click “Keys”
  4. Click the ”+” button
  5. Name the Key
  6. Click “Enable” on “Apple Push Notifications Service (APNs)”
  7. Click “Continue”
  8. Click “Register”
  9. Click “Download”
  10. Go to the APNs Provider Configuration
  11. Enter the required information
  12. 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

ParameterTypeDescription
clientId*stringYour NotificationAPI account clientId. You can get it from here.
userId*stringThe 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.

sequenceDiagram participant User participant iOSApp participant "NotificationAPI SDK" participant "Your Backend" participant NotificationAPI participant "Apple (APNs)" User->>iOSApp: Opens App & Logs In iOSApp->>"NotificationAPI SDK": Initializes with Client ID & User ID "NotificationAPI SDK"->>"Apple (APNs)": Requests Push Token "Apple (APNs)"-->>"NotificationAPI SDK": Returns Push Token "NotificationAPI SDK"->>NotificationAPI: Sends User ID and Push Token "Your Backend"->>NotificationAPI: Triggers Notification for User ID NotificationAPI->>"Apple (APNs)": Sends Notification Payload "Apple (APNs)"->>iOSApp: Delivers Push Notification iOSApp->>User: Displays 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.