xamarin.formsservicesdkforeground

getting issue while updating my application from sdk 33 to sdk 34 on Foreground service types


`Iam upgrading my application from sdk 33 to SDK 34. while running the updated application when I am turing to the screen which uploads pictures for questionnaires , I am getting an exception

Android.App.ServiceStartNotAllowedExceptionInvoker: 'Starting FGS without a type callerApp=ProcessRecord{3e07c0e 4863:com.OIA.oia_agent.dev/u0a190} targetSDK=34' write down the changes that need to done both in uploadprogressservice.cs and andriod manifest file. So that applications works after uploading the pictures

 using Android.App;      
 using Android.App;
  using Android.Content;
  using Android.OS;
  using OIA_Agent.Helpers;
  using OIA_Agent.Services.ServiceInterfaces;
  using Xamarin.Forms;
namespace OIA_Agent.Droid.Notification
{
[Service]

public override void OnCreate()
{
    base.OnCreate();
}
public override StartCommandResult OnStartCommand(Intent 
 intent, StartCommandFlags flags, int startId)
{
    JobNumber = intent.GetStringExtra("JobNumber");
    foregroundChannelId = 
    intent.GetStringExtra("notificationChannelId");
    base.OnStartCommand(intent, flags, startId);
    Android.App.Notification notification = 
    DependencyService.Get<IProgressNotificationService> 
    ().ReturnNotif(foregroundChannelId, 
    ConstantHelper.Alerts.UploadStarted + " " + JobNumber, 
    ConstantHelper.Alerts.UploadInProgress1 + JobNumber + 
    ConstantHelper.Alerts.UploadInProgress2);
    StartForeground(ServiceRunningNotifID, notification);
    return StartCommandResult.Sticky;
}
  public override void OnDestroy()
{
    base.OnDestroy();
}
public override bool StopService(Intent name)
{
    return base.StopService(name);
}
}

this it the service file uploadProgreService.cs below is the android manifest file

 <?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="46" android:versionName="2.5.2" package="com.OIA.oia_agent.dev" android:installLocation="preferExternal">
<uses-sdk android:minSdkVersion="23" android:targetSdkVersion="34" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.CALL_PRIVILEGED" />
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
<uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.INSTANT_APP_FOREGROUND_SERVICE" />
<queries>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="http" />
    </intent>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="https" />
    </intent>
</queries>
<application android:allowBackup="false" android:label="OnSite ImmoAgent" android:icon="@mipmap/iconred" android:largeHeap="true" android:requestLegacyExternalStorage="true" android:networkSecurityConfig="@xml/network_security_config">
    <activity android:exported="true" android:name=".MyActivity" android:screenOrientation="portrait" />
    <uses-library android:name="org.apache.http.legacy" android:required="false" />
    <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />
    <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="${applicationId}" />
        </intent-filter>
    </receiver>
    <provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true">
        <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@drawable/file_paths1"></meta-data>
    </provider>
    <provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true">
        <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@drawable/file_paths1"></meta-data>
    </provider>
    <meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/oiahomered" />
    <meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/notificationcolor" />
</application>
</manifest>

Solution

  • You can set the foreground service type using the ServiceAttribute.

    [Service(Name = "full.namespace.in.lowercase.LocationTrackingService", ForegroundServiceType = Android.Content.PM.ForegroundService.TypeLocation, Enabled = true)]
    public class LocationTrackingService : Service
    {
    }