Apple Push Notifications Service (APNs) Setup and Mobile Push Notification Sending in Connectif

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.

listKeysApn.png

 

When creating it, you will need to check the Apple Push Notification Service (APNs) capability and click Continue.

newKeyWithAPNs.png

 

The next step will ask you for confirmation before registering the key.

registerKey.png

 

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.

downloadKey.png

 

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.

uploadKeyConnectif.png

 

Once the file is uploaded, the section interface will look like this:

configuracionInicialApnConnectif.png

 

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.

teamDevAndKeyId.png

 

Next, configure the Bundle identifier, which you can find in the general information of your project in xCode under the main target.

BundleIdentifier.png

 

Once these parameters are configured in Connectif, the integration with all settings should look like this:

integracionPushIosTerminada.png

 

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.

addCapabilitiesiOS.png

 

A dialog will appear in this section allowing you to search for and add the Push Notification capability.

Captura de pantalla 2024-10-21 a las 15.18.28.png

 

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.

2024-10-21_13-03.png

 

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.

Captura de pantalla 2024-10-21 a las 13.09.57.png

 

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:

Captura de pantalla 2024-10-21 a las 13.12.44.png

 

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

 If you need to force an update of the user's push notification permission status (for example, if the user has changed permissions in the iOS system settings and you want to immediately reflect that change in Connectif), you can use the updatePushPermissionStatus() method.

This method allows sending the current notification permission status of the device to the SDK at any time.

Connectif.updatePushPermissionStatus()

 

Congratulations!
You have reached the end of the lesson.

  

Do you still have unresolved questions?
Remember that our Connectif specialists are at your disposal. To contact them, just open a Support ticket by clicking the blue “Help” button on your dashboard.


Keep learning!

To take full advantage of your Connectif account, we recommend continuing with the following articles: