React Native Code Snippets

Handling Back-Press on Android Hardware

Since Hyper SDK internally uses an android fragment for opening the bank page, we will need the back-press event while the bank page is active to trigger events required for hardware back press. This can be done by invoking addEventListener on the BackHandler provided by React-Native:

componentDidMount() {
   ...
   BackHandler.addEventListener('hardwareBackPress', () => {
     return !HyperSdkReact.isNull() && HyperSdkReact.onBackPressed();
   });
   ...
 }
 componentWillUnmount() {
   ...
   BackHandler.removeEventListener('hardwareBackPress', () => null);
   ...
 }

The blocking asynchronous call HyperSdkReact.onBackPressed() (refer here) should be called during activity’s onBackPressed(). If the call returns true, Hyper SDK will handle the back press, else merchant can handle it.

Note: HyperSdkReact.isNull() can also be called before calling onBackPressed() to ensure that the HyperServices object is not null.


Handling Permissions on Android Hardware

Hyper SDK needs to listen to the response of permissions asked to the user for handling auto SMS reading (wherever applicable). To do so, the merchant's activity should delegate the response to Hyper SDK once it is received from the user. This can be done by adding the following snippet in merchant's react activity (MainActivity):

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
     HyperSdkReactModule.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

Helper: Is Null

This is a helper method and can be used to check whether the HyperServices object is null at any particular moment. It is a blocking synchronous method and returns a boolean value. Refer (here) for more information.

var isNull: boolean = HyperSdkReact.isNull();
console.log('is HyperSDK null: ', isNull);

Is Initialised (Recommended)

This is a helper / optional method to check whether SDK has been initialised after [Initiate](doc:initiate #initiate). It returns a JS Promise with a boolean value.

HyperSdkReact.isInitialised().then((init: boolean) => {
  console.log('isInitialised:', init);
});

Did this page help you?