Reputation: 424
I am using notifications in my Android application. Whenever i receive a notification, i am trying to increment the number of unread notifications using notification.number as in the code below.
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon,
"A new notification", System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.number += 1;
Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "This is the title",
"This is the text", activity);
notificationManager.notify(0, notification);
But, no matter how many notifications i receive, the counter is not incrementing. Its always displaying 1 only. I am not getting what is wrong here. Could anybody help me...?
Upvotes: 1
Views: 3471
Reputation: 1793
The problem is, you are creating a new notification object. just create once, check if it exists in field
public class main extends Activity {
Notification notification=null;
//....
if (this.notification == null)
this.notification = new Notification(R.drawable.icon,
"A new notification", System.currentTimeMillis());
this.notification.flags |= Notification.FLAG_AUTO_CANCEL;
this.notification.number += 1;
Upvotes: 1