1

I am trying to add the Whatsapp/Email notification Style to my app, where when one notification comes and another one was present, they are both displayed on the same message. I do this via saving the notifications to a database which is deleted when message is clicked. Currently, my loop works great except when i send more than 2 notifications e.g 4. [![When working][1]][1]

When it breaks..

[![pics][2]][2]

The numbers represent the order in which i sent notifications. As you can see, Four is repeated twice instead of none..The Required order for screenshot two from top to bottom would have been Four->Three->Two->One.

Here's the code for the loop..

    Cursor cur;
    ............
    ............
    int imsg = cur.getColumnIndex(KEY_MSG);
    int ititle = cur.getColumnIndex(KEY_TITLE);

    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    inboxStyle.setBigContentTitle(makeNotificationLine(title, ""));

    if (c == 0) {
        //when DB is empty...
        inboxStyle.addLine(msg);
    } else {

        for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
            num++;
            inboxStyle.setBigContentTitle("Reelforge");
            inboxStyle.addLine(makeNotificationLine(title, msg));
            inboxStyle.addLine(makeNotificationLine(cur.getString(ititle), cur.getString(imsg)));
        }
    }

    inboxStyle.setSummaryText(num + " new notifications");
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(title)
            .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS)
            .setContentText(msg);
        mBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
        mBuilder.setAutoCancel(true);
        mBuilder.setStyle(inboxStyle);
        mBuilder.setContentIntent(contentIntent);

        the_db.close();
        Random random = new Random();
        int m = random.nextInt(9999 - 1000) + 1000;
        String x = m + "";

        SimpleDateFormat sdf = new SimpleDateFormat("LLL d, yyyy");
        sdf.setTimeZone(TimeZone.getDefault());
        String date = sdf.format(new Date());

        try {
            the_db.open();
            the_db.createmsgEntry(x, title, msg);
            the_db.createmsgEntry2(x, title, msg, date);
            the_db.close();

        } catch (Exception e) {
            the_db.close();
        } finally {
            if (the_db != null) {
                the_db.close();
            }
        }

        mNotificationManager.notify(N_ID, mBuilder.build());



public long createmsgEntry(String s2, String s3, String s4) {

    ContentValues cv = new ContentValues();
    cv.put(KEY_MSGID, s2);
    cv.put(KEY_TITLE, s3);
    cv.put(KEY_MSG, s4);

    return ourDatabase.insert(DATABASE_TABLE, null, cv);

}
Bmbariah
  • 687
  • 2
  • 10
  • 22
  • c is a count for records in the database and Yes, the Title(ititle) is in bold and imsg for the message – Bmbariah Oct 31 '15 at 14:33
  • I've added code to reflect that... – Bmbariah Oct 31 '15 at 14:48
  • createmsgEntry and createmsgEntry2 are exactly the same, only that one of them is deleted when notification is clicked while the other is saved to allow user to see sent notifications..I am not sending messages just updates that i would like to store for the user to see later. – Bmbariah Oct 31 '15 at 15:01
  • added the createmsgEntry code.. – Bmbariah Oct 31 '15 at 15:04
  • Didn't work...still displaying multiple entries twice on notification – Bmbariah Nov 05 '15 at 13:54

2 Answers2

1

Finally solved it....

mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    int num = 0;
    Random random = new Random();
    int m = random.nextInt(9999 - 1000) + 1000;

    String x = m + "";

    SimpleDateFormat sdf = new SimpleDateFormat("LLL d, yyyy");
    sdf.setTimeZone(TimeZone.getDefault());
    String date = sdf.format(new Date());

    Intent i = new Intent(this, MainActivity.class);

    i.setAction("gcm");

    //opened after clicking
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, 0);

    try {
        the_db.open();
        c = the_db.getCount();
        cur = the_db.getmsgData();
        the_db.createmsgEntry(x, title, msg);
        the_db.createmsgEntry2(x, title, msg, date);

    } catch (Exception e) {
        the_db.close();
    }

    int imsg = cur.getColumnIndex(KEY_MSG);
    int ititle = cur.getColumnIndex(KEY_TITLE);

    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    inboxStyle.setBigContentTitle(makeNotificationLine(title, ""));

    if (c == 0) {
        num++;
        inboxStyle.addLine(msg);
    } else {
        for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
            num++;
            inboxStyle.setBigContentTitle("New Notifications");
            inboxStyle.addLine(makeNotificationLine(cur.getString(ititle), cur.getString(imsg)));
        }
    }

    inboxStyle.setSummaryText(num + " new notifications");

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS)
            .setContentText(msg);
    mBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
    mBuilder.setAutoCancel(true);
    mBuilder.setStyle(inboxStyle);
    mBuilder.setContentIntent(contentIntent);
    the_db.close();

    mNotificationManager.notify(N_ID, mBuilder.build());
Bmbariah
  • 687
  • 2
  • 10
  • 22
0

If you look at the docs here Applying an expanded layout to a notification

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
    .../
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

// Set up inboxStyle
inboxStyle.setBigContentTitle(.../
    ...
// Moves events into the expanded layout
for (int i=0; i < events.length; i++) {

    inboxStyle.addLine(events[i]);
}
// Moves the expanded layout object into the notification object.
mBuilder.setStyle(inBoxStyle);
    ...
// Issue the notification here.

The lines are added after the Builder is created. Firstly: take care of your database;

// To do.. add new notifications to db, parse values etc,

Then create new builder and loop through your db to get values to add to lines.

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
        ..../
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(makeNotificationLine(title, ""));

if (c == 0) {
    //when DB is empty...
    inboxStyle.addLine(msg);
} else {

    for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
        num++;
        inboxStyle.setBigContentTitle("Reelforge");
        inboxStyle.addLine(makeNotificationLine(title, msg));
        inboxStyle.addLine(makeNotificationLine(cur.getString(ititle), cur.getString(imsg)));
    }
}

inboxStyle.setSummaryText(num + " new notifications");

 mNotificationManager.notify(N_ID, mBuilder.build());

Each time you update a notification like this, you need to actually create a new notification builder. You cannot add lines to an existing notification builder. You can update some things, like title.

Some good links.

Update android notification with new line is removing previous lines

Android multiple line notification like Gmail app

Update text of notification, not entire notification

Community
  • 1
  • 1