• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Where do you set up notification channels?

Flizzy

Lurker
Hello,
I try to get some opinions on where to set up notification channels in Android.
I see 2 main approaches: Either in an helper class, or in the application class.
I think the 2nd approach is better, because it doesn't execute the channel creation code unnecessarily often.

Do you have any examples on notification channels that are created in the application class?
 
Yea I know that Helper class, but I think there are some things wrong with it.
It doesn't use the Compat classes as recommended on the Android training pages. They use Notification.Builder instead of NotificationCompat.Builder and they use the NotificationManger to display the notification, while in their tutorials they recommend to use the NotificationManagerCompat, which has some internal checks for backwards compatibility.
This is the main part that makes using the helper class so awkward. Instead of 1 NotificationManager, you have to handle 2 NotificationManagers, because the Compat version can not create channels.
Also they recommend to create these channels as soon as the application starts, and their helper class tries to create the notification channels unnecesseraily often. It doesn't throw an error, because the system will just update the old channels, but it's still a lot of code that gets executed everytime I create an object of that helper class, which can be quite often.

I think this helper class is only meant to show how to use notifications on 26+, but they don't take care of any backwards compatiblity, so I would not use it in an app like this.
 
Back
Top Bottom