Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NotificationCompat with API 26

I dont see any information about how to use NotificationCompat with Android O's Notification Channels

I do see a new Constructor that takes a channelId but how to take a Compat notification and use it in a NotificationChannel since createNotificationChannel takes a NotificationChannel object

like image 485
tyczj Avatar asked Jun 08 '17 19:06

tyczj


People also ask

What is NotificationCompat?

NotificationCompat.Action. Structure to encapsulate a named action that can be shown as part of this notification.


1 Answers

Create the NotificationChannel only if API >= 26

public void initChannels(Context context) {     if (Build.VERSION.SDK_INT < 26) {         return;     }     NotificationManager notificationManager =             (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);     NotificationChannel channel = new NotificationChannel("default",                                                           "Channel name",                                                           NotificationManager.IMPORTANCE_DEFAULT);     channel.setDescription("Channel description");     notificationManager.createNotificationChannel(channel); } 

And then just use:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, "default"); 

So your notifications are working with both API 26 (with channel) and below (without).

like image 183
stankocken Avatar answered Sep 23 '22 06:09

stankocken