Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to update notification number

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);
}
like image 550
DkPathak Avatar asked Sep 23 '12 11:09

DkPathak


People also ask

How do I change the number of notifications?

1 Tap Notification Settings on the notification panel or tap the Settings app. 2 Tap Notifications. 3 Tap App icon badges. 4 Select Show with number.

How can I set notification count in Android?

If you want to change badge with number, you can be changed in NOTIFICATION SETTING on the notification panel or Settings > Notifications > App icon badges > Select Show with number.


2 Answers

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);
like image 133
Andrea Motto Avatar answered Nov 15 '22 21:11

Andrea Motto


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.

like image 34
rock3r Avatar answered Nov 15 '22 20:11

rock3r