How to Create a Group of Foreground Notifications?

Viewed 389

I am working on an Android APP which has two foreground services and wants to group the notification into one Group. It is possible to do this? I have used NotificationCompat.Builder setGroup(String groupKey) to enable Group Notification, but it does not work.

NotificationHelper.kt

enum ChannelType{ ID1, ID2} 

fun getNotificationChannelID(context: Context, channelType: ChannelType) : String? {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val channelID = channelType.channelID
                val description = channelType.reourceID
                val channel = NotificationChannel(channelID, description,
                        NotificationManager.IMPORTANCE_HIGH);
                val mNotificationManager = mContext.getSystemService(Context.NOTIFICATION_SERVICE)
                        as android.app.NotificationManager
                mNotificationManager.createNotificationChannel(channel)
                return channelID
            }
            return null;
        }

fun createBuilder(context: Context, channelId: String, title: String, content: String,
                          ongoing: Boolean, autoCancel: Boolean,
                          style: NotificationCompat.Style, @DrawableRes icon: Int,
                          priority: Int, vibratePattern: LongArray): NotificationCompat.Builder {

           val builder = NotificationCompat.Builder(context, channelId)
            builder.setSmallIcon(icon)
                    .setContentTitle(title)
                    .setContentText(content)
                    .setAutoCancel(autoCancel)
                    .setOngoing(ongoing)
                    .setGroup(GROUP_KEY_NOTIF)
                    .setStyle(style)
                    .setGroupSummary(true)
            return builder

        }
    }

from Service 1

onStartCommand() {
    var channelID = getNotificationChannelID(context, ChannelType.ID1)
   startForeground(NOTIFICATION_ID1,createBuilder(context,
                                              channelID,
                                              "title",
                                              "description",
                                              ongoing = false,
                                              autocancel = true,
                                              someIcon,
                                              BigTextStyle,
                                               priority, vibratePattern )
                                               .build());
}   

from service 2

onStartCommand() {
    var channelID = getNotificationChannelID(context, ChannelType.ID1)
   startForeground(NOTIFICATION_ID1,createBuilder(context,
                                              channelID,
                                              "title",
                                              "description",
                                              ongoing = false,
                                              autocancel = true,
                                              someIcon,
                                              BigTextStyle,
                                              priority, vibratePattern)
                                               .build());
}   

Am I missing anything? I have also set ongoing to false, still no change in result.

Thanks in advance.

0 Answers
Related