Java 如何显示具有单个通知Id的android GCM通知?
我想显示新闻GCm通知。 收到多条消息后,我想显示3条未读消息,4条未读消息。 请帮忙,我该怎么办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,标记)的通
(抱歉英语不好)根据您的问题,我假设您正在尝试显示堆叠通知(更新当前通知) 以下是管理和更新通知的详细方法: 多个通知:
只需计算您有多少未读邮件,构建一个通知,然后 使用[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(此,消息)代码>