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

Exception "Bad notification" running sample #313

Open
LeonSweden opened this issue Mar 31, 2020 · 5 comments
Open

Exception "Bad notification" running sample #313

LeonSweden opened this issue Mar 31, 2020 · 5 comments

Comments

@LeonSweden
Copy link

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.

@Deba22
Copy link

Deba22 commented Jul 19, 2020

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

@amr-moomen
Copy link

I'm facing the same issue. any help!

@Deba22
Copy link

Deba22 commented Jul 29, 2020

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.

@tatapuchi
Copy link

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

@johnjore
Copy link

johnjore commented Jul 18, 2021

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

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

5 participants