user1278062
user1278062

Reputation:

How to show android GCM notification having single notification Id?

I want to show news GCm notification. After receiving multiple message i want to show like 3 unread msg,4 unread msg. Please help how can i do (sorry for bad english)

Upvotes: 1

Views: 2080

Answers (2)

forcewill
forcewill

Reputation: 1647

Just count how many unread messages you have, build a notification and use the [a NotificationManager.notify](http://developer.android.com/reference/android/app/NotificationManager.html#notify(int, android.app.Notification)) there are two versions one that accepts a notification id and the other accepts a tag and an id, for both methods if there is already a notification with the same id or with the (id,tag) any previous notification is replaced by the new one.

Take a look into a this, to see the available styles and patterns and use the one appropriated to your case, here's an example of how to do a Big Text or Inbox Style notification based on the number of messages

public static void updateOrSendNotification(Context context, String[] messages) {
    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);


    int count = messages.length;
    if (messages.length == 0) {

        notificationManager.cancel(NOTIFICATION_ID);
        return;
    }


    //Intent to be launched on notification click
    Intent intent = new Intent(Intent.ACTION_VIEW,
            null,
            context, MainActivity.class);

    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);


    int requestID = (int) System.currentTimeMillis();
    PendingIntent contentIntent = PendingIntent.getActivity(context, requestID,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);

    String ticker = context.getString(R.string.new_notification_ticker);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context);

    String contentTitle = context.getString(R.string.notification_text_style_title, messages.length);

    mBuilder.setSmallIcon(R.drawable.ic_stat_notification)
            .setTicker(ticker)                      // the thicker is the message that appears on the status bar when the notification first appears
            .setDefaults(Notification.DEFAULT_ALL)  // use defaults for various notification settings
            .setContentIntent(contentIntent)        // intent used on click
            .setAutoCancel(true)                    // if you want the notification to be dismissed when clicked
            .setOnlyAlertOnce(true); // don't play any sound or flash light if since we're updating


    NotificationCompat.Style style;
    if (count > 1) {
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        style = inboxStyle;

        mBuilder.setContentTitle(contentTitle);

        for (String r : messages) {
            inboxStyle.addLine(r);
        }
    } else {
        NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
        style = bigTextStyle;

        bigTextStyle.setBigContentTitle(messages[0].substring(0, 10).concat(" ..."));
        bigTextStyle.bigText(messages[0]);
    }

    mBuilder.setStyle(style);


    notificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

This method assumes you call it every time with all the new messages your app has received, since the NOTIFICATION_ID is always the same any previous notification will be replaced by the new one, You can test it like this

String[] messages = { "hi john how are you ?", "john you never told me if you'r doing ok !", "john lets go out" }; updateOrSendNotification(this, messages);

Upvotes: 1

AndroidEnthusiast
AndroidEnthusiast

Reputation: 6657

From your question I assume you are trying to show stacked notifications (updating the current notifications)

Here is a detailed way of managing and updating notifications: http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Managing

Multiple notifications: How to display multiple notifications in android

Upvotes: 0

Related Questions