我正在尝试使用 React Native 项目中的 NativeModules 通过我的应用程序发送短信。但我收到此错误:
[TypeError: null is not an object (evaluating 'DirectSms.sendDirectSms')]
这是我发送短信的代码:
import React, {useState} from 'react';
// import all the components we are going to use
import {
SafeAreaView,
StyleSheet,
View,
Text,
TouchableOpacity,
TextInput,
NativeModules,
PermissionsAndroid,
} from 'react-native';
var DirectSms = NativeModules.DirectSms;
const App = () => {
// Setting up States
const [mobileNumber, setMobileNumber] = useState('');
const [bodySMS, setBodySMS] = useState('');
// async function to call the Java native method
const sendDirectSms = async () => {
if (mobileNumber != null) {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.SEND_SMS,
{
title: 'Send SMS App Sms Permission',
message:
'Send SMS App needs access to your inbox ' +
'so you can send messages in background.',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
DirectSms.sendDirectSms(mobileNumber, bodySMS);
alert('SMS sent');
} else {
alert('SMS permission denied');
}
} catch (error) {
console.warn(error);
alert(error);
}
} else{
alert("Mobile number is empty")
}
}
谁能告诉我为什么它显示为空。我正在使用 android 的 SmsManager 库并尝试创建 react native 桥。