Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何显示具有单个通知Id的android GCM通知?_Java_Android_Google Cloud Messaging - Fatal编程技术网

Java 如何显示具有单个通知Id的android GCM通知?

Java 如何显示具有单个通知Id的android GCM通知?,java,android,google-cloud-messaging,Java,Android,Google Cloud Messaging,我想显示新闻GCm通知。 收到多条消息后,我想显示3条未读消息,4条未读消息。 请帮忙,我该怎么办 (抱歉英语不好)根据您的问题,我假设您正在尝试显示堆叠通知(更新当前通知) 以下是管理和更新通知的详细方法: 多个通知: 只需计算您有多少未读邮件,构建一个通知,然后 使用[aNotificationManager.notify](,android.app.Notification))有两个版本,一个接受通知id,另一个接受标签和id, 对于这两种方法,如果已经存在具有相同id或(id,标记)的通

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

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

以下是管理和更新通知的详细方法:

多个通知:

只需计算您有多少未读邮件,构建一个通知,然后 使用[a
NotificationManager.notify
](,android.app.Notification))有两个版本,一个接受通知id,另一个接受标签和id, 对于这两种方法,如果已经存在具有相同id或(id,标记)的通知,则以前的任何通知都将替换为新通知

看看,, 要查看可用的样式和模式并使用适合您的案例的样式和模式,下面是一个示例,说明如何根据邮件数量执行
大文本
收件箱样式
通知

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());
}
此方法假定您每次调用它时都会收到应用程序收到的所有新消息, 由于通知ID始终相同,因此以前的任何通知都将替换为新通知, 你可以这样测试它

String[]消息={
“嗨,约翰,你好吗?”,
“约翰,你从来没告诉过我你过得好不好!”,
“约翰让我们出去”
};
updateOrSendNotification(此,消息)