cjlotz / Xamarin.Plugins

Cross platform Xamarin Plugins
MIT License
113 stars 56 forks source link

SendSmsInBackground exception #118

Open lpereira98 opened 3 years ago

lpereira98 commented 3 years ago

Bug Information

Version Number of Plugin: 5.2.0

Android Version: 11

Version of VS: 16.11.5

Version of Xamarin: 16.11


Hello, i'm very new to Xamarin development and I'm trying to send a background SMS.

my AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.helloworld" android:installLocation="preferExternal">
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.WRITE_SMS" />
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30" />
    <application android:label="HelloWorld.Android" android:theme="@style/MainTheme">
        <provider android:name="android.support.v4.content.FileProvider" android:authorities="HelloWorld.Android.fileprovider" android:exported="false" android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data>
        </provider>
    </application>
</manifest>

This code works but it doesn't send the SMS automatically. Code:

            var smsMessenger = CrossMessaging.Current.SmsMessenger;
            if (smsMessenger.CanSendSms)
            {
                try
                {
                    smsMessenger.SendSms("+351xxxxxxxxx", "Well hello there from Xam.Messaging.Plugin");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }

When i try to run this code, i get this Exception: Untitled

The error clearly indicates that the tag android.permission.SEND_SMS isn't present, but it is .. and smsMessenger.SendSms works fine.. Sorry if this is a newbie question, but i would be much appreciated if you could help me :)

lpereira98 commented 3 years ago

Hello, solved it. Found what i need in this answer: Android permission doesn't work even if I have declared it So basically we need to add the permission in both sides, program side and also have to ask for those permissions at runtime from the user. I just enabled SMS permissions on my phone and it worked!

Thanks!

behroozbc commented 2 years ago

hi you can get permission with this

          if(await Permissions.CheckStatusAsync<Permissions.Sms>() != PermissionStatus.Granted)
            {
                var status = await Permissions.RequestAsync<Permissions.Sms>();
            }

and documentation about https://docs.microsoft.com/en-us/xamarin/essentials/permissions?tabs=android