Naga
Naga

Reputation: 2021

Update Inbox style Notification Like gmail

I want to use inbox style notification, and once notification is still showing in notification status bar then it should append to the existing notification like gmail.

But I don't know how to detect that notification is showing in the status bar, Is there any way to get the notification id

Is ther any way to know that notification generated by my application is already displayed and just update it with +1 more(Inbox style)

What I thought :-

I thought I can store the notification id in shared prefrences and I will pass the pending intent which will start a intent service which will clear the notification is stored in shared prefrences and during notification posting I will check the notification id in prefrences If it is not cleared then I will update it

Does any one have any better idea ?

Upvotes: 2

Views: 4844

Answers (3)

g4gaj
g4gaj

Reputation: 85

you can use the static variables to keep the track of notification ID and for appending the notification i mean for stacked notification also you can keep a number in static variables...

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

            // Moves events into the big view
            for (int i = 0; i < extrasList.size(); i++) {
                inboxStyle.addLine(extrasList.get(i).getString(mString));
            }

            if (number >= 8) {
                inboxStyle.setSummaryText("+" + (number - 7) + " more reply(s)");
            } else {
                inboxStyle.setSummaryText(contentText);
            }
            mBuilder.setStyle(inboxStyle);
            mBuilder.setNumber(number);
            mBuilder.setContentIntent(contentIntent);
            mBuilder.setAutoCancel(true);
            mNotificationManager.notify(Integer.parseInt(type), mBuilder.build());

Upvotes: 0

RaphMclee
RaphMclee

Reputation: 1623

Do not save it in the preferences. Just use a constant value as the Notification ID.

static final int MY_NOTIFICATION_ID = 1;

As the ID's are unique per application you can use the number you want. Then use it when notifying the NotifiactionManager. Use the same code to update your notifiaction.

NotificationManager.notify(MY_NOTIFICATION_ID , notification);

Upvotes: 1

JanBo
JanBo

Reputation: 2933

I think you should have everything on this link: http://developer.android.com/training/notify-user/managing.html

It like you said, you need to know the notification id in order to update it. Using Shared Preferences is an easy way to do it since it only a few lines of code to do everything,

Your idea good, clear the preferences file when the user clicks on the notification.

Upvotes: 1

Related Questions