如何显示具有单一通知ID的android GCM通知?

时间:2014-10-03 13:06:22

标签: java android google-cloud-messaging

我想要显示新闻GCm通知。 收到多条消息后,我希望显示3个未读消息,4个未读消息。 请帮忙我该怎么办 (抱歉英语不好)

2 个答案:

答案 0 :(得分:1)

只需计算您有多少条未读邮件,即可构建通知 使用[a NotificationManager.notify](http://developer.android.com/reference/android/app/NotificationManager.html#notify(int,android.app.Notification))有两个版本,一个接受通知ID,另一个接受标签和id, 对于这两种方法,如果已经存在具有相同ID或(id,tag)的通知,则任何先前的通知都将替换为新通知。

看看a this, 要查看可用的样式和模式,并使用适用于您的案例,以下是根据邮件数量执行Big TextInbox Style通知的示例

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

此方法假设您每次都使用您的应用收到的所有新消息来调用它, 由于NOTIFICATION_ID始终相同,因此任何先前的通知都将被新的通知替换, 你可以像这样测试它

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

答案 1 :(得分:0)

根据您的问题,我假设您正在尝试显示堆叠通知(更新当前通知)

以下是管理和更新通知的详细方法:http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Managing

多个通知: How to display multiple notifications in android