notification count isn't counting

2.2k Views Asked by At

I have the following code.... as the notification comes...it's always showing "1" and isn't counting if there are more notifications.... what i'm doing wrong?

I'm going to the code started from:

public class ActionSendSMS extends Activity {

private static final int NOTIFY_ME_ID=5476;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.actionsendsms);

    givenotification(getBaseContext());
    finish();
}

.... ....

public void givenotification(Context context){


    //Get the notification manager
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager nm = (NotificationManager)context.getSystemService(ns);

    //Create Notification Object
    int count=1;
    int icon = R.drawable.red_ball;
    CharSequence tickerText = "Nice message!";
    long when = System.currentTimeMillis();
    final Notification notify = new Notification(icon, tickerText, when);

    notify.flags |= Notification.DEFAULT_SOUND;
    notify.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
    notify.flags |= Notification.FLAG_AUTO_CANCEL;

    notify.number += count;

    //Set Notification send options
    Intent intent = new Intent(context, ActionNotifySendMessage.class);

    PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
    notify.setLatestEventInfo(context, "Message Alert", tickerText, pi);

    nm.notify(NOTIFY_ME_ID, notify);
}
2

There are 2 best solutions below

2
On BEST ANSWER

You set your count like count=1 and after you increment notify.number by 1 ??? I never see you increment your counter itself...

You could try to make it static as member and increment it everytime like this:

public class ActionSendSMS extends Activity {

    private static final int NOTIFY_ME_ID=5476;
    private static int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actionsendsms);

        sendSMS();
        givenotification(getBaseContext());
        finish();
    }

    public void givenotification(Context context){


    //Get the notification manager
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager nm = (NotificationManager)context.getSystemService(ns);

    //Create Notification Object
    count++;
    int icon = R.drawable.red_ball;
    CharSequence tickerText = "PhoneFinder send GPS-message!";
    long when = System.currentTimeMillis();
    final Notification notify = new Notification(icon, tickerText, when);

    notify.flags |= Notification.DEFAULT_SOUND;
    notify.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
    notify.flags |= Notification.FLAG_AUTO_CANCEL;

    notify.number += count;

    //Set Notification send options
    Intent intent = new Intent(context, ActionNotifySendMessage.class);

    PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
    notify.setLatestEventInfo(context, "DroidFinder Alert", tickerText, pi);

    nm.notify(NOTIFY_ME_ID, notify);
}
0
On

Make int count=1; global to Activity as you have declared NOTIFY_ME_ID. You are declaring count inside givenotification() so its initializing with 1 always.