Many of us have used notifications in our android application but do you know we can update those shown notification in our application. When we make a notification we passes an Id in the notify(), as the syntax is
1 |
public void notify(int id, Notification notification) |
Its an unique id and its unique within your application, so when you passes another notification with the same id it overrides the previous one and creates a new notification.
So if we wanted a stacked notification of various events we have to
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra("from_notification", true); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_ONE_SHOT); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_status) .setContentTitle("Messages") .setContentText("Hey") .setLargeIcon(icon) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); int id_notification = 1; notificationManager.notify(id_notification, notificationBuilder.build()); |
and then when you have a new message to show, the append it in setContentText() to show stacked messages in a notification.