Enabling push notifications with the Connect SDK on iOS
Push notifications allow users to instantly receive important information on their mobile devices. To enable push notifications in an iOS native mobile app that uses the Connect SDK, you must perform server-side configuration and make changes to the project files for the iOS native mobile app.
Server-side configuration
Perform the following server-side configuration steps:
- Go to Apple Development Center to create a provisioning profile for the iOS mobile app that you are developing.
- Create a Pega 7 Platform application and do the following steps:
- On the Mobile tab of the Application rule form, click .
- Create an iOS certificate set that specifies the push notifications certificate file.
Remember that to use an iOS certificate set, you must also use a mobile provisioning file. For more information, see Configuring push notifications on iOS. - In the Certificate set name list, select the new iOS certificate set that you created in the previous step.
- Click .
- Send push notifications by using the Push Console or by adding a push notifications shape to a workflow in your application. For more information, see Accessing the Push Console and Adding the push notifications shape to a workflow.
Procedure
Perform the following steps to enable push notifications in an iOS native mobile app project that uses the Connect SDK.
- Open the Xcode project for your mobile app.
- Add the
UserNotifications
andConnectSDK
framework import statements to the AppDelegate.swift file:import UserNotifications import ConnectSDK
- To register for push notifications in a standard way, add the following code to the
AppDelegate
class:func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { UNUserNotificationCenter.current().requestAuthorization(options: [.sound, .alert, .badge]) { granted, error in if granted { DispatchQueue.main.async { UIApplication.shared.registerForRemoteNotifications() } } } return true }
- To obtain a device token from the APNS server and register for push notifications on your instance of Pega Platform, add the following code to the
AppDelegate
class:func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)}) NSLog("application:didRegisterForRemoteNotificationsWithDeviceToken: \(deviceTokenString)") let pushRegisterURL = URL(string: "http://sample.pega.com:8080/prweb")! PegaApi.shared.authenticationService.registerPushNotification(serverURL: pushRegisterURL, user: "test.user", password: "password", deviceToken: deviceToken) { error in if let error = error { NSLog("Login error: \(error.localizedDescription)") return } NSLog("Registered") // ... } } func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { NSLog("application:didFailToRegisterForRemoteNotificationsWithError: \(error.localizedDescription)") }
- To receive remote push notifications, add the following code to the
AppDelegate
class:func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { self.application(application, didReceiveRemoteNotification: userInfo) completionHandler(.newData) // ... } func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) { // ... }