Newer Version Available

This content describes an older version of this product. View Latest

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:

1[SFPushNotificationManager sharedInstance]
This class implements four registration methods:
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.
  1. Register with Apple for push notifications by calling registerForRemoteNotifications. Place the call 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    //
    9    // Register with APNS for push notifications. Note that,
    10    // to receive push notifications from Salesforce, you
    11    // also need to register for Salesforce notifications in the 
    12    // application:
    13    // didRegisterForRemoteNotificationsWithDeviceToken:
    14    // method (as demonstrated below.)
    15    //
    16    [[SFPushNotificationManager sharedInstance]
    17        registerForRemoteNotifications];
    18
    19    [[SFAuthenticationManager sharedManager] 
    20        loginWithCompletion:self.initialLoginSuccessBlock 
    21                    failure:self.initialLoginFailureBlock];
    22    
    23    return YES;
    24}

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

  2. Forward the device token from Apple to SFPushNotificationManager by calling didRegisterForRemoteNotificationsWithDeviceToken on the SFPushNotificationManager shared instance.
    1- (void)application:(UIApplication*)application 
    2    didRegisterForRemoteNotificationsWithDeviceToken:
    3        (NSData*)deviceToken
    4{
    5    //
    6    // Register your device token with 
    7    // the push notification manager
    8    //
    9    [[SFPushNotificationManager sharedInstance] 
    10        didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
    11    }}
  3. Register to receive Salesforce notifications through the connected app by calling registerForSalesforceNotifications. Make this call only if the access token for the current session is valid.
    1- (void)application:(UIApplication*)application 
    2    didRegisterForRemoteNotificationsWithDeviceToken:
    3        (NSData*)deviceToken
    4{
    5    //
    6    // Register your device token with the 
    7    // push notification manager
    8    [[SFPushNotificationManager sharedInstance] 
    9        didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
    10
    11    if ([SFAccountManager sharedInstance].credentials.accessToken
    12        != nil){
    13        [[SFPushNotificationManager sharedInstance] 
    14            registerForSalesforceNotifications];
    15    }}

    Behind the scenes, Mobile SDK automatically reads this value and uses it to register the device against the Salesforce connected app. Each time the user re-opens the app, Mobile SDK automatically renews the registration. This validation allows Salesforce to send notifications to the connected app.

    A device becomes unregistered if:

    The app goes unused for an extended period of time

    OR

    The user logs out of the app

    Note

  4. Add the following method to log an error if registration with Apple fails.
    1- (void)application:(UIApplication*)application 
    2    didFailToRegisterForRemoteNotificationsWithError:
    3        (NSError*)error
    4{
    5    NSLog(@"Failed to get token, error: %@", error);
    6}