-
-
Save anishtr4/1d9c6aea15190aeb8a7f852f5c747be8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) 2011-present, salesforce.com, inc. All rights reserved. | |
Redistribution and use of this software in source and binary forms, with or without modification, | |
are permitted provided that the following conditions are met: | |
* Redistributions of source code must retain the above copyright notice, this list of conditions | |
and the following disclaimer. | |
* Redistributions in binary form must reproduce the above copyright notice, this list of | |
conditions and the following disclaimer in the documentation and/or other materials provided | |
with the distribution. | |
* Neither the name of salesforce.com, inc. nor the names of its contributors may be used to | |
endorse or promote products derived from this software without specific prior written | |
permission of salesforce.com, inc. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR | |
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY | |
WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
#import "AppDelegate.h" | |
#import "InitialViewController.h" | |
#import <React/RCTRootView.h> | |
#import <React/RCTBundleURLProvider.h> | |
#import <SalesforceSDKCore/SFSDKAppConfig.h> | |
#import <SalesforceSDKCore/SFPushNotificationManager.h> | |
#import <SalesforceSDKCore/SFDefaultUserManagementViewController.h> | |
#import <SalesforceSDKCore/SalesforceSDKManager.h> | |
#import <SalesforceSDKCore/SFUserAccountManager.h> | |
#import <SalesforceReact/SalesforceReactSDKManager.h> | |
#import <SalesforceSDKCore/SFLoginViewController.h> | |
#import <SalesforceReact/SFSDKReactLogger.h> | |
#import <SalesforceSDKCore/SFSDKAuthHelper.h> | |
#import "RNBootSplash.h" // <- add the header import | |
#import <UserNotifications/UserNotifications.h> | |
#import <RNCPushNotificationIOS/RCTConvert+Notification.h> | |
#import <RNCPushNotificationIOS.h> | |
#import <React/RCTBridge.h> | |
#import <React/RCTBundleURLProvider.h> | |
#import <React/RCTLinkingManager.h> | |
@implementation AppDelegate { | |
__weak UILocalNotification * initialNotificationPlaceholder; | |
__weak RCTBridge *bridge; | |
} | |
- (id)init | |
{ | |
self = [super init]; | |
if (self) { | |
// Need to use SalesforceReactSDKManager in Salesforce Mobile SDK apps using React Native | |
[SalesforceReactSDKManager initializeSDK]; | |
//App Setup for any changes to the current authenticated user | |
[SFSDKAuthHelper registerBlockForCurrentUserChangeNotifications:^{ | |
[self resetViewState:^{ | |
[self setupRootViewController]; | |
}]; | |
}]; | |
} | |
return self; | |
} | |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions | |
{ | |
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; | |
[self initializeAppViewState]; | |
// If you wish to register for push notifications, uncomment the line below. Note that, | |
// if you want to receive push notifications from Salesforce, you will also need to | |
// implement the application:didRegisterForRemoteNotificationsWithDeviceToken: method (below). | |
// [self registerForRemotePushNotifications]; | |
//Uncomment the code below to see how you can customize the color, textcolor, font and fontsize of the navigation bar | |
[self customizeLoginView]; | |
[SFSDKAuthHelper loginIfRequired:^{ | |
[self setupRootViewController]; | |
}]; | |
NSLog(@"App launched %@", launchOptions); | |
NSMutableDictionary * newLaunchOptions = launchOptions.mutableCopy; | |
if (newLaunchOptions == nil) { | |
newLaunchOptions = [NSMutableDictionary new]; | |
} | |
UILocalNotification * placeholder = [UILocalNotification new]; | |
initialNotificationPlaceholder = placeholder; | |
[newLaunchOptions setObject:initialNotificationPlaceholder forKey:UIApplicationLaunchOptionsLocalNotificationKey]; | |
RCTBridge * bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:newLaunchOptions]; | |
self->bridge = bridge; | |
/* Initialization of the Core EventsManager singleton. */ | |
// Define UNUserNotificationCenter | |
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; | |
center.delegate = self; | |
return YES; | |
} | |
/* | |
*/ | |
//sdk functions | |
// Called when a notification is delivered to a foreground app. | |
-(void)userNotificationCenter:(UNUserNotificationCenter *)center | |
willPresentNotification:(UNNotification *)notification | |
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler | |
{ | |
// Still call the JS onNotification handler so it can display the new message right away | |
NSDictionary *userInfo = notification.request.content.userInfo; | |
[RNCPushNotificationIOS didReceiveRemoteNotification:userInfo | |
fetchCompletionHandler:^void (UIBackgroundFetchResult result){}]; | |
// allow showing foreground notifications | |
completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge); | |
// or if you wish to hide all notification while in foreground replace it with | |
// completionHandler(UNNotificationPresentationOptionNone); | |
} | |
// Required for localNotification event | |
- (void)userNotificationCenter:(UNUserNotificationCenter *)center | |
didReceiveNotificationResponse:(UNNotificationResponse *)response | |
withCompletionHandler:(void (^)(void))completionHandler | |
{ | |
UILocalNotification * wrapper= [[self->bridge launchOptions] objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; | |
NSDictionary * realNotification = [RCTConvert RCTFormatUNNotificationResponse:response]; | |
wrapper.alertAction = realNotification[@"alertAction"]; | |
wrapper.alertBody = realNotification[@"alertBody"]; | |
wrapper.alertLaunchImage = realNotification[@"alertLaunchImage"]; | |
wrapper.alertTitle = realNotification[@"alertTitle"]; | |
wrapper.applicationIconBadgeNumber = [realNotification[@"applicationIconBadgeNumber"] intValue]; | |
wrapper.category = realNotification[@"category"]; | |
wrapper.fireDate = realNotification[@"date"]; | |
wrapper.hasAction = realNotification[@"hasAction"]; | |
wrapper.region = realNotification[@"region"]; | |
wrapper.regionTriggersOnce = realNotification[@"regionTriggersOnce"]; | |
wrapper.repeatCalendar = realNotification[@"repeatCalendar"]; | |
wrapper.repeatInterval = [realNotification[@"repeatInterval"] intValue]; | |
wrapper.soundName = realNotification[@"soundName"]; | |
wrapper.timeZone = realNotification[@"timeZone"]; | |
wrapper.userInfo = realNotification[@"userInfo"]; | |
NSLog(@"local notification"); | |
[RNCPushNotificationIOS didReceiveNotificationResponse:response]; | |
} | |
// Required for the notification event. You must call the completion handler after handling the remote notification. | |
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo | |
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler | |
{ | |
NSLog(@"Notification received %@",userInfo); | |
[RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler]; | |
} | |
- (void)registerForRemotePushNotifications { | |
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) { | |
if (granted) { | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
[[SFPushNotificationManager sharedInstance] registerForRemoteNotifications]; | |
}); | |
} | |
if (error) { | |
[SFLogger e:[self class] format:@"Push notification authorization error: %@", error]; | |
} | |
}]; | |
} | |
- (void)customizeLoginView { | |
SFSDKLoginViewControllerConfig *loginViewConfig = [[SFSDKLoginViewControllerConfig alloc] init]; | |
// Set showSettingsIcon to NO if you want to hide the settings icon on the nav bar | |
loginViewConfig.showSettingsIcon = NO; | |
// Set showNavBar to NO if you want to hide the top bar | |
loginViewConfig.showNavbar = NO; | |
loginViewConfig.navBarColor = [UIColor colorWithRed:0.051 green:0.765 blue:0.733 alpha:1.0]; | |
loginViewConfig.navBarTitleColor = [UIColor whiteColor]; | |
loginViewConfig.navBarFont = [UIFont fontWithName:@"Helvetica" size:16.0]; | |
[SFUserAccountManager sharedInstance].loginViewControllerConfig = loginViewConfig; | |
} | |
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken | |
{ | |
// Uncomment the code below to register your device token with the push notification manager | |
[self didRegisterForRemoteNotificationsWithDeviceToken:deviceToken]; | |
[RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken]; | |
[[SFPushNotificationManager sharedInstance] didRegisterForRemoteNotificationsWithDeviceToken:deviceToken]; | |
if ([SFUserAccountManager sharedInstance].currentUser.credentials.accessToken != nil) { | |
[[SFPushNotificationManager sharedInstance] registerSalesforceNotificationsWithCompletionBlock:nil failBlock:nil]; | |
} | |
} | |
- (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { | |
[[SFPushNotificationManager sharedInstance] didRegisterForRemoteNotificationsWithDeviceToken:deviceToken]; | |
if ([SFUserAccountManager sharedInstance].currentUser.credentials.accessToken != nil) { | |
[[SFPushNotificationManager sharedInstance] registerSalesforceNotificationsWithCompletionBlock:nil failBlock:nil]; | |
} | |
} | |
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error | |
{ | |
NSLog(@"Notification fail"); | |
// Respond to any push notification registration errors here. | |
//[RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error]; | |
} | |
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options | |
{ | |
// Uncomment following block to enable IDP Login flow | |
// return [self enableIDPLoginFlowForURL:url options:options]; | |
return NO; | |
} | |
- (BOOL)enableIDPLoginFlowForURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options { | |
return [[SFUserAccountManager sharedInstance] handleIDPAuthenticationResponse:url options:options]; | |
} | |
#pragma mark - Private methods | |
- (void)initializeAppViewState | |
{ | |
if (![NSThread isMainThread]) { | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
[self initializeAppViewState]; | |
}); | |
return; | |
} | |
self.window.rootViewController = [[InitialViewController alloc] initWithNibName:nil bundle:nil]; | |
[self.window makeKeyAndVisible]; | |
} | |
- (void)setupRootViewController | |
{ | |
NSURL *jsCodeLocation; | |
jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil]; | |
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation | |
moduleName:@"myapp" | |
initialProperties:nil | |
launchOptions:self.launchOptions]; | |
rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1]; | |
UIViewController *rootViewController = [UIViewController new]; | |
rootViewController.view = rootView; | |
self.window.rootViewController = rootViewController; | |
[RNBootSplash initWithStoryboard:@"LaunchScreen" rootView:rootView]; // <- initialization using the storyboard file name | |
} | |
- (void)resetViewState:(void (^)(void))postResetBlock | |
{ | |
if ([self.window.rootViewController presentedViewController]) { | |
[self.window.rootViewController dismissViewControllerAnimated:NO completion:^{ | |
postResetBlock(); | |
}]; | |
} else { | |
postResetBlock(); | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment