To send and receive pushes using the iOS Mobile SDK, you need to have Apple Push Notifications Service (APNs) configured. This article explains the necessary steps to complete the setup.
1. Create and Configure the APNs Key
You may already have a Key available; in that case, you won’t need to perform these steps and can proceed to configure the integration.
From your Apple Developer account, go to the Keys section and click the + symbol to add one.
When creating it, you will need to check the Apple Push Notification Service (APNs) capability and click Continue.
The next step will ask you 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 that you can only download this file once.
2. Configure the Integration in Connectif
Within the Mobile App channel settings in Connectif, go to the iOS Push section. Upload the ".p8" file downloaded in step 1.
Once the file is uploaded, the section interface will look like this:
Select between the two Host types, Production or Development, and configure them according to your purpose.
You can obtain the Team ID and Key ID parameters from 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 under the main target.
Once these parameters are configured in Connectif, the integration with all settings should look like this:
3. Add Push Notification Capability
If your project does not have the Push Notification capability enabled, you will need to add it.
Click on your project, then on the main Target, and in the Signing & Capabilities section, click + Capability.
A dialog will appear in this section allowing you to search for and add the Push Notification capability.
4. Add Background Modes Capability with Remote Notifications
Using the same process you used to add the Push Notification capability, add the Background Modes capability.
Once added, go back to its configuration to ensure the Remote Notifications option is checked.
5. Add Notification Service Extension
To properly enrich and track your Push Notifications, you will need to create a Notification Service Extension.
Click the button to add a target and search for Notification Service Extension. A possible name for the extension can be ConnectifNotificationServiceExtension.
Remember that if your main project has a specific "Minimum Deployment," this extension should have the same.
6. Add App Group
The App Group capability will allow 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, add a container in its configuration. Maintain the format group + bundle identifier + connectif. It will look like this example:
Repeat this process and add it to the previously created Notification Service Extension, respecting the same container name.
7. Code Modifications
7.1. SDK Initialization
When starting the SDK, add the appGroup parameter indicating 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, making sure 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 directly in your AppDelegate, add the following:
Important note about permissions: When invoking the method Connectif.addPushToken(token), the Connectif SDK will automatically send, along with the new token, the current status of the user's push notification permissions on the device. This ensures that Connectif always has up-to-date information about the user's ability to receive notifications in real time.
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 whether the push was handled by Connectif, allowing you to handle 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 Permission
Remember that it is a requirement to ask the contact for permission to receive push notifications and for them to accept it in order to receive pushes. Once granted, you can register the device for Push Notifications and the token will be sent to Connectif.
Here is an example of requesting permissions and registering the device to obtain the token.
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
if granted {
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
}
}
9. Additional Configuration
1- Update Notification Permission Status
This method allows sending the current notification permission status of the device to the SDK at any time.
Connectif.updatePushPermissionStatus()
Keep learning!
To take full advantage of your Connectif account, we recommend continuing with the following articles:
- Complete Guide to Integrate Connectif with Your Mobile App, to learn in depth about all the changes in this integration.
- iOS SDK Get Started, to add the Connectif Mobile SDK to your iOS project.
- Android SDK Get Started, to add the Connectif Mobile SDK to your Android project.
- Firebase Cloud Messaging Configuration, to enable push sending and receiving through the Android Mobile SDK.