多条消息的更新通知

4
以下是生成应用程序中新消息通知的代码:

代码

 private static void generateNotification(Context context, String message) {
        int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();
        int counter = 0;
        counter++;

        NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);

        String title = context.getString(R.string.app_name);

        Intent notificationIntent = new Intent(context, MessageActivity.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(counter, notification);

    }

当我收到多个消息时,会有多个通知。

例如

假设我收到3条新消息,则会出现3个单独的通知。但我只想要多条消息的单个通知。当我收到多条消息时,通知应该像我们在WhatsApp中看到的那样更新。我该怎么做?


将唯一标识符设置为您的通知。 - M D
没问题,伙计,这样做很好。但是我希望之前的信息还能保留在那里... - Looking Forward
好的。那你知道WhatsApp的通知是如何工作的吗?有什么想法吗? - Looking Forward
抱歉,我不知道这个。 - M D
1
顺便说一下,你的问题很好。祝你好运! - M D
显示剩余7条评论
1个回答

1
如果您有多个消息,例如在JsonArray中,您可以循环遍历该数组并为数组中的每个项目创建一个通知。在循环中创建的通知可以通过设置相同的Unique_ID进行更新。如果您想显示一个数字来显示此通知中的消息数量(即未读消息的数字),请创建一个计数器,并在循环中将其增加1。根据目标Android-SDK使用NotificationCompat.InboxStyle(需要android.v4.support库)或Notification.InboxStyle来显示多行通知,并使用InboxStyle.addLine将循环中每个数组项的内容添加到其中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接