About Push Notifications
Configure a Connected App or External Client App for APNS (iOS)
Code Modifications (iOS)
Configure WebSockets
REST Wrappers for SFAP APIs
Using Key-Value Stores for Secure Data Storage
Dark Mode and Dark Theme Settings
To handle notifications in iOS apps, you register in the AppDelegate class using the provided template code. In Swift apps, you must add an extension to decrypt incoming Notification Builder notifications.
Mobile SDK for iOS provides the PushNotificationManager class to handle push registration. To use it in Objective-C, include @import SalesforceSDKCore to support PushNotificationManager. Swift doesn’t require a specific import.
The SFPushNotificationManager class is available as a runtime shared instance:
1PushNotificationManager.sharedInstance()1[SFPushNotificationManager sharedInstance]This class implements these registration methods:
1func registerForRemoteNotifications()
2
3func application(_ application: UIApplication,
4 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
5
6func registerSalesforceNotifications(completionBlock: (() → Void)?,
7 fail: (() → Void)?)
8
9func unregisterSalesforceNotifications(withCompletionBlock: UserAccount,
10 completionBlock: (() → Void)?) // for internal use1- (void)registerForRemoteNotifications;
2
3- (void)didRegisterForRemoteNotificationsWithDeviceToken:
4 (NSData*)deviceTokenData;
5
6- (BOOL)registerSalesforceNotificationsWithCompletionBlock:(nullable
7 void (^)(void))completionBlock failBlock:(nullable void (^)(void))failBlock;
8
9- (BOOL)unregisterSalesforceNotificationsWithCompletionBlock:(SFUserAccount*)user
10 completionBlock:(nullable void (^)(void))completionBlock; // for internal useMobile SDK calls unregisterSalesforceNotifications at logout.
Note
In Mobile SDK 8.2 and later, the forceios iOSNativeSwiftTemplate app requests notification authorization through the iOS UNUserNotificationCenter object. A specialized version of iOSNativeSwiftTemplate, iOSNativeSwiftEncryptedNotificationTemplate, extends the UNNotificationServiceExtension iOS class to handle notification decryption. This extension class, NotificationService, provides boilerplate decryption code that Mobile SDK apps can use without changes. To support encrypted notifications, you must be using Mobile SDK 8.2 or later, and your app must include this extension.
To create a Swift project that supports Notification Builder encrypted notifications, you can use the iOSNativeSwiftEncryptedNotificationTemplate template with forceios. Even if you’re updating an existing Mobile SDK project, it’s easiest to start fresh with a new forceios template project. If you’d rather update a Swift project manually, skip to “Example: Add Push Registration Manually (Swift)”.
Install the latest forceios version from node.js:
1[sudo] npm install -g forceiosCall the forceios createWithTemplate command:
1forceios createWithTemplateAt the first prompt, enter iOSNativeSwiftEncryptedNotificationTemplate:
1forceios createWithTemplate
2Enter URI of repo containing template application or a Mobile SDK template name:
3 iOSNativeSwiftEncryptedNotificationTemplateIn the remaining prompts, enter your company and project information. If your information is accepted, forceios creates a project that is ready for encrypted notifications.
If you’re updating an older Mobile SDK project, copy your app-specific resources from your old project into the new project.
This example requires an existing app built on Mobile SDK 8.2 or higher and based on the iOSSwiftNativeTemplate project.
Configure Push Notification Registration in AppDelegate
In the application(_:didFinishLaunchingWithOptions:) method, uncomment the call to registerForRemotePushNotifications.
1func application(_ application: UIApplication, didFinishLaunchingWithOptions
2 launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
3
4 self.window = UIWindow(frame: UIScreen.main.bounds)
5 self.initializeAppViewState()
6 // If you wish to register for push notifications, uncomment the line below.
7 // Note that if you want to receive push notifications from Salesforce,
8 // you will also need to implement the
9 // application(application, didRegisterForRemoteNotificationsWithDeviceToken)
10 // method (below).
11 //
12 self.registerForRemotePushNotifications()
13 ...The registerForRemotePushNotifications method attempts to register your app with Apple for receiving remote notifications. If registration succeeds, Apple passes a device token to the application(_:didRegisterForRemoteNotificationsWithDeviceToken:) method of your AppDelegate class.
In the application(_:didRegisterForRemoteNotificationsWithDeviceToken:) method, uncomment the call to didRegisterForRemoteNotifications(withDeviceToken:).
1func application(_ application: UIApplication,
2 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
3 // Uncomment the code below to register your device token with the
4 // push notification manager
5 //
6 didRegisterForRemoteNotifications(deviceToken)
7 ...
8 }
9 ...To log a debugger error if registration with Apple fails, add the following code to the application(_:didFailToRegisterForRemoteNotificationsWithError:) method.
1func application(_ application: UIApplication,
2 didFailToRegisterForRemoteNotificationsWithError error: Error ) {
3
4 // Respond to any push notification registration errors here.
5 SalesforceLogger.d(type(of:AppDelegate.self),
6 message:"Failed to get token, error: \(error)")
7 }
8}Add the Decryption Extension
AppDelegate.developer.apple.com/documentation.iOSNativeSwiftEncryptedNotificationTemplate/NotificationServiceExtension folder, replace the code in your new extension’s Swift file with the code from NotificationServiceExtension.swift.We've Moved