Integrating ContextPush
Follow this step-by-step process to integrate ContextPush with your iOS app.
5
6
Understanding Background Wake-Up Behavior
func sceneDidBecomeActive(_ scene: UIScene) {
// This is called when your app enters the foreground
// Place foreground-specific initialization code here
}func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Your existing app startup code
ContextPush.applicationDidFinishLaunchingWithOptions()
// Observe when the app becomes active
NotificationCenter.default.addObserver(
self,
selector: #selector(applicationDidBecomeActive),
name: UIApplication.didBecomeActiveNotification,
object: nil
)
return true
}
@objc private func applicationDidBecomeActive() {
// This is called when your app enters the foreground
// Place foreground-specific initialization code here
}7
Provide User ID and Push Token
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)
}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)
}// Set the user ID once it is available to associate notifications with the correct user
ContextPush.setUserId(userId)8
Handle Background Notifications
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any],fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// ContextSDK will process the notification if it is a valid ContextPush 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
}9
Handle Notification Opens
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()
}11
Integrate With Your Push Notification Provider
Last updated
Was this helpful?