Closed Auto72 closed 4 months ago
This will fix the issue.
void RegisterForegroundService()
{
NotificationChannel chan = new NotificationChannel("my_service_urgent", "My Channel", NotificationImportance.None);
chan.EnableVibration(true);
chan.LockscreenVisibility = NotificationVisibility.Secret;
NotificationManager notificationManager = GetSystemService(NotificationService) as NotificationManager;
notificationManager.CreateNotificationChannel(chan);
var notification = new Notification.Builder(this, "my_service_urgent")
.SetContentTitle(Resources.GetString(Resource.String.app_name))
.SetContentText(Resources.GetString(Resource.String.notification_text))
.SetSmallIcon(Resource.Drawable.ic_stat_name)
.SetContentIntent(BuildIntentToShowMainActivity())
.SetOngoing(true)
.AddAction(BuildRestartTimerAction())
.AddAction(BuildStopServiceAction())
.Build();
StartForeground(Constants.SERVICE_RUNNING_NOTIFICATION_ID, notification);
}
Even after making the change suggested by @Auto72, I am getting a NullReferenceException on the call to StartForeground
These errors are fixed in Pull Request #346
Sample link
https://docs.microsoft.com/en-us/samples/xamarin/monodroid-samples/applicationfundamentals-servicesamples-foregroundservicedemo/
Issue description
Foreground Service Demo doesn't work Anymore on Android 11 The permission "FOREGROUND_SERVICE" needs to be added to the manifest.
When you compile the project, the following warning are listed:
Build started... 1>------ Build started: Project: ForegroundServiceDemo, Configuration: Debug Any CPU ------ 1>C:\Users\username\Downloads\Xamarin_Android_Foreground_Service_Sample\TimestampService.cs(32,14,32,27): warning CS0618: 'Handler.Handler()' is obsolete: 'deprecated' 1>C:\Users\username\Downloads\Xamarin_Android_Foreground_Service_Sample\TimestampService.cs(128,23,128,53): warning CS0618: 'Notification.Builder.Builder(Context?)' is obsolete: 'deprecated' 1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Xamarin\Android\Xamarin.Android.Legacy.targets(311,5): warning XA0122: Assembly 'Xamarin.Android.Support.Annotations' is using a deprecated attribute '[assembly: Java.Interop.DoNotPackageAttribute]'. Use a newer version of this NuGet package or notify the library author. 1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Xamarin\Android\Xamarin.Android.Legacy.targets(396,5): warning XA0125: 'ServicesDemo3.pdb' is using a deprecated debug information level. 1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Xamarin\Android\Xamarin.Android.Legacy.targets(396,5): warning XA0125: Set the debugging information to Portable in the Visual Studio project property pages or edit the project file in a text editor and set the 'DebugType' MSBuild property to 'portable' to use the newer, cross-platform debug information level. 1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Xamarin\Android\Xamarin.Android.Legacy.targets(396,5): warning XA0125: If this file comes from a NuGet package, update to a newer version of the NuGet package or notify the library author. ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
So obsolete methods are used.
Steps to reproduce the issue
What's the expected result?
See the example working.
What's the actual result?
If get the following error:
[AndroidRuntime] Shutting down VM [AndroidRuntime] FATAL EXCEPTION: main [AndroidRuntime] Process: com.xamarin.xample.foregroundservicedemo, PID: 8524 [AndroidRuntime] android.app.RemoteServiceException: Bad notification for startForeground [AndroidRuntime] at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2188) [AndroidRuntime] at android.os.Handler.dispatchMessage(Handler.java:107) [AndroidRuntime] at android.os.Looper.loop(Looper.java:237) [AndroidRuntime] at android.app.ActivityThread.main(ActivityThread.java:8167) [AndroidRuntime] at java.lang.reflect.Method.invoke(Native Method) [AndroidRuntime] at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496) Android.Util.AndroidRuntimeException: 'Bad notification for startForeground'
Additional details / screenshot (Optional)