xamarin / GooglePlayServicesComponents

Other
314 stars 146 forks source link

bundle.GetString(SmsRetriever.ExtraStatus) returns null?? #913

Closed LeoJHarris closed 1 month ago

LeoJHarris commented 1 month ago

Xamarin.Android Version (eg: 6.0):

Android 14.0

Google Play Services Version

Not to sure, the project uses Xamarin.GooglePlayServices.Auth.Api.Phone 118.1.0.2

Describe your Issue

The following always returns null

Statuses? status = (Statuses?)bundle.GetString(SmsRetriever.ExtraStatus);

The OnReceive successfully gets called when the SMS is received, the message is visible on inspection but the above returns null every time

The complete code is

[BroadcastReceiver(Enabled = true, Exported = true)]
[IntentFilter([SmsRetriever.SmsRetrievedAction])]
public class SmsReceiverService : BroadcastReceiver
{
    public override void OnReceive(Android.Content.Context? context, Intent? intent)
    {
        if (intent?.Extras is Bundle bundle && intent?.Action is SmsRetriever.SmsRetrievedAction)
        {
            IEventAggregator? eventAggregator = App.ContainerProvider?.GetService<IEventAggregator>();

            Statuses? status = (Statuses?)bundle.GetString(SmsRetriever.ExtraStatus);
            switch (status?.StatusCode)
            {
                case CommonStatusCodes.Success:
                    if (bundle.GetString(SmsRetriever.ExtraSmsMessage) is string messageContent)
                    {
                        eventAggregator?.GetEvent<OneTimePasswordSmsReceivedEventArgs>().Publish((true, messageContent));
                    }

                    break;

                case CommonStatusCodes.Timeout:
                    eventAggregator?.GetEvent<OneTimePasswordSmsReceivedEventArgs>().Publish((false, string.Empty));
                    break;
            }
        }
    }
}

The SMS message is:

<#> Your code is: 12345 [AppHashKey]

Relevant information

Packages used:

    <ItemGroup Condition="'$(TargetFramework)' == 'net8.0-android34.0'">
    <PackageReference Include="Xamarin.AndroidX.Collection">
        <Version>1.4.4</Version>
    </PackageReference>
    <PackageReference Include="Xamarin.AndroidX.Collection.Ktx">
        <Version>1.4.4</Version>
    </PackageReference>
    <PackageReference Include="Xamarin.AndroidX.Fragment.Ktx">
        <Version>1.8.3.1</Version>
    </PackageReference>
    <PackageReference Include="Xamarin.Firebase.Core">
        <Version>121.1.1.11</Version>
    </PackageReference>
    <PackageReference Include="Xamarin.Firebase.Crashlytics">
        <Version>119.1.0</Version>
    </PackageReference>
    <PackageReference Include="Xamarin.Firebase.Messaging">
        <Version>124.0.1</Version>
    </PackageReference>
    <PackageReference Include="Xamarin.GooglePlayServices.Auth.Api.Phone">
        <Version>118.1.0.2</Version>
    </PackageReference>
</ItemGroup>

Build settings (tools)

<AndroidDexTool>d8</AndroidDexTool>
<AndroidLinkTool>r8</AndroidLinkTool>
<AndroidPackageFormat>aab</AndroidPackageFormat>

Minimal Repro Code Sample

If required a sample can be provided

jpobst commented 1 month ago
Statuses? status = (Statuses?)bundle.GetString(SmsRetriever.ExtraStatus);

This seems like you are casting a string to a Statuses?. Are you sure that's a valid cast?

LeoJHarris commented 1 month ago

@jpobst I would say that is probably the issue, happy to close and reopen if this is not the case.