首先,我阅读了this并将这个简单的代码用于多个通知:
public static void notifyMessage()
{
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
int notifyID = 1;
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
int numMessages = 0;
builder.setSmallIcon(R.drawable.icon)
.setContentTitle("Event tracker")
.setContentText("Events received")
.setNumber(++numMessages)
.setWhen(System.currentTimeMillis()).setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("bigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbig"));
Notification notification = builder.build();
notificationManager.notify(1, notification);
它正在工作。如果消息的长度大于通常的限制,它将显示“bigbig...”。
下一步,我想使用用户的消息显示在通知中。所以我将功能更改为:
public static void notifyMessage(String message , boolean flashLed)
{
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
int notifyID = 1;
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
int numMessages = 0;
builder.setSmallIcon(R.drawable.icon)
.setContentTitle("Event tracker")
.setContentText(message)
.setNumber(++numMessages)
.setWhen(System.currentTimeMillis()).setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message));
if (flashLed)
{
builder.setLights(0xFFFF0000, 500, 500);
}
Notification notification = builder.build();
notificationManager.notify(1, notification);
}
我试图显示消息 而不是“bigbigbig....”,但它总是显示简短的通知类型。我也试过这个:
public static void notifyMessage(String message)
{
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
String[] events = new String[4];
// Sets a title for the Inbox in expanded layout
events[0] = message.substring(0, 15);
events[1] = message.substring(15, 35);
events[2] = message.substring(35, 55);
events[3] = message.substring(55, 75);
inboxStyle.setBigContentTitle("Event tracker details:");
// Moves events into the expanded layout
for (int i=0; i < events.length; i++) {
inboxStyle.addLine(events[i]);
}
builder.setSmallIcon(R.drawable.icon)
.setContentTitle("Event tracker"")
.setContentText("Events received")
.setWhen(System.currentTimeMillis()).setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_MAX)
.setStyle(inboxStyle);
Notification notification = builder.build();
notificationManager.notify(1, notification);
}
但是当新消息到达时应用程序崩溃。如何更改第二或第三个功能?
最佳答案
从 .setContentText("")
中删除内容
改为添加:
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message));
例子:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(GetNotificationIcon())
.setContentTitle(Constants.APP_NAME)
.setContentText("")
.setSound(uri)
.setVibrate(vibrate)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message));
关于java - Android-多行通知不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31313092/