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);
}