Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Foreground Service Demo doesn't work Anymore on Android 11 #325

Open
Auto72 opened this issue Jul 7, 2021 · 3 comments
Open

Foreground Service Demo doesn't work Anymore on Android 11 #325

Auto72 opened this issue Jul 7, 2021 · 3 comments

Comments

@Auto72
Copy link

Auto72 commented Jul 7, 2021

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

  1. Get the source to your local PC
  2. Run it on Android 11
  3. Click the START SERVICE button.

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)

1

@Auto72
Copy link
Author

Auto72 commented Jul 13, 2021

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);
 }

@dfcowan
Copy link

dfcowan commented Jul 7, 2022

Even after making the change suggested by @Auto72, I am getting a NullReferenceException on the call to StartForeground

@rbdavison
Copy link

These errors are fixed in Pull Request #346

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants