Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SendSmsInBackground exception #118

Open
lpereira98 opened this issue Oct 25, 2021 · 2 comments
Open

SendSmsInBackground exception #118

lpereira98 opened this issue Oct 25, 2021 · 2 comments

Comments

@lpereira98
Copy link

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
Copy link
Author

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants