Skip to content

Latest commit

 

History

History
318 lines (226 loc) · 10.9 KB

iOS.Example.md

File metadata and controls

318 lines (226 loc) · 10.9 KB

Example React Native iOS Implementation for Responsys SDK

This is the example implementation for iOS of PushIOManager SDK with React Native.

Requirements

1. Install PushIOManager React Native module

Create new react app or go to existing app.

cd <your_react_native_app>
// Install the pushiomanager react native module
yarn add @oracle/react-native-pushiomanager // or npm install @oracle/react-native-pushiomanager

2. Setup PushIOManager framework installation

Run the below commands after installing the react-native-pushiomanager module

//Create Framework directory
mkdir ios/framework

//Copy the podspec file in the framework directory. **This step is crucial**
cp node_modules/@oracle/react-native-pushiomanager/PushIOManager/PushIOManager.podspec  ios/framework/

Place the latest iOS CX_Mobile_SDK.xcframework inside ios/framework

After above these steps your framework directory should look like this.

FrameworkCopy

3. Add PushIOManager dependency to Podfile

Go to iOS directory in your react native app. Open the Podfile

use_native_modules!
pod 'PushIOManager', :path => './framework/'

Sample Podfile after adding PushIOManager dependenct look like this.

Note: This depends on the your react native version. Verify the PushIOManager setup.

require_relative '../node_modules/react-native/scripts/react_native_pods'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'

platform :ios, '12.0'

target 'testapp' do
  config = use_native_modules!

  use_react_native!(
    :path => config[:reactNativePath],
    # to enable hermes on iOS, change `false` to `true` and then install pods
    :hermes_enabled => false
  )

  target 'testappTests' do
    inherit! :complete
    # Pods for testing
  end

  # Enables Flipper.
  #
  # Note that if you have use_frameworks! enabled, Flipper will not work and
  # you should disable the next line.
  use_flipper!()
  
  use_native_modules!
  pod 'PushIOManager', :path => './framework/'

  post_install do |installer|
    react_native_post_install(installer)
    __apply_Xcode_12_5_M1_post_install_workaround(installer)
  end
end

4. Install Pods

//iOS directory in your react native app
cd ios 
pod install

5. Copy pushio.config.json

Download your iOS pushio_config.json from Responsys Mobile App Developer Console.

Open your iOS project <react-native-app-path>/ios/<project-name>.xcworkspace in Xcode.

Drag and Drop the pushio_config.json in Xcode in your <react-native-test-app-name> directory

Please make sure to select <Your App Target> in "Add to Targets" and also "Copy items if needed".

AddTarget

5. Implementing Delegate Methods:

Please make sure to implement the Bold code parts in your AppDelegate.m.


//Appdelegate.m

#import "AppDelegate.h"

#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>

#ifdef FB_SONARKIT_ENABLED
#import <FlipperKit/FlipperClient.h>
#import <FlipperKitLayoutPlugin/FlipperKitLayoutPlugin.h>
#import <FlipperKitUserDefaultsPlugin/FKUserDefaultsPlugin.h>
#import <FlipperKitNetworkPlugin/FlipperKitNetworkPlugin.h>
#import <SKIOSNetworkPlugin/SKIOSNetworkAdapter.h>
#import <FlipperKitReactPlugin/FlipperKitReactPlugin.h>


//Import Framework
#import <PushIOManager/PushIOManager.h>
//For UserNotifications support
#import <UserNotifications/UserNotifications.h>

//Implement UserNotifications delegate to receive the notification callbacks in iOS 10.
@interface AppDelegate()<UNUserNotificationCenterDelegate>
@end


