Giter Club home page Giter Club logo

Comments (2)

dfcowan avatar dfcowan commented on June 2, 2024

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

from monodroid-samples.

rbdavison avatar rbdavison commented on June 2, 2024

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

from monodroid-samples.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.