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:

Swift
1PushNotificationManager.sharedInstance()
Objective-C
1[SFPushNotificationManager sharedInstance]

This class implements four registration methods:

Swift
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 use
Objective-C
1- (void)registerForRemoteNotifications;
2- (void)didRegisterForRemoteNotificationsWithDeviceToken:
3    (NSData*)deviceTokenData;
4- (BOOL)registerForSalesforceNotifications; // for internal use
5- (BOOL)unregisterSalesforceNotifications; // for internal use

Mobile 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.

  1. In your AppDelegate class, create an instance method named - (void) registerForRemotePushNotifications.
    1. In this method, request authorization to receive push notifications.
      1- (void)registerForRemotePushNotifications {
      2    [[UNUserNotificationCenter currentNotificationCenter] 
      3        requestAuthorizationWithOptions:(UNAuthorizationOptionSound | 
      4                                         UNAuthorizationOptionAlert | 
      5                                         UNAuthorizationOptionBadge) 
      6                      completionHandler:^(BOOL granted, 
      7                                          NSError * _Nullable error) {
      8        if (granted) {
      9
      10        } else {
      11
      12        }
      13
      14        if (error) {
      15
      16        }
      17    }];
      18}
    2. 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.
      1- (void)registerForRemotePushNotifications {
      2    [[UNUserNotificationCenter currentNotificationCenter] 
      3        requestAuthorizationWithOptions:(UNAuthorizationOptionSound | 
      4                                         UNAuthorizationOptionAlert | 
      5                                         UNAuthorizationOptionBadge) 
      6                      completionHandler:^(BOOL granted, 
      7                                          NSError * _Nullable error) {
      8        if (granted) {
      9            // All good! 
      10            dispatch_async(dispatch_get_main_queue(), ^{
      11               [[SFPushNotificationManager sharedInstance] 
      12                   registerForRemoteNotifications];
      13            });
      14        } else {
      15            // Authorization not granted
      16            [SFLogger d:[self class] 
      17                format:@"Push notification authorization denied"];
      18        }
      19
      20        if (error) {
      21            // Operation failed with error message
      22            [SFLogger e:[self class] 
      23                 format:@"Push notification authorization error: %@", 
      24                        error];
      25        }
      26    }];
      27}
  2. Call your registerForRemotePushNotifications in the application:didFinishLaunchingWithOptions: method.
    1- (BOOL)application:(UIApplication *)application 
    2    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    3{
    4    self.window = [[UIWindow alloc]
    5        initWithFrame:[UIScreen mainScreen].bounds];
    6    [self initializeAppViewState];
    7    
    8    [self registerForRemotePushNotifications];
    9
    10    [[SFAuthenticationManager sharedManager] 
    11        loginWithCompletion:self.initialLoginSuccessBlock 
    12                    failure:self.initialLoginFailureBlock];
    13    
    14    return YES;
    15}

    If registration succeeds, Apple passes a device token to the application:didRegisterForRemoteNotificationsWithDeviceToken: method of your AppDelegate class.

  3. In the application:didRegisterForRemoteNotificationsWithDeviceToken: method, register the device token from Apple with Mobile SDK’s push notification manager.
    1. Call the SFPushNotificationManager didRegisterForRemoteNotificationsWithDeviceToken: method.
      1- (void)application:(UIApplication*)application 
      2    didRegisterForRemoteNotificationsWithDeviceToken:
      3        (NSData*)deviceToken
      4{
      5    [[SFPushNotificationManager sharedInstance] 
      6        didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
      7
      8
      9}
    2. If the current user’s access token exists, call the SFPushNotificationManager registerSalesforceNotificationsWithCompletionBlock:failBlock: method.
      1- (void)application:(UIApplication*)application 
      2    didRegisterForRemoteNotificationsWithDeviceToken:
      3        (NSData*)deviceToken
      4{
      5    [[SFPushNotificationManager sharedInstance] 
      6        didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
      7
      8    if ([SFUserAccountManager sharedInstance].
      9            currentUser.credentials.accessToken != nil) {
      10        [[SFPushNotificationManager sharedInstance] 
      11            registerSalesforceNotificationsWithCompletionBlock:nil 
      12                                                     failBlock:nil];
      13    }
      14}
  4. To log an error if registration with Apple fails, implement the application:didFailToRegisterForRemoteNotificationsWithError: method.
    1- (void)application:(UIApplication*)application 
    2    didFailToRegisterForRemoteNotificationsWithError:
    3        (NSError*)error
    4{
    5    // Example of an implementation
    6    [SFLogger e:[self class] format:@"Push notification authorization error: %@", error];
    7}