static void InitializeFlipper(UIApplication *application) {
  FlipperClient *client = [FlipperClient sharedClient];
  SKDescriptorMapper *layoutDescriptorMapper = [[SKDescriptorMapper alloc] initWithDefaults];
  [client addPlugin:[[FlipperKitLayoutPlugin alloc] initWithRootNode:application withDescriptorMapper:layoutDescriptorMapper]];
  [client addPlugin:[[FKUserDefaultsPlugin alloc] initWithSuiteName:nil]];
  [client addPlugin:[FlipperKitReactPlugin new]];
  [client addPlugin:[[FlipperKitNetworkPlugin alloc] initWithNetworkAdapter:[SKIOSNetworkAdapter new]]];
  [client start];
}
#endif

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  
  
#ifdef FB_SONARKIT_ENABLED
  InitializeFlipper(application);
#endif

  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                   moduleName:@"testapp"
                                            initialProperties:nil];
![#f03c15](https://placehold.it/15/f03c15/000000?text=+) `#f03c15`
  if (@available(iOS 13.0, *)) {
      rootView.backgroundColor = [UIColor systemBackgroundColor];
  } else {
      rootView.backgroundColor = [UIColor whiteColor];
  }

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];
  

  // Responsys implementation 
  [UNUserNotificationCenter currentNotificationCenter].delegate = self;
  
  //To recieve the notifications in App foreground
  [PushIOManager sharedInstance].notificationPresentationOptions = UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound;

  // Call the didFinishLaunching of SDK at end.
  [[PushIOManager sharedInstance] didFinishLaunchingWithOptions:launchOptions];
  
  return YES;
}

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
  return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
#else
  return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}


- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  [[PushIOManager sharedInstance] didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  [[PushIOManager sharedInstance] didReceiveRemoteNotification:userInfo fetchCompletionResult:UIBackgroundFetchResultNewData fetchCompletionHandler:completionHandler];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    [[PushIOManager sharedInstance] didFailToRegisterForRemoteNotificationsWithError:error];
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options {
   [[PushIOManager sharedInstance] openURL:url options:options];
  return YES;
}

//iOS 10 or later
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler {

    [[PushIOManager sharedInstance] userNotificationCenter:center didReceiveNotificationResponse:response withCompletionHandler:completionHandler];
}

-(void) userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:
(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
    [[PushIOManager sharedInstance] userNotificationCenter:center willPresentNotification:notification withCompletionHandler:completionHandler];
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options {
    [[PushIOManager sharedInstance] openURL:url options:options];
   return YES;
}


@end

Usage

The module can be accessed in JS code as follows,

import PushIOManager from 'react-native-pushiomanager';

Configure And Register

  • Configure the SDK,

     PushIOManager.configure("your-pushio_config_name.json", (error, response) => {
           
     });
  • Once the SDK is configured, register the app with Responsys,

     import { Platform } from 'react-native';
     
     if (Platform.OS === 'android') {
     	PushIOManager.registerApp(true, (error, response) => {
         });
     } else {
     	PushIOManager.registerForAllRemoteNotificationTypes((error, response) => { //This api will raise iOS push permission alert                 
         	PushIOManager.registerApp(true, (error, response) => {			
     		});  
     	});
     }
  • Combination of above steps

     import { Platform } from 'react-native';
    
     PushIOManager.configure("your-pushio_config_name.json", (error, response) => { // Configure the SDK with config provided
       if (Platform.OS === 'android') {
           PushIOManager.registerApp(true, (error, response) => { //Register for android
                 
           });
       } else {
     	  PushIOManager.registerForAllRemoteNotificationTypes((error, response) => { //This api will raise iOS push permission alert                 
               PushIOManager.registerApp(true, (error, response) => { //Register for iOS. `useLocation` is now supported in 7.0.0.
    
     	      });  
     	  });
      }
           
     });
  • Additional APIs (optional)

    iOS Only:

    • You can delay registration while app is launching or coming to foreground by implementing below API.
    // Implement before `registerForAllRemoteNotificationTypes` calls.
    PushIOManager.setDelayRegistration(true); 
    

Support

If you have access to My Oracle Support, please raise a request here, otherwise open an issue in this repository.

License

Copyright (c) 2020 Oracle and/or its affiliates and released under the Universal Permissive License (UPL), Version 1.0.

Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.