Saty
Saty

Reputation: 2621

Notification inbox style however gets updated when a notification comes

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

Upvotes: 2

Views: 5132

Answers (2)

Guillermo Segalerba
Guillermo Segalerba

Reputation: 31

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;

Upvotes: 1

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

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

Upvotes: 0

Related Questions