Reputation: 77
I am developing an app which will count received notifications in notification bar. Till now I am done with create multiple notifications on button click ans it is showing different notifications. What i want is it shows setText when single notification and when it is multiple it will count it n shows in setText.
I tried hard to find this on Stackoverflow and other Android sites but didnot find what i am looking for. Any help would be very support full.
If anyone is not able to understand my question then please let me know.
Here is the code:
Button noti;
int counter = 0;
boolean ncompi = false;
// private static int pendingNotificationsCount = 0;
NotificationManager nManager;
NotificationCompat.Builder ncomp;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.imageclass);
SharedPreferences sharedpreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putInt("count", count);
editor.putInt("counter", counter);
editor.commit();
SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
int i = sharedPreferences.getInt("count", 0);
int j = sharedPreferences.getInt("counter", 0);
Button increaseButton = (Button) findViewById(R.id.btn1);
increaseButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
ncomp = new NotificationCompat.Builder(ImageClass.this);
ncomp.setContentTitle("9999900000");
if(counter == 0){
ncomp.setContentText(ss);
counter++;
// ncomp.setContentText(ss);
Intent resultIntent = new Intent(ImageClass.this, MainActivity.class);
// resultIntent.putExtra("msg", ss);
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 1, resultIntent, 0);
ncomp.setContentIntent(pi);
ncomp.setTicker("Notification Listener");
ncomp.setSmallIcon(R.drawable.ic_launcher);
ncomp.setAutoCancel(true);
nManager.notify(NOTIFY_ID, ncomp.build());
}
else{
ncomp.setContentTitle("Notification");
ncomp.setContentText(counter + " Notifications");
Intent resultIntent = new Intent(ImageClass.this, SmsActivity.class);
// resultIntent.putExtra("message", ss);
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 1, resultIntent, 0);
ncomp.setContentIntent(pi);
ncomp.setTicker("Notification Listener");
ncomp.setSmallIcon(R.drawable.ic_launcher);
ncomp.setAutoCancel(true);
nManager.notify(NOTIFY_ID, ncomp.build());
counter++;
}
And also How to differ read and unread notifications in Notification Activity?
ArrayList<MessageData> arrayList = new ArrayList<MessageData>();
CustomListAdapter adapter = new CustomListAdapter(this, arrayList);
smsListView.setAdapter(adapter);
MessageData data1 = new MessageData();
data1.setMessage("");
data1.setRead(false);
Created CustomListAdapter n here is the code.
public class CustomListAdapter extends BaseAdapter {
private Context context;
private ArrayList<MessageData> dataList;
public CustomListAdapter(Context context, ArrayList<MessageData> arr){
super();
this.context = context;
this.dataList = arr;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return dataList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return dataList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View view, ViewGroup arg2) {
// TODO Auto-generated method stub
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.listitem_spinner, null);
}
TextView textView = (TextView) view.findViewById(R.id.txt);
textView.setText("" + dataList.get(position).getMessage());
// here you can get Read/Unread status for all message
boolean isRead = dataList.get(position).isRead();
if (isRead) {
textView.setTextColor(Color.GRAY);
} else {
textView.setTextColor(Color.BLACK);
}
return view;
}
}
Upvotes: 1
Views: 10435
Reputation: 2423
You need to see Managing Notifications as it explains how to update notifications. Here is what you might want:
public variables
final int NOTIFY_ID=1; // any integer number
int count = 0;
on click event:
NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder ncomp = new NotificationCompat.Builder(MainActivity.this);
ncomp.setContentTitle("My Notification");
if (count == 0)
ncomp.setContentText("Notification");
else
ncomp.setContentText(count + " Notifications");
Intent resultIntent = new Intent(MainActivity.this, Message.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(MainActivity.this);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
ncomp.setContentIntent(resultPendingIntent);
ncomp.setTicker("Notification Listener");
ncomp.setSmallIcon(R.drawable.ic_launcher);
ncomp.setAutoCancel(true);
nManager.notify(NOTIFY_ID, ncomp.build());
count++;
Hope this helped!
Edit: To store count refer Shared Preferences
Upvotes: 2