如何在 android 中对推送通知进行分组?

How to group pushnotifications in android?

我在android中实现了推送通知,它完全符合需要,但唯一的问题是它显示为单独的,我希望它在分组中,意味着如果有 5 个通知,它应该显示“5 条消息”而不是通知列表(当前[即将到来)],我的代码如下所示,我找到了 style 可以做到这一点的解决方案,并且尝试过但没有改变,

代码

@SuppressWarnings("null")
    public void sendNotification(String title, String description) {
        Random random = new Random();
        int id = random.nextInt(50);
        NotificationCompat.Builder myNotification = null;
        NotificationManager notificationManager = null;

        notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent myIntent = new Intent(context, ActivityHome.class);
        myIntent.putExtra("push", "push");
        TaskStackBuilder taskbuiler = TaskStackBuilder.create(this);
        taskbuiler.addParentStack(ActivityHome.class);

        taskbuiler.addNextIntent(myIntent);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        myNotification = new NotificationCompat.Builder(context);
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        String[] events = new String[6];
        // Sets a title for the Inbox in expanded layout
        inboxStyle.setBigContentTitle("Event tracker details:");

        // Moves events into the expanded layout
        for (int i = 0; i < events.length; i++) {

            inboxStyle.addLine(events[i]);
        }
        // Moves the expanded layout object into the notification object.
        myNotification.setStyle(inboxStyle);

        myNotification.setStyle(new NotificationCompat.InboxStyle());
        myNotification.setContentTitle(title);
        myNotification.setContentText(description);
        myNotification.setContentIntent(pendingIntent);
        myNotification.setDefaults(Notification.DEFAULT_SOUND);
        myNotification.setAutoCancel(true);
        myNotification.setSmallIcon(R.drawable.app_icon);
        myNotification.build();

        notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(id, myNotification.build());

    }

看看这个:

https://developer.android.com/training/wearables/notifications/stacks.html

也许这有帮助。