Hi i want to show all the notification in a single view .. and want to update number of notification in status bar ... its updating all info but showing number always 1.. please
This is my code, and it works. I have only tested on old Android versions thou. I suspect on newer versions the "number" badge has been made invisible, but I haven't had the chance to test it.
void notifyMsgReceived(String senderName, int count) {
int titleResId;
String expandedText, sender;
// Get the sender for the ticker text
// i.e. "Message from <sender name>"
if (senderName != null && TextUtils.isGraphic(senderName)) {
sender = senderName;
}
else {
// Use "unknown" if the sender is unidentifiable.
sender = getString(R.string.unknown);
}
// Display the first line of the notification:
// 1 new message: call name
// more than 1 new message: <number of messages> + " new messages"
if (count == 1) {
titleResId = R.string.notif_oneMsgReceivedTitle;
expandedText = sender;
}
else {
titleResId = R.string.notif_missedCallsTitle;
expandedText = getString(R.string.notif_messagesReceivedTitle, count);
}
// Create the target intent
final Intent intent = new Intent(this, TargetActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
final PendingIntent pendingIntent =
PendingIntent.getActivity(this, REQUEST_CODE_MSG_RECEIVED, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Build the notification
Notification notif = new Notification(
R.drawable.ic_notif_msg_received, // icon
getString(R.string.notif_msgReceivedTicker, sender), // tickerText
System.currentTimeMillis()); // when
notif.setLatestEventInfo(this, getText(titleResId), expandedText, pendingIntent);
notif.number = count;
notif.flags = Notification.FLAG_AUTO_CANCEL;
// Show the notification
mNotificationMgr.notify(NOTIF_MSG_RECEIVED, notif);
}
It is also easy to update the notification later on: you just have to call the method again with new values. The number will be displayed in the notification icon badge if and only if it was greater than zero when the notification was created.
Similarily, the number badge won't be hidden (the number will, thou) if you set the number to a number that is less than 1. Maybe clearing the notification before re-showing it could fix it.
You have to keep track of the count. You could extend the Application class:
public class AppName extends Application {
private static int pendingNotificationsCount = 0;
@Override
public void onCreate() {
super.onCreate();
}
public static int getPendingNotificationsCount() {
return pendingNotificationsCount;
}
public static void setPendingNotificationsCount(int pendingNotifications) {
pendingNotificationsCount = pendingNotifications;
}
}
And you should modify the onReceive:
@Override
public void onReceive(Context context, Intent intent) {
...
int pendingNotificationsCount = AppName.getPendingNotificationsCount() + 1;
AppName.setPendingNotificationsCount(pendingNotificationsCount);
notification.number = pendingNotificationsCount;
...
}
And you could reset the count when the user open the notification:
AppName.setPendingNotificationsCount(0);
You have to keep track of the count. The increment that you're trying to perform on notif.number isn't working, since that state is unavailable (i.e. notif.number is always 0, then you increment it to 1). Keep track of number somewhere in your app(shared preferences, perhaps), and increment and store it there, then when you build the notification, set
notif.number = myStoredValueForWhatShouldShowInNumber;
Give that a try.