Notification inbox style however gets updated when

2019-07-29 09:24发布

I am trying to make a notification style where I want all notification would under one hood and they will be shown line wise. If a new notification comes, it should be under the second latest. The list of notification should be of 5, that means only latest 5 will be displayable.

I am using this below code for this, I don't know how to achieve this, I tried my hands StackBuilder too however I got that, this would work from higher API than I am using one now.

NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, GcmActivity.class);
notificationIntent.putExtra("title", messagetype);
notificationIntent.putExtra("message", msg);
PendingIntent intent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
        | Intent.FLAG_ACTIVITY_SINGLE_TOP);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("MY MESSENGER").setContentText("MESSAGES");
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle("MESSAGES" + " Details");

    inboxStyle.addLine(messagetype + ":" + msg);


mBuilder.setStyle(inboxStyle);
mBuilder.setContentIntent(intent);
mBuilder.setAutoCancel(true);
notificationManager.notify(Integer.parseInt("0"), mBuilder.build());

Target Design

I know I can do this by adding any number of line while creating

NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();  

However I want that when ever my GCMIntentService will be called, this list will be updated.

I tried the work using this below code as well however that did not work either

 NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, GcmActivity.class);
notificationIntent.putExtra("title", messagetype);
notificationIntent.putExtra("message", msg);
PendingIntent intent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
        | Intent.FLAG_ACTIVITY_SINGLE_TOP);



NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(  
        this).setAutoCancel(true)  
        .setContentTitle("MY MESSENGER")  
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentText("MESSAGES");  

 NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();  

for(int j= 0; j<listMessage.size(); j++)
  inboxStyle.addLine(listMessage.get(j));  
mBuilder.setStyle(inboxStyle);
mBuilder.setContentIntent(intent);
notificationManager.notify(0, mBuilder.build());

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-07-29 09:39

I had the same issue and fixed it by persisting the notifications on the database to load them when a new one arrives, and delete them on click or delete.

DatabaseHelper dbHelper = new DatabaseHelper(this);
    Cursor notifications = dbHelper.getAllNotifications();
    NotificationCompat.Builder mBuilder = extractNotifications(title, msg, contentIntent, notifications);
    dbHelper.insertNotification(title + ": " + msg);

private NotificationCompat.Builder extractNotifications(String title, String msg, PendingIntent contentIntent, Cursor notifications) {
    NotificationCompat.Builder mBuilder;
        mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_icon)
                        .setContentTitle(title)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(NOTIFICAITONS))
                        .setContentText(msg)
                        .setAutoCancel(true)
                        .setLights(Color.WHITE, 1000, 5000)
                        .setDefaults(Notification.DEFAULT_VIBRATE |
                                Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS)
                        .setContentIntent(contentIntent);

        NotificationCompat.InboxStyle inboxStyle =
                new NotificationCompat.InboxStyle();

        inboxStyle.setBigContentTitle(NOTIFICAITONS);
        while (notifications.moveToNext())
        {
            inboxStyle.addLine(notifications.getString(notifications.getColumnIndex(DatabaseHelper.NOTIFICATION_MESSAGE)));
        }
        inboxStyle.addLine(title + ": " + msg);
        mBuilder.setStyle(inboxStyle);
    return mBuilder;
查看更多
Deceive 欺骗
3楼-- · 2019-07-29 09:40

You need to do that yourself, by assigning own ID to your notification you will be able to update it later. And you need to build the notification yourself too, by concatenating messages

查看更多
登录 后发表回答