2

I am creating an application with a notification at different times. First notification display fine in the notification bar with an icon, title and text. But when the second notification comes the title and text of the first notification gets cleared and the second one displays. For the third notification again the second one's title and text gets cleared and so on. The code i am using is

        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        long mNotificationId = System.currentTimeMillis();
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setSound(uri);

        NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();

        bigTextStyle.setBigContentTitle("Time to Sleep!!!");
        bigTextStyle.bigText("A good laugh and a long sleep are the best cures in the doctor's book.");

        mBuilder.setStyle(bigTextStyle);
        mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
        // Gets an instance of the NotificationManager service
        NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // Builds the notification and issues it.
        mNotifyMgr.notify((int)mNotificationId, mBuilder.build());

Any help would be appreciated. Thanks in advance.

Sumesh P
  • 385
  • 2
  • 10

2 Answers2

2

I think you are only setting text for BigTextStyle

Also set Values for small or default settings

for eg

// Small - default style
NotificationCompat.Builder mBuilder =
    new NotificationCompat.Builder(context)
        .setDefaults(Notification.DEFAULT_ALL)
        .setSmallIcon(context.getApplicationInfo().icon)
        .setWhen(System.currentTimeMillis())
        .setContentTitle(extras.getString("title"))
        .setTicker(extras.getString("title"))
        .setContentIntent(contentIntent);

Just check the mNotificationId value if its same If same you can use the following to generate the random number

Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;

and then use this m

mNotifyMgr.notify(m, mBuilder.build());
Vaibs_Cool
  • 6,126
  • 5
  • 28
  • 61
  • I am getting multiple notifications but only the title and the text get cleared. What i can see is a blank notification with an icon – Sumesh P Jun 22 '15 at 11:37
  • Do i have to set the above code along with bigTextStyle ? – Sumesh P Jun 22 '15 at 12:10
  • yes `NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();` – Vaibs_Cool Jun 22 '15 at 12:12
  • Now am getting the title and text for all the notifications. But the problem now is only the last notification shows the full text. Others shows the text in a single line only. – Sumesh P Jun 22 '15 at 12:27
  • NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setDefaults(Notification.DEFAULT_ALL) .setContentTitle("Time to Sleep!!!") .setContentText("A good laugh and a long sleep are the best cures in the doctor's book.") .setSound(uri); NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle(); bigTextStyle.setBigContentTitle("Time to Sleep!!!"); bigTextStyle.bigText("A good laugh and a long sleep are the best cures in the doctor's book."); mBuilder.setStyle(bigTextStyle); – Sumesh P Jun 22 '15 at 12:35
1

Try using

            int notification_id = (int) date.getTime();

to ensure that every time a unique id is generated. Notifications with unique ids do not get over written and you can see multiple notifications in the notification panel. Hope this helps.

user765
  • 167
  • 6