Question

Im developing an Android application that will generate notifications and the application is working fine, but the problem is that when the phone receive a notification and receive another notification, the later notification hide the first. I want as the application receive many notifications theses all notification should be displayed not only one (the later) please how could i do that?

Here is the code that handle the notification.

  private static void generateNotification(Context context, String message) {
    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);

    Intent notificationIntent = new Intent(context, Activity_SplashScreen.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent =
            PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;

    //notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");

    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);      

}
Was it helpful?

Solution

You need to pass different notification id for each Notification . If you pass same id (i.e., 0 in your case), the existed notification will be updated with the new data

So change this:

  notificationManager.notify(0, notification);

to , e.g., Set some unique id int notificationId = 0x10;

notificationManager.notify(++notificationId, notification);  

ID - If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.

OTHER TIPS

In the last line notificationManager.notify(0, notification); the 0 is the notification ID. If you want to show multiple notifications, you need to change that ID. All notifications with the same ID overwrite each other.

http://developer.android.com/reference/android/app/NotificationManager.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top