monodroid-samples icon indicating copy to clipboard operation
monodroid-samples copied to clipboard

ForegroundServiceDemo crashes with null reference exception when clicking start service

Open Hallupa opened this issue 2 years ago • 2 comments

Issue description

When starting the ForegroundServiceDemo on my Samsung S10 using the Visual Studio debugger attached, it crashes when clicking 'start service'

The exception is a null reference exception with the stack trace:

at Android.Runtime.JNINativeWrapper._unhandled_exception (System.Exception e) [0x0000e] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12 at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLII_I (_JniMarshal_PPLII_I callback, System.IntPtr jnienv, System.IntPtr klazz, System.IntPtr p0, System.Int32 p1, System.Int32 p2) [0x00022] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:221 at (wrapper native-to-managed) Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLII_I(intptr,intptr,intptr,int,int)

The line it fails on is TimeStampService.cs line 155, on the code: var pendingIntent = PendingIntent.GetActivity(this, 0, notificationIntent, PendingIntentFlags.UpdateCurrent);

Steps to reproduce the issue

  1. Run ForegroundServiceDemo on S10 device with Visual Studio debugger attached
  2. Click Start Service

What's the expected result?

Service starts

What's the actual result?

Crash

Hallupa avatar Jun 20 '22 23:06 Hallupa

I am seeing the same thing when running the ForegroundServiceDemo in the emulator.

dfcowan avatar Jul 07 '22 21:07 dfcowan

I found I had to add the FOREGROUND_SERVER permission. Either to the AndroidManifest.xml <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> of to a source file (Often AssemblyInfo.cs or MainActivity.cs) outside the namespace declaration [assembly: UsesPermission(Android.Manifest.Permission.ForegroundService)]

The error that is then reported is **Android.Util.AndroidRuntimeException:** 'Bad notification for startForeground'

To get the project to actually build I had to drop the project from Android 12.1 (API 32) down to Android 12.0 (API 31) since API 32 isn't in the Approved List (Recommended) repository. I guess it'll be updated in time. But these demos really should be targeting the Recommended repository.

Edit: The remaining error 'Bad notification for startForeground' can be resolved by changing the RegisterForegroundService() method as follows

        void RegisterForegroundService()
        {
            string channelId = Resources.GetString(Resource.String.app_name) + "_" + Constants.SERVICE_RUNNING_NOTIFICATION_ID.ToString();
            NotificationChannel chan = new NotificationChannel(channelId, Resources.GetString(Resource.String.app_name), NotificationImportance.None)
            {
                LockscreenVisibility = NotificationVisibility.Private
            };
            NotificationManager service = GetSystemService(Context.NotificationService) as NotificationManager;
            service.CreateNotificationChannel(chan);

            Notification notification = new Notification.Builder(this, channelId)
                .SetContentTitle(Resources.GetString(Resource.String.app_name))
                .SetContentText(Resources.GetString(Resource.String.notification_text))
                .SetSmallIcon(Resource.Drawable.ic_stat_name)
                .SetContentIntent(BuildIntentToShowMainActivity())
                .SetOngoing(true)
                .SetChannelId(channelId)
                .AddAction(BuildRestartTimerAction())
                .AddAction(BuildStopServiceAction())
                .Build();

            // Enlist this instance of the service as a foreground service
            StartForeground(Constants.SERVICE_RUNNING_NOTIFICATION_ID, notification);
        }

rbdavison avatar Jul 28 '22 06:07 rbdavison