通知中的 setgroup() 不起作用 [英] setgroup() in notification not working

查看:28
本文介绍了通知中的 setgroup() 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm tring to create notification group, this is my code:

 // Build the notification, setting the group appropriately
 Notification notif = new NotificationCompat.Builder(getApplicationContext())
          .setContentTitle("New mail from " + 1)
          .setContentText("cv")
          .setSmallIcon(R.drawable.rh_logo)
          .setStyle(new NotificationCompat.InboxStyle()
            .addLine("Alex Faaborg   Check this out")
            .addLine("Jeff Chang   Launch Party")
            .setBigContentTitle("2 new messages")
            .setSummaryText("johndoe@gmail.com"))
          .setGroup(GROUP_KEY_EMAILS)
          .setGroupSummary(true)
          .build();

 // Issue the notification



 NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
 notificationManager.notify(++NOTIFICATION_ID, notif);

When I run the app, and send notification messages, they do not show in group. Can someone explain me what I need to change?

解决方案

You have to create a group notification before creating your custom notification. Just like this:

NotificationCompat.Builder groupBuilder =
            new NotificationCompat.Builder(context)
                    .setContentTitle(title)
                    .setContentText(content)
                    .setGroupSummary(true)
                    .setGroup("GROUP_1")
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(content))
                    .setContentIntent(pendingIntent);

Do not forget setGroupSummary to true.

Then create your child notification which group value is same to groupBuilder's value。Here is "GROUP_1".

NotificationCompat.Builder builder =
            new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic_stat_communication_invert_colors_on)
                    .setContentTitle(title)
                    .setContentText(content)
                    .setGroup("GROUP_1")
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(content))
                    .setContentIntent(pendingIntent)

Finally use NoticationManagerCompat to notify them.

    NotificationManagerCompat manager = NotificationManagerCompat.from(context);
    manager.notify(GROUP_ID, groupBuilder.build());
    manager.notify(id, builder.build());

这篇关于通知中的 setgroup() 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