0

The below code is working fine in android version lollipop but in newer versions of android sms body is not getting autofilled.Even after giving required permissions It opens the corresponding number with no text prefilled.Please suggest a solution

smsPermission = async() => {
    try {
        const isGranted = await PermissionsAndroid.check( PermissionsAndroid.PERMISSIONS.SEND_SMS )
        if(!isGranted) {
            const granted = await PermissionsAndroid.request(
                PermissionsAndroid.PERMISSIONS.SEND_SMS,
                {
                title: 'Send SMS Permission',
                message: 'jose2007kj need permission to send invite through sms',
                buttonNegative: 'Cancel',
                buttonPositive: 'OK',
                },
            );
            if (granted === PermissionsAndroid.RESULTS.GRANTED) {
                var sparator = Platform.OS == 'ios' ? '&' : '?';
                var message =  `Check ${this.props.user.username}'s profile on jose2007kj now ${API.BASE_URL}/pages/@${this.props.user.username}`;
                Linking.openURL('sms:'+`${this.state.invite_number}`+sparator+'body=' + message)

            }      
        }else{
            var sparator = Platform.OS == 'ios' ? '&' : '?';
            var message =  `Check ${this.props.user.username}'s profile on jose2007kj now ${API.BASE_URL}/pages/@${this.props.user.username}`;
            Linking.openURL('sms:' + `${this.state.invite_number}` + sparator + 'body=' + message)

        }
    } catch (err) {
        console.warn(err);
    }
}
Jose Kj
  • 2,912
  • 2
  • 28
  • 40

2 Answers2

1

It's probably about this restrictions. To send sms in new android versions use this Intent(I don't know the way in react-native but it must likely the same)

val intent = Intent().apply {
        action = Intent.ACTION_SENDTO
        data = Uri.parse("smsto:0123456789")
        putExtra("sms_body", "text message")
    }
    if (intent.resolveActivity(packageManager) != null) {
        startActivity(intent)
    }

And remove the permissions because apps with sms group permissions will be removed from google play if not sent the reasons of using this permissions to google.

David
  • 2,129
  • 25
  • 34
0

I was able to get it working by using below code

 @ReactMethod
  public void sendSms(String mobileNo,String message){
    try{
      Intent sendIntent;
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(reactContext);
        sendIntent = new Intent(Intent.ACTION_SEND);
        if (defaultSmsPackageName != null){
            sendIntent.setPackage(defaultSmsPackageName);
        }
        sendIntent.setType("text/plain");
      }else {
        sendIntent = new Intent(Intent.ACTION_VIEW);
        sendIntent.setType("vnd.android-dir/mms-sms");
      }
      sendIntent.putExtra("sms_body", message);
      sendIntent.putExtra("exit_on_sent", true);
      sendIntent.putExtra("address", mobileNo);
      reactContext.startActivity(sendIntent);

    }catch(Exception e){
      Log.d("sms ", "sms error: "+e.toString());
    }
  }

Reference

Davids detailed explanation regarding permissions,usage of Action_sendto

I got this piece of code from react-native-sms

Jose Kj
  • 2,912
  • 2
  • 28
  • 40