iOS Push Notifications

How to set up WonderPush push notifications on iOS apps for iphone and ipad devices.

Easy setup Apple push notifications for iOS apps

Setting up push notifications for your iOS app is easy. Push notifications are the ideal solution to re-engage users and bring them back to your app. This guide tells you how to configure an iOS app to send and receive push notifications on any Apple mobile device, whether it's an iphone, an ipad, or an Apple watch.

Estimated setup time: 15 minutes.

📘

Using a cross-platform framework?

We have specific instructions for Cordova, React Native and Flutter.

Prerequisites

Getting started with WonderPush is easy.

You'll need:

  • Xcode
  • An iOS device such as an iPhone or an iPad
  • An iOS Push Certificate (here's a how to obtain it)

If you haven't already, sign up for a free account on wonderpush.com.

📘

This guide uses Swift Package Manager, which is our preferred integration method. This requires Xcode version 13 or later. We also support Cocoapods, manual Framework integration and Carthage

Note: if you're upgrading from our SDK version 3.x? Follow this guide to update your integration.

Step 1. Create your project

Click on New Project:

Choose a name for your project and select iOS as a platform then click Create:

📘

Already have a project?

Just add the iOS platform to any existing project by going to Settings, selecting the first tab named after your project and clicking Edit. You'll be presented a form that lets you add a platform.

Step 2. Upload your push certificate

Get your push certificate handy. If you don't have one, here's how to obtain it. In a few words, head to the Apple developer website, go to Accounts, then Certificates, Identifiers & Profiles and click the plus button to add a new certificate. Choose the type Apple Push Notification service SSL (Sandbox & Production):

Then select your app ID (be sure not to select the app ID for the notification service extension):

Follow the rest of the wizard to download your certificate, and open it so it shows in Keychain Access.

We now have to properly export it along its private key before uploading them in the WonderPush dashboard.

  1. Launch Keychain Access on the Mac where you created the certificate request.
  2. In the Category section, select My Certificates.
  3. Find the newly created certificate and expand its content by clicking on the triangle icon.
    You’ll see both a certificate and a private key.
  1. Select both the certificate and the key, and choose File > Export Items.
  2. From the File Format pop-up menu, ensure P12 is selected.
  3. Enter a filename in the Save As field, then click Save.
    The certificate and key are saved to the location you specified as a .p12 file.

Now go to Settings / Platforms, click the iOS application section title and fill in:

  • your push certificate by clicking the Browse button,
  • the push certificate password if appropriate.

📘

Be sure to check how to use WonderPush in both Sandbox & Production.

Step 3. Create the notification service extension

🚧

If you're using React Native, Flutter, Cordova or Ionic, make sure to follow the steps specific to your platform:

In Xcode, select File / New / Target..., and choose Notification Service Extension:

Enter WonderPushNotificationServiceExtension as the name for your new target:

When Xcode prompts you to activate the new scheme, answer Cancel to keep Xcode building and debugging your app instead of the extension:

If you activate by mistake, switch back to your app's scheme from the dropdown menu located next to the play button.

Set the Deployment Target of your Notification Service Extension to 10.0:

Make sure to use Automatic Signing for the extension target:

📘

If you're getting the following error, make sure to set up Automatic Signing for the extension:

Provisioning profile "XXX" has app ID "YYY", which does not match the bundle ID "YYY.WonderPushNotificationServiceExtension".

Step 4. Integrate WonderPush to your Xcode project

In your project settings, find the tab "Package Dependencies" and click the "+" button to add a package dependency.

In the search field, type the URL of our github repository:

https://github.com/wonderpush/wonderpush-ios-sdk

Add the package to your application, make sure the dependency rule is "Up to Next Major Version" and "4.0.0" then click "Add package".

Finally, add the WonderPush product to your app, and the WonderPushExtension product to the WonderPushNotificationServiceExtension.

You should see the package dependency in the left pane of your Xcode project:

Step 5. Add device capabilities

Select your app's main target and, under the Capabilities tab, make sure:

  • the Background Modes capability is ON,
  • the Remote notifications background mode is checked,
  • the Push Notifications capability is ON.

Step 6. Add required code

Take note of your Client ID and Client Secret from the iOS app section of the Settings / Platforms page:

Copy your credentials to enable push notifications in your iOS applicationCopy your credentials to enable push notifications in your iOS application

Copy your credentials to enable push notifications in your iOS application

Open your AppDelegate and add the following, adapting YOUR_CLIENT_ID and YOUR_CLIENT_SECRET with the values you've noted above:

import WonderPush

func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        WonderPush.setClientId("YOUR_CLIENT_ID", secret: "YOUR_CLIENT_SECRET")
        WonderPush.setupDelegate(for: application)
        WonderPush.setupDelegateForUserNotificationCenter()
        return true
}
#import <WonderPush/WonderPush.h>
  
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [WonderPush setClientId:@"YOUR_CLIENT_ID" secret:@"YOUR_CLIENT_SECRET"];
    [WonderPush setupDelegateForApplication:application];
    [WonderPush setupDelegateForUserNotificationCenter];
    return YES;
}

Let's add the code that will prompt users to subscribe to push notifications. We recommend you place this code in a more suitable place in the user journey, but we'll do it at launch time for now. In your AppDelegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    // Prompts users to subscribe to push notifications.
    // Move this in a more suitable place in the user journey.
    WonderPush.subscribeToNotifications()
    return true
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    // Prompts users to subscribe to push notifications.
    // Move this in a more suitable place in the user journey.
    [WonderPush subscribeToNotifications];
    return YES;
}

Finally, we'll modify the code of the Notification Service Extension you created in Step 3. Replace the whole contents of NotificationService.swift or (NotificationService.h and NotificationService.m for Objective-C) with :

import WonderPushExtension

class NotificationService: WPNotificationServiceExtension {

    override class func clientId() -> String {
        return "YOUR_CLIENT_ID"
    }

    override class func clientSecret() -> String {
        return "YOUR_CLIENT_SECRET"
    }

}
#import <WonderPushExtension/WonderPushExtension.h>

@interface NotificationService : WPNotificationServiceExtension

@end
#import "NotificationService.h"

@implementation NotificationService

+ (NSString *)clientId {
    return @"YOUR_CLIENT_ID";
}

+ (NSString *)clientSecret {
    return @"YOUR_CLIENT_SECRET";
}

@end

Adapt YOUR_CLIENT_ID and YOUR_CLIENT_SECRET with the values you've noted above, the same you just gave to your AppDelegate.

Receive your first push!

Let take your iphone or your ipad, and discover your first iOS Push notification.

📘

To test push notifications, you'll need to use a real device as the simulator doesn't support push notifications.

Build & run your application and get the permission prompt you've configured:
Permission prompt for native push notifications on iphonePermission prompt for native push notifications on iphone

Wait a couple of minutes and receive the default welcome notification:

A welcome push notification on iOSA welcome push notification on iOS

👍

Congratulations, you're done!