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

ForegroundServiceDemo crashes with null reference exception when clicking start service #335

Open
Hallupa opened this issue Jun 20, 2022 · 2 comments

Comments

@Hallupa
Copy link

Hallupa commented Jun 20, 2022

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

@dfcowan
Copy link

dfcowan commented Jul 7, 2022

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

@rbdavison
Copy link

rbdavison commented Jul 28, 2022

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

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