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

Exception "Bad notification" running sample

Open LeonSweden opened this issue 4 years ago • 5 comments

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 avatar Mar 31 '20 12:03 LeonSweden

@LeonSweden did you find a solution to this? even im facing the same issue. Please help!

Deba22 avatar Jul 19 '20 08:07 Deba22

I'm facing the same issue. any help!

amr-moomen avatar Jul 29 '20 09:07 amr-moomen

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.

Deba22 avatar Jul 29 '20 10:07 Deba22

NotificationManager.Importance none is obsolete and will be removed, use NotificationImportance.None instead

tatapuchi avatar Dec 23 '20 12:12 tatapuchi

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

johnjore avatar Jul 18 '21 09:07 johnjore