Bad notification for startForeground in Android app

4

I am developing a service using Xamarin Android 3.5. Our app targets Android 8.1 (API 27 - Oreo). I want the service to run as a foreground service. However I am getting the following error when I run the service.

Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=1 contentView=null vibrate=null sound=null defaults=0x0 flags=0x42 color=0x00000000 vis=PRIVATE)

Here is the code for the service.

public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
  base.OnStartCommand(intent, flags, startId);
  var context = Application.Context;
  const int pendingIntentId = 0;
  PendingIntent pendingIntent = PendingIntent.GetActivity(context, pendingIntentId, intent, PendingIntentFlags.OneShot);
  var notification = new NotificationCompat.Builder(context)
    .SetContentTitle("Testing")
    .SetContentText("location tracking has begun.")
    .SetSmallIcon(Resource.Drawable.icon)
    .SetContentIntent(pendingIntent)
    .SetOngoing(true)
    .Build();
    // Enlist this instance of the service as a foreground service
    const int Service_Running_Notification_ID = 935;
    StartForeground(Service_Running_Notification_ID, notification);
    return StartCommandResult.NotSticky;
}

I have updated the AndroidManifest.xml with the following.

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

In the MainActivity.cs I have the follwing code which we use to create a notification channel for sending app notifications (and which correctly creates the notification channel).

private void CreateNotificationChannel()
{
  if (Build.VERSION.SdkInt < BuildVersionCodes.O)
  {
    // Notification channels are new in API 26 (and not a part of the
    // support library). There is no need to create a notification 
    // channel on older versions of Android.
    return;
  }
  var channel = new NotificationChannel(ApplicationConstants.ChannelId, ApplicationConstants.ChannelName, NotificationImportance.Default)
  {
    Description = ApplicationConstants.ChannelDescription
  };
  var notificationManager = (NotificationManager)GetSystemService(NotificationService);
  notificationManager.CreateNotificationChannel(channel);
}
android
xamarin
xamarin.forms
xamarin.android
asked on Stack Overflow Jul 15, 2019 by DomBurf

3 Answers

4

invalid channel for service notification

You are creating a notification channel but never assigning it in your NotificationCompat.Builder:

var notification = new NotificationCompat.Builder(context)
   ~~~
   .SetChannelId(ApplicationConstants.ChannelId)
   ~~~

Docs: https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder

answered on Stack Overflow Jul 15, 2019 by SushiHangover
3

For Xamarin.Forms and Xamarin.Android

========Put this code in public override StartCommandResult OnStartCommand ===========

 if (Build.VERSION.SdkInt >= Build.VERSION_CODES.O)
      RegisterForegroundServiceO();
  else { RegisterForegroundService(); }

========================================END=============================

 void RegisterForegroundService()
        {
            var notification = new Notification.Builder(this)
                .SetContentTitle(Resources.GetString(Resource.String.app_name))
                .SetContentText(Resources.GetString(Resource.String.notification_text))
                .SetSmallIcon(Resource.Drawable.icon_userProfile)
                .SetContentIntent(BuildIntentToShowMainActivity())
                .SetOngoing(true)
                .Build();
            const int Service_Running_Notification_ID = 936;
            StartForeground(Service_Running_Notification_ID, notification);
        }


void RegisterForegroundServiceO()
    {
        String NOTIFICATION_CHANNEL_ID = "com.Your.project.id";
        NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Your Channel Name", NotificationManager.ImportanceHigh);

        NotificationManager manager = (NotificationManager)GetSystemService(Context.NotificationService);

        manager.CreateNotificationChannel(chan);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);

        Notification notification= notificationBuilder.SetOngoing(true)
            .SetContentTitle(Resources.GetString(Resource.String.app_name))
            .SetContentText(Resources.GetString(Resource.String.notification_text))
            .SetSmallIcon(Resource.Drawable.icon_userProfile)
            .SetContentIntent(BuildIntentToShowMainActivity())
            .SetOngoing(true)
            .Build();

        const int Service_Running_Notification_ID = 936;
        StartForeground(Service_Running_Notification_ID, notification);
    }

Happy Coding. :-)

answered on Stack Overflow Sep 28, 2019 by Ripdaman Singh
0

Here is a shot solution.

NotificationChannel chan = new NotificationChannel( "my_service_urgent", "My Channel", NotificationImportance.None );
chan.EnableVibration( false );
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();

// SERVICE_RUNNING_NOTIFICATION_ID = 101
// Enlist this instance of the service as a foreground service
StartForeground( Constants.SERVICE_RUNNING_NOTIFICATION_ID, notification );
answered on Stack Overflow May 7, 2020 by M. Hamza Rajput

User contributions licensed under CC BY-SA 3.0