I know this has been asked a long time ago and probably lots of users like me were searching here as well but I will give the full code of notification in this manner. Imagine a scenario where you have a chat application and where you want to get notifications grouped BY EACH USER!notice by each user. So the best way to do it to notify by unique user id, thus you have to write a single code to notify as you can`t guess how many notifications are present at that moment if you are targeting below API level 23 which is getting almost lots of users out of sight. So what shall you do when you want to group each user received notification and keep logic? The best way I did that was using shared preferences with some slight logic there let me post the code with comments.
(By this you targeting to lower APIs so the KitKat will handle this logic too)
The class which imitates the notification as a test.
/**
* Simple id just for test
*/
private int NOTIFICATION_ID = 1;
private static int value = 0;
Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
private NotificationCompat.Builder mCopat;
private Bitmap bitmap;
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
mCopat = new NotificationCompat.Builder(getApplicationContext());
/**
* Here we create shared preferences.Probably you will create some helper class to access shared preferences from everywhere
*/
sharedPreferences = getSharedPreferences("shared", MODE_PRIVATE);
editor = sharedPreferences.edit();
/**
* I clear this for test purpose.
*/
editor.clear();
editor.commit();
}
public void hey(View view) {
/**
* Add notification,let`s say add 4 notifications with same id which will be grouped as single and after it add the rest which will be grouped as new notification.
*/
int clickedTime = value++;
if (clickedTime == 4) {
NOTIFICATION_ID++;
}
/**
* Here is the important part!We must check whether notification id inserted inside the shared preferences or no.If inserted IT MEANS THAT WE HAVE an notification
* to where we should add this one (add the new one to the existing group in clear way)
*/
if (sharedPreferences.getString(String.valueOf(NOTIFICATION_ID), null) != null) {
Log.d("fsafsafasfa", "hey: " + "not null adding current");
/**
* TAKE A NOTICE YOU MUST NOT CREATE A NEW INSTANCE OF INBOXSTYLE, OTHERWISE, IT WON`T WORK. JUST ADD VALUES TO EXISTING ONE
*/
NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle("Lanes");
builder.setContentText("Notification from Lanes " + value);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(bitmap);
builder.setAutoCancel(true);
/**
* By this line you actually add an value to the group,if the notification is collapsed the 'Notification from Lanes' text will be displayed and nothing more
* otherwise if it is expanded the actual values (let`s say we have 5 items added to notification group) will be displayed.
*/
inboxStyle.setBigContentTitle("Enter Content Text");
inboxStyle.addLine("hi events " + value);
/**
* This is important too.Send current notification id to the MyBroadcastReceiver which will delete the id from sharedPrefs as soon as the notification is dismissed
* BY USER ACTION! not manually from code.
*/
Intent intent = new Intent(this, MyBroadcastReceiver.class);
intent.putExtra("id", NOTIFICATION_ID);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
/**
* Simply set delete intent.
*/
builder.setDeleteIntent(pendingIntent);
builder.setStyle(inboxStyle);
nManager.notify("App Name", NOTIFICATION_ID, builder.build());
/**
* Add id to shared prefs as KEY so we can delete it later
*/
editor.putString(String.valueOf(NOTIFICATION_ID), "notification");
editor.commit();
} else {
/***
* Here is same logic EXCEPT that you do create an INBOXSTYLE instance so the values are added to actually separate notification which will just be created now!
* The rest logic is as same as above!
*/
/**
* Ok it gone to else,meaning we do no have any active notifications to add to,so just simply create new separate notification
* TAKE A NOTICE to be able to insert new values without old ones you must call new instance of InboxStyle so the old one will not take the place in new separate
* notification.
*/
Log.d("fsafsafasfa", "hey: " + " null adding new");
inboxStyle = new Notification.InboxStyle();
NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle("Lanes");
builder.setContentText("Notification from Lanes " + value);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(bitmap);
builder.setAutoCancel(true);
inboxStyle.setBigContentTitle("Enter Content Text");
inboxStyle.addLine("hi events " + value);
Intent intent = new Intent(this, MyBroadcastReceiver.class);
intent.putExtra("id", NOTIFICATION_ID);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
builder.setDeleteIntent(pendingIntent);
builder.setStyle(inboxStyle);
nManager.notify("App Name", NOTIFICATION_ID, builder.build());
editor.putString(String.valueOf(NOTIFICATION_ID), "notification");
editor.commit();
}
}
}
The broadcast receiver for delete intent. Do not forget to add a receiver to your manifest!
public class MyBroadcastReceiver extends BroadcastReceiver {
/**
* Receive swipe/dismiss or delete action from user.This will not be triggered if you manually cancel the notification.
* @param context
* @param intent
*/
@Override
public void onReceive(Context context, Intent intent) {
/**
* As soon as received,remove it from shared preferences,meaning the notification no longer available on the tray for user so you do not need to worry.
*/
SharedPreferences sharedPreferences = context.getSharedPreferences("shared", context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(String.valueOf(intent.getExtras().getInt("id")));
editor.commit();
}
}