Edit: It won't allow me to accept my own answer for 2 days, but I figured it out. See my answer below if you have a similar problem
I'm working on stacking the push notifications from my app and displaying them using InboxStyle with a summary text. Currently, I'm displaying up to 3 notifications, followed by a "+ x more" message if necessary. To do so, I'm keeping track of then number of messages; however, I'm unsure how to reset the number of messages when the user clicks on the notification. For example, if the user gets 3 notifications, then clicks on the push notification to go into the app, I would like the next notification to be displayed as a single notification but it's currently being displayed "message a message b message c + 1 more." Is there a listener of some sort that I could use to reset message number to 0 when the user cancels the notification? Code below for reference:
public class GCMPushReceiverService extends GcmListenerService {
private static final String TAG = "GCMPushReceiverService";
private int objectTypeCode; //DLS
private String contentText;
private String objectKeyGUID; //DLG
private String alert;
private String sound;
private boolean vibrate;
private String contentTitle;
private String collapse_key;
private String tickerText;
private static int message_number = 0;
private static ArrayList<String> messages = new ArrayList<String>();
private static final String MY_GROUP = "my_group";
//with every new message
@Override
public void onMessageReceived(String from, Bundle data){
this.objectTypeCode = Integer.parseInt(data.getString("objectTypeCode"));
this.contentText = data.getString("contentText");
this.objectKeyGUID = data.getString("objectKeyGUID");
this.alert = data.getString("alert");
this.sound = data.getString("sound");
this.vibrate = Boolean.parseBoolean(data.getString("vibrate"));
this.contentTitle = data.getString("contentTitle");
this.collapse_key = data.getString("collapse_key");
this.tickerText = data.getString("ticker_Text");
messages.add(message_number, contentTitle +": " + contentText);
message_number++;
Log.d(TAG, "From: " + from);
sendNotification();
}
private void sendNotification() {
Intent intent;
if (objectKeyGUID != null && !objectKeyGUID.equals("")) intent = deepLink();
else intent = new Intent(this, LogInPage.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
int requestCode = 0;
PendingIntent pendingIntent =
PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this);
noBuilder.setSmallIcon(R.drawable.icon);
noBuilder.setContentText(contentText);
noBuilder.setTicker(tickerText);
noBuilder.setContentIntent(pendingIntent);
noBuilder.setSound(sound);
noBuilder.setGroup(MY_GROUP);
noBuilder.setAutoCancel(true);
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),
R.drawable.icon);
noBuilder.setLargeIcon(largeIcon);
//if we have more than one message, we want to display them in one notification, inbox style
//if it's more than 3, display the first three and have a "+ x more" message
//TODO: reset the message_number when we click the notification
if (message_number > 1) {
noBuilder.setContentTitle(message_number + " messages");
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
for (int i = 0; i < message_number && i < 3; i++)
inboxStyle.addLine(messages.get(i));
if (message_number > 3)
inboxStyle.setSummaryText("+ " + (message_number - 3) + " more");
inboxStyle.setBigContentTitle(message_number + " Notifications");
//noBuilder.setContentTitle(message_number + " Notifications");
noBuilder.setStyle(inboxStyle);
}
else {
noBuilder.setContentTitle(contentTitle);
}
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, noBuilder.build()); //0 = ID of notification
}