USSD implementation for React Native on Android
npm install react-native-android-ussdReact Native Library to handle USSD on Android.
$ npm install react-native-android-ussd --save
$ react-native link react-native-android-ussd
Following configurations need to be done before using in either of the platforms
Add permissions to Make calls in the Manifest
``xml`
Ussd code can be dialled simply by calling dial method with required dialling number.
`javascript
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()
`javascript
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
`javascript
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();
}
}
``