monodroid-samples
monodroid-samples copied to clipboard
Exception "Bad notification" running sample
After adding to manifest required but missing permission:
I get this error when pressing START SERVICE:
Android.Util.AndroidRuntimeException: 'Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x42 color=0x00000000 actions=2 vis=PRIVATE)'
I suggest that you do rewrite this sample to current Android version.
@LeonSweden did you find a solution to this? even im facing the same issue. Please help!
I'm facing the same issue. any help!
I was able to get it working by using the below code while registering for a foreground service:
String NOTIFICATION_CHANNEL_ID = "com.test.testapp";
String channelName = "test app service";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.ImportanceNone);
chan.LockscreenVisibility = NotificationVisibility.Private;
NotificationManager manager = (NotificationManager)GetSystemService(Context.NotificationService);
manager.CreateNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.SetOngoing(true)
.SetSmallIcon(Resource.Drawable.ic_stat_name)
.SetContentTitle("App is running in background")
.SetPriority(1)
.SetCategory(Notification.CategoryService)
.Build();
StartForeground(Constants.SERVICE_RUNNING_NOTIFICATION_ID, notification);
I hope this helps you.
NotificationManager.Importance none is obsolete and will be removed, use NotificationImportance.None instead
Combining the two posts above, this is my replacement (its half baked, and half implemented in my app, hence the namespace changes etc), but my app does not crash at startup anymore.
void RegisterForegroundService()
{
String NOTIFICATION_CHANNEL_ID = "no.jore.hajk";
String channelName = "test app service";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationImportance.None)
{
LockscreenVisibility = NotificationVisibility.Private
};
NotificationManager manager = (NotificationManager)GetSystemService(Context.NotificationService);
manager.CreateNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.SetOngoing(true)
.SetSmallIcon(Resource.Drawable.route)
.SetContentTitle(Resources.GetString(Resource.String.app_name))
.SetContentText(Resources.GetString(Resource.String.notification_text))
.SetContentIntent(BuildIntentToShowMainActivity())
.SetPriority(1)
.SetOngoing(true)
.SetCategory(Notification.CategoryService)
.AddAction(BuildStopServiceAction())
.Build();
// Enlist this instance of the service as a foreground service
StartForeground(PrefsActivity.SERVICE_RUNNING_NOTIFICATION_ID, notification);
}
Updating the sample to a working version would be nice