To send and receive push notifications via the iOS Mobile SDK, you need to configure Apple Push Notifications Service (APNs). This article explains the steps required for the configuration.
1. Create and configure the APNs Key
You may already have an existing Key; if so, you can skip these steps and go directly to configuring the integration.
From your Apple Developer account, go to the Keys section and click the + symbol to add a new one.
When creating the key, you will need to enable the Apple Push Notification Service (APNs) capability and click Continue.
The next step will ask for confirmation before registering the key.
Once created, download the ".p8" file, which you will need later to configure iOS Push Notifications in Connectif.
Remember, you can only download this file once.
2. Configure the integration in Connectif
In the configuration section of the Mobile App channel in Connectif, go to the iOS Push section. Upload the ".p8" file you downloaded in step 1.
Once the file is uploaded, the interface will look like this:
Select between the two Host types, Production or Development, and configure them according to your needs.
The parameters Team ID and Key ID can be found on your Apple Developer page in the Key section:
- In the top-right corner, you will find your Team ID and, next to the Key, the Key ID.
Next, configure the Bundle identifier, which you can find in the general information of your project in Xcode in the main target.
Once these parameters are configured in Connectif, the integration will look like this:
3. Add Push Notification capability
If your project does not have Push Notifications capability enabled, you will need to add it.
Click on your project, go to the Target, and in the Signing & Capabilities section, click on + Capability.
A dialog will appear, allowing you to search for and add the Push Notifications capability.
4. Add Background Modes capability with Remote Notifications
Follow the same process you used to add the Push Notification capability to add the Background Modes capability.
Once added, ensure that the Remote Notifications option is enabled in the configuration.
5. Add Notification Service Extension
To properly enrich and track your Push Notifications, you need to create a Notification Service Extension.
Click the add target button and search for Notification Service Extension. A possible name for the extension could be ConnectifNotificationServiceExtension.
Remember that if your main project has a specific "Minimum Deployments," this extension should have the same.
6. Add App Group
The App Group capability allows you to share data between your main target and your notification extension.
Just like you added the Push Notification and Background Modes capabilities, add an App Group to your app's main target.
Once the App Groups capability is added, you will need to add a container in its configuration. Use the format group + bundle identifier + connectif. It will look like this example:
Repeat this process and add it to the Notification Service Extension you created earlier, using the same container name.
7. Code modifications
7.1. SDK Initialization
When initializing the SDK, add the appGroup parameter with the name of the container you created in the previous step.
Connectif.initialize(apiKey: "YOUR_API_KEY",
appGroup: "YOUR_CONTAINER_APPGROUP_NAME")
7.2. Modifications in AppDelegate
If you want to use our AppDelegate extension directly, you can extend ConnectifAppDelegate, without forgetting to call super.application.
class AppDelegate: ConnectifAppDelegate {
override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
super.application(
application,
didFinishLaunchingWithOptions: launchOptions
)
Connectif.initialize(apiKey: "YOUR_API_KEY",
appGroup: "YOUR_CONTAINER_APPGROUP_NAME")
return true
}
}
If you want to modify the functions in your AppDelegate directly, add the following:
-
didRegisterForRemoteNotificationsWithDeviceToken
Connectif.addPushToken(deviceToken: deviceToken)
-
didFinishLaunchingWithOptions
UNUserNotificationCenter.current().delegate = self
-
didReceiveRemoteNotification
completionHandler(.newData)
- userNotificationCenter(_: UNUserNotificationCenter, willPresent ...
completionHandler([.alert, .sound, .badge])
- userNotificationCenter( _: UNUserNotificationCenter, didReceive ...
Connectif.handleNotificationUserAction will return a boolean indicating if the push was handled by Connectif, allowing you to manage pushes from other providers.
Connectif.handleNotificationUserAction(
actionIdentifier: response.actionIdentifier,
userInfo: response.notification.request.content.userInfo)
completionHandler()
7.3. Modifications in Notification Service Extension
override func didReceive(
_ request: UNNotificationRequest,
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void
) {
self.contentHandler = contentHandler
guard
Connectif.handleNotification(
appGroup: "YOUR_APP_GROUP", request: request,
contentHandler: contentHandler)
else {
//Notification not handled by Connectif, handle other notifications here
contentHandler(request.content)
return
}
}
8. Notification Permissions
Remember that in order to receive push notifications, users must be asked for permission, and they must accept it. Once permission is granted, you can register the device for Push Notifications, and the token will be sent to Connectif.
Here is an example of how to request permissions and register the device to obtain the token.
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
if granted {
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
}
}
Keep learning!
To take full advantage of your Connectif account, we recommend continuing with the following articles:
- Complete guide to integrating Connectif with your Mobile App, to learn in detail about all the changes from this integration.
- iOS SDK Get Started, to add Connectif Mobile SDK to your iOS project.
- Android SDK Get Started, to add Connectif Mobile SDK to your Android project.
- Firebase Cloud Messaging Configuration, to activate sending and receiving pushes via the Android Mobile SDK.