maui icon indicating copy to clipboard operation
maui copied to clipboard

Java.Lang.IllegalArgumentException when I try to start a ForegroundService

Open ComptonAlvaro opened this issue 2 years ago • 9 comments

Description

I would like to start a Foreground Service using the overload method of StartForeground in which I can pass as parameter the permission that I need for the service.

This is the documentation:

My code is this:

In the manifest, I include this:

      <service
          android:name=".AlarmasForegroundService"
          android:foregroundServiceType="location"
          android:exported="false">
      </service>

And this is how I start the ForegroundService:

var notificationManager = (GetSystemService(Context.NotificationService) as NotificationManager)!;

createNotificationChannel(_notificationManager);


Notification notification = new Notification.Builder(this, NOTIFICATION_CHANNEL_ID)

    .SetContentTitle("GTS Registros Horarios")
    .SetContentText($"{DateTime.Now}: Se han iniciado las alarmas.")
    .SetSmallIcon(Resource.Drawable.logo32x32)
    .SetAutoCancel(false)
    .SetOngoing(false)
    .Build();

//Java.Lang.IllegalArgumentException
StartForeground(NOTIFICATION_ID, notification, Android.Content.PM.ForegroundService.TypeLocation);

But when I try to call the StartForeground service, I get this error:

Java.Lang.IllegalArgumentException: 'foregroundServiceType 0x00000008 is not a subset of foregroundServiceType attribute 0x00000000 in service element of manifest file'

How could I request the permission when i call the StartForeground() method? Because according to the documentation, it is needed from Android 14 because if not, I would get a security exception.

Thanks.

Steps to Reproduce

  1. Create a Foreground service.
  2. try to start the foreground service passing the permission as parameter.

Link to public reproduction project repository

not needed

Version with bug

7.0.86

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android API 33

Did you find any workaround?

I don't have any workaround.

Relevant log output

No response

ComptonAlvaro avatar May 26 '23 09:05 ComptonAlvaro

I'm afraid you don't really include enough information to diagnose the problem and this is probably not a MAUI bug. For instance is your app getting ACCESS_FINE_LOCATION or coarse location permission? Without that it won't work.

You should read through https://developer.android.com/about/versions/14/changes/fgs-types-required

philipag avatar May 28 '23 15:05 philipag

Hi @ComptonAlvaro. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ghost avatar May 30 '23 18:05 ghost

I have all permissions needed. The application works if I don't use the overload method of StartForegreound that takes 2 arguments, the ID and notification.

ComptonAlvaro avatar May 31 '23 14:05 ComptonAlvaro

In our app we only use the 2 parameter StartForeground(). If you don't include the third parameter (service type) then it will just use the service declared service type which in your case is already "location" so you can just use the method with 2 parameters instead. That is what we do and it works properly.

If you do that, do things work properly?

philipag avatar Jun 04 '23 04:06 philipag

Yes, if I use the method with two parameters it works with no problems.

My doubt is about the documentation: foreground documentation

It seems that is recommended to use the method with the three parameters, in which in the the third parameter, I can pass all the needed permissions.

Perhaps I misunderstood and the documentation doesn't recomend to use this method with three parameters and it is enough to use the method with two parameters.

ComptonAlvaro avatar Jun 05 '23 10:06 ComptonAlvaro

still not working and now it is required for android 14 :/

aksik avatar Oct 19 '23 15:10 aksik

Yes. Android 14 requires that third parameter and for some reason the service declaration in the AndroidManifest is treated as if non existent.

With or without the service declaration in the XAML file the same Java.Lang.IllegalArgumentException occurs. Irrespective of the provided type, whether it is location or something else like health.

This is how the resulting APK's Manifest looks like in my case:

        <service
            android:name="PrayerTimeEngine.Platforms.Android.MyNotificationService"
            android:foregroundServiceType="0x100" />

// something else 

        <service
            android:name="crc64971d130048267f6d.MyNotificationService" />

Just guessing right now but maybe the second declaration is the effective one and is apparently missing this foregroundServiceType. The thing is that my AndroidManifest.xaml only has the first declaration with type before the APK is built. So I assume the second partial declaration of this service is the problem?


UPDATE: I think I found the problem. If I am not mistaken, .NET MAUI requires us to add these service details not directly to the AndroidManifest.xaml itself but rather to the Service attribute in the C# code:

    [Service(ForegroundServiceType = global::Android.Content.PM.ForegroundService.TypeDataSync)]
    public class MyNotificationService : Service
    {
    // ...
    StartForeground(0, initialNotification, global::Android.Content.PM.ForegroundService.TypeDataSync)
    // ...
    
    }

THIS attribute will be generated into the AndroidManifest.xaml later on. Maybe this is also why the visual AndroidManifest editor doesn't include a section to register services.

Now the foreground service start call works fine on Android 14.

TalipV avatar Nov 11 '23 23:11 TalipV

Hi @ComptonAlvaro. We have added the "s/try-latest-version" label to this issue, which indicates that we'd like you to try and reproduce this issue on the latest available public version. This can happen because we think that this issue was fixed in a version that has just been released, or the information provided by you indicates that you might be working with an older version.

You can install the latest version by installing the latest Visual Studio (Preview) with the .NET MAUI workload installed. If the issue still persists, please let us know with any additional details and ideally a reproduction project provided through a GitHub repository.

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ghost avatar Jan 23 '24 10:01 ghost

According to the user’s latest reply, the user’s problem no longer recurs, so we add a non-recurrence tag.

Zhanglirong-Winnie avatar Jan 23 '24 10:01 Zhanglirong-Winnie