Skip to content

Push Notifications

Prerequisites

Configure your environment to use iOS Remote Notifications:

  1. In your Xcode project, go to App Settings -> General and change the Bundle Identifier to something unique.
  2. Go to App Settings -> Capabilities and set the switch for Push Notifications to On. This creates the App ID in your member center and adds the push notifications entitlement to it.
  3. Log in to your Apple Developer Member Center and go to Certificates, Identifiers & Profiles -> Identifiers -> App IDs and select the App ID for your app. Follow the steps to download the certificate.
  4. From the mobile services cockpit, configure the application to use your push notification certificate.

Push Notification Registration

Use Apple's UIApplication.shared.registerForRemoteNotifications() to receive push notifications from the Apple Push Notification service (APNs).

Use SAP BTP SDK for iOS's SAPcpmsRemoteNotificationClient.registerDeviceToken() to upload the device token to SAP Mobile Services so that SAP Mobile Services can send push notifications to that device using APNs.

// step 1
UIApplication.shared.registerForRemoteNotifications()

var remoteNotificationClient = SAPFoundation.SAPcpmsRemoteNotificationClient(sapURLSession: urlSession, settingsParameters: settingsParameters)

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    // step 2
    remoteNotificationClient.registerDeviceToken(deviceToken) { error in
        // error handling
        // ...
    }
}

For detailed information, see the API reference documentation.

Subscribe to Push Notifications

Applications created using the SAP BTP SDK for iOS can subscribe to specific push notifications based on a user's topics of interest, such as patches, new features, or security, for example. Topics may be considered to be part of the relationship between mobile apps and the notification producer for those given topics. The SDK supports customizing this relationship by allowing users to subscribe to certain topics while ignoring others. With this feature, a device can receive notifications only for the subscribed topics and avoid unwanted app notifications.

The SDK provides the following methods for subscriptions:

  • The mobile application can retrieve all active subscriptions using the getAllSubscribedTopics method.
Task.init {
    let remoteNotificationClient = SAPcpmsRemoteNotificationClient(sapURLSession: urlSession, settingsParameters: settingsParameters)
    do {
        let subscribedTopics = try await remoteNotificationClient.getAllSubscribedTopics()
        // Handle the response
    } catch {
        logger.error("Failed to retrieve list of active topic subscriptions, error: \(error)")
    }
}
  • Mobile applications can subscribe to a new topic using the subscribe(to topic: String) method.
Task.init {
    let remoteNotificationClient = SAPcpmsRemoteNotificationClient(sapURLSession: urlSession, settingsParameters: settingsParameters)
    do {
        try await remoteNotificationClient.subscribe(to: "Topic1")
    } catch {
        logger.error("Failed to subscribe to topic, error: \(error)")
    }
}
  • Mobile applications can unsubscribe from a topic using the unsubscribe(from topic: String) method.
Task.init {
    let remoteNotificationClient = SAPcpmsRemoteNotificationClient(sapURLSession: urlSession, settingsParameters: settingsParameters)
    do {
        try await remoteNotificationClient.unsubscribe(from: "Topic1")
    } catch {
        logger.error("Failed to unsubscribe from topic, error: \(error)")
    }
}

Last update: December 21, 2023