Newer Version Available
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:
Mobile SDK calls registerForSalesforceNotifications after login and unregisterSalesforceNotifications at logout. You call the other two methods from your AppDelegate class.
1- (void)registerForRemoteNotifications;
2- (void)didRegisterForRemoteNotificationsWithDeviceToken:
3 (NSData*)deviceTokenData;
4- (BOOL)registerForSalesforceNotifications; // for internal use
5- (BOOL)unregisterSalesforceNotifications; // for internal use-
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.
-
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 }} -
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 }} -
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}