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]1- (void)registerForRemoteNotifications;
2- (void)didRegisterForRemoteNotificationsWithDeviceToken:
3 (NSData*)deviceTokenData;
4- (BOOL)registerForSalesforceNotifications; // for internal use
5- (BOOL)unregisterSalesforceNotifications; // for internal useMobile 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.
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} -
If authorization is granted, call the SFPushNotificationManager
registerForRemoteNotifications
method. If authorization isn’t granted or the operation returned an
error, log 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}
-
In this method, request authorization to receive push notifications.
-
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.
-
In the application:didRegisterForRemoteNotificationsWithDeviceToken:
method, register the device token from Apple with Mobile SDK’s push notification
manager.
-
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} -
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}
-
Call the SFPushNotificationManager
didRegisterForRemoteNotificationsWithDeviceToken:
method.
-
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}