react-native-android-ussd

1.0.21 • Public • Published

react-native-android-ussd

React Native Library to handle USSD on Android.

Getting started

$ npm install react-native-android-ussd --save

Mostly automatic installation

$ react-native link react-native-android-ussd

Usage

Following configurations need to be done before using in either of the platforms

Android

Add permissions to Make calls in the Manifest

<manifest ...>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application...>

Dialing a USSD CODE

Ussd code can be dialled simply by calling dial method with required dialling number.

import Ussd from "react-native-android-ussd";

// Add USSD code you want to dial
Ussd.dial("*#456#");

A event listener should be initialized to listen for ussd replies from the dialling made using Ussd.dial()

import Ussd, {ussdEventEmitter} from 'react-native-android-ussd';

// Add USSD code you want to dial
const sim = 0;
//sim is the sim card to dial the ussd code with
Ussd.dial("*#456#", sim);


....
// in useEffect or in componentDidMount
this.eventListener = ussdEventEmitter.addListener('ussdEvent', (event) => {
       console.log(event.ussdReply)
    });

....

//unregister the listener after using (probably in componentWillUnmount)
this.eventListener.remove();
....

Example Usecase

import * as React from "react";
import { Text, View, TouchableOpacity, PermissionsAndroid } from "react-native";
import Ussd, { ussdEventEmitter } from "react-native-android-ussd";

export default class App extends React.Component {
  async dialUssd() {
    let granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.CALL_PHONE,
      {
        title: "I need to make some calls",
        message: "Give me permission to make calls ",
      }
    );

    if (granted) {
      Ussd.dial("*#456#", 1);
    } else {
      console.log("CALL MAKING Permission Denied");
    }
  }
  componentDidMount() {
    this.eventListener = ussdEventEmitter.addListener("ussdEvent", (event) => {
      console.log(event.ussdReply);
    });
  }
  componentWillUnmount() {
    this.eventListener.remove();
  }
}

Package Sidebar

Install

npm i react-native-android-ussd

Weekly Downloads

0

Version

1.0.21

License

MIT

Unpacked Size

67.6 kB

Total Files

27

Last publish

Collaborators

  • engrtitus