Forum Xamarin.Android

Ask permission for receiving SMS

I have a Xamarin.Android project with MVVMCross.

I need to receive SMS. But at first, I should create the request for the user and show popup window for the confirmation.

So, at first, I add the permissions in AndroidManifest:

<uses-permission android:name="android.permission.RECEIVE_SMS" />

But I need to make a popup window that should be shown after user clicks "Next" button during the registration.

Can I do it without any plugins?

I tried to use two plugins:

  • PermissionsPlugin
  • ACR User Dialogs

So, I can show two examples of the code.
Here is the ViewModel.cs part that works. When the user types his number and clicks the button, the user's smartphone receives the SMS:

using MvvmCross.Core.Navigation;
using MvvmCross.Core.ViewModels;
using System.Collections.Generic;
using TMy.project.Core.Models;
using System.Windows.Input;
using My.project.Core.Services;

...

public RegistrationViewModel(IMvxNavigationService navigationService)
{
   _navigationService = navigationService;

   RegisterCommand = new MvxAsyncCommand(async () => {
        RegistrationService.SendCode(PhoneNumber);
        await _navigationService.Navigate<VerificationViewModel>();
    });
}

public IMvxAsyncCommand RegisterCommand { get; private set; }

But I need to add the popup window. So I changed this code:

using MvvmCross.Core.Navigation;
using MvvmCross.Core.ViewModels;
using System.Collections.Generic;
using My.project.Core.Models;
using System.Windows.Input;
using My.project.Core.Services;
using Plugin.Permissions;
using Plugin.Permissions.Abstractions;
using Acr.UserDialogs;

public RegistrationViewModel(IMvxNavigationService navigationService)
    {
    _navigationService = navigationService;

    RegisterCommand = new MvxAsyncCommand(async () =>

       {
           var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Sms);
           if (status != PermissionStatus.Granted)
           {
               if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Sms))
               {
                   var result = await UserDialogs.Instance.ConfirmAsync(new ConfirmConfig
                   {
                       Message = "Do you allow the application read your SMS?",
                       OkText = "Yes",
                       CancelText = "No"
                   });
                   if (result)
                   {
                       var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Sms);
                   }

               }

               if (status == PermissionStatus.Granted)
               {
                   PhoneNumber = SelectedCountryCode.Name + UserNumber;
                   RegistrationService.SendCode(PhoneNumber);
                   await _navigationService.Navigate<VerificationViewModel>();
               }
           }
       });
} 

public IMvxAsyncCommand RegisterCommand { get; private set; }

The build is still successful, there are no underlines in the code, but it doesn't work.
I don't see the popup window and moreover, the phone doesn't receive the SMS! So the older code is broken.

What's wrong?
Maybe there is an easier way to show the user dialog with yes/no buttons and give the permissions?

Sign In or Register to comment.