8

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 tell me how to solve it...

@Override
public void onReceive(Context context, Intent intent)
{
    //Random randGen = new Random();
    //int notify_id = randGen.nextInt();
    NotificationManager notificationManager = (NotificationManager)
        context.getSystemService(Activity.NOTIFICATION_SERVICE);
    String title = intent.getStringExtra(TableUtils.KEY_TITLE);
    String occasion = intent.getStringExtra(TableUtils.KEY_OCCASION);
    Notification notification = 
        new Notification(R.drawable.icon, "Love Cardz" , 
                         System.currentTimeMillis());
    // notification.vibrate = new long[]{100,250,300,330,390,420,500};
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.number+=1;
    Intent intent1 = new Intent(context, ThemesBrowserActivity.class);
    PendingIntent activity = 
        PendingIntent.getActivity(context, 1 , intent1, 
                                  PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(context, occasion, title, activity);
    notificationManager.notify(1, notification);
}
DkPathak
  • 452
  • 1
  • 4
  • 16

3 Answers3

19

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);
Andrea Motto
  • 1,540
  • 21
  • 38
  • 13
    Pretty ridiculous how the framework doesn't have a simple, `getNotifications(int id)` call to simply check this... – b.lyte Jul 10 '14 at 21:48
  • 5
    Unfortunately if the application has been killed, the counter resets... Probably should save to SharedPreference for persistence – xnagyg May 11 '15 at 11:54
  • 1
    How can you know when the notification was opened to reset the counter...? – Micro Mar 11 '16 at 01:59
  • 1
    @MicroR, you need to put the Activity class as part of your Intent into your `PendingIntent`. http://stackoverflow.com/questions/7184963/how-to-set-click-listener-for-notification – Andrea Motto Mar 11 '16 at 02:23
  • So what you are saying is that in the onCreate() or something of the Activity, I should reset the counter? I was hoping there was a way to do it not in that way. – Micro Mar 11 '16 at 15:18
4

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.

rock3r
  • 701
  • 4
  • 14
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.

Travis
  • 3,737
  • 1
  • 24
  • 24