iOS Client Integration
Step 1: Import ContextSDK
Step 2: Ensure Remote Notification
entitlement
In your Xcode project, go to Signing & Capabilities
and enable the Remote Notification
capability in the Background Modes
section.
Step 3: Provide User ID and Push Token
You need to register the user's device with a unique user ID and the device's push token. The user-id may be whatever you use to identify each user in your existing push notification system, e.g. an email address or a user ID from your database.
public func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
{
// Your existing code for handling device token registration
// Set the device token and user ID with ContextSDK to enable push notifications
ContextPush.setDeviceToken(deviceToken).setUserId(userId)
}
public func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
{
// Your existing code for handling device token registration
// Set the device token with ContextSDK to register the device for push notifications
ContextPush.setDeviceToken(deviceToken)
}
Step 4: Handle Background Notifications
Ensure that your app can handle push notifications received while in the background by implementing the following method:
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
{
// ContextSDK will process the notification if it is a valid Context Push notification
// The completion handler will be automatically called if processed
if ContextPush.applicationDidReceiveRemoteNotification(userInfo, fetchCompletionHandler: completionHandler) {
return
}
// Handle other types of background notifications with your existing code, if needed
}
Step 5: Handle Notification Opens
Track when users open notifications to gather engagement metrics and improve user experience:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
{
// Process the notification with ContextSDK to track open rates and user interactions
ContextPush.userNotificationCenterDidReceiveResponse(response)
// Your existing code
// Ensure completion handler is called to finalize notification handling
completionHandler()
}
Next Step: Push Provider Integration
After setting up your iOS client, integrate with your existing push provider: