Enable Push Notifications in a Salesforce Mobile SDK iOS App
Salesforce Mobile SDK for iOS provides the SFPushNotificationManager class to handle push registration. To use it, import <SalesforceSDKCore/SFPushNotificationManager>. The SFPushNotificationManager class is available as a runtime singleton:
SwiftThis class implements four registration methods:
SwiftMobile SDK calls registerForSalesforceNotifications after login and unregisterSalesforceNotifications at logout. You call the other two methods from your AppDelegate class.
Mobile SDK 8.2 updates the push notification implementation to use the iOS UNUserNotificationCenter class. This class authorizes the calling app to receive push notifications from APNS. Mobile SDK iOS template apps request authorization for alerts, sounds, and badges.
To decode encrypted notifications, you extend the iOS UNNotificationServiceExtension class. Mobile SDK provides a special app template that includes the boilerplate extension code for you to copy. See Handle Notification Encryption in Salesforce Mobile SDK Apps.
-
In your AppDelegate class, create an
instance method named - (void)
registerForRemotePushNotifications.
-
In this method, request authorization to receive push notifications.
-
If authorization is granted, call the SFPushNotificationManager
registerForRemoteNotifications
method. If authorization isn’t granted or the operation returned an
error, log the appropriate error messages.
-
In this method, request authorization to receive push notifications.
-
Call your registerForRemotePushNotifications in the
application:didFinishLaunchingWithOptions: method.
If registration succeeds, Apple passes a device token to the application:didRegisterForRemoteNotificationsWithDeviceToken: method of your AppDelegate class.
-
In the application:didRegisterForRemoteNotificationsWithDeviceToken:
method, register the device token from Apple with Mobile SDK’s push notification
manager.
-
Call the SFPushNotificationManager
didRegisterForRemoteNotificationsWithDeviceToken:
method.
-
If the current user’s access token exists, call the SFPushNotificationManager
registerSalesforceNotificationsWithCompletionBlock:failBlock:
method.
-
Call the SFPushNotificationManager
didRegisterForRemoteNotificationsWithDeviceToken:
method.
-
To log an error if registration with Apple fails, implement the application:didFailToRegisterForRemoteNotificationsWithError:
method.