如何在android中组织推送?

时间:2015-08-12 12:09:16

标签: android notifications push-notification

我已经在android中实现了推送通知,它完全按照需要完成,但只有一个问题是它显示为个体,我希望它在分组中,意味着如果有5个通知,它应该显示“5个消息”不是通知列表(当前即将发布),我的代码是belo,我找到了风格可以做到这一点的解决方案,并尝试了但没有改变,

@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());

    }

1 个答案:

答案 0 :(得分:0)