0

我正在使用 API 14 设备。我一直在尝试开发类似于 Gmail 通知的多行通知。我已经解决了几个堆栈溢出问题,但没有找到任何解决方案可以为我提供 API<16 的多行通知。请注意,大视图通知仅适用于 OS 4.1+,我希望它适用于 API 14。

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    NotificationCompat.InboxStyle inboxStyle;
    inboxStyle = new NotificationCompat.InboxStyle();

    inboxStyle.addLine("Hello 1")
    .addLine("Hello 2")
    .addLine("Hello 3")
    .addLine("Hello 4");

    builder.setStyle(inboxStyle);
    builder.setTicker("HELLO WORLD MSG");
    builder.setSmallIcon(android.R.drawable.sym_def_app_icon);
    builder.setContentTitle("HELLO WORLD");
    builder.setPriority(NotificationCompat.PRIORITY_HIGH);
    notificationManager.notify(1000, builder.build());

这是我得到的结果。 这里的字符串

我没有在单独的行中获得字符串“hello 1”、“hello 2”、...。请帮我...

编辑:由于发布了许多错误的解决方案,我想指出多行通知在其他现代手机(nexus)中运行良好。我发布的代码没有错误/错误。但它不适用于 API 14。

4

2 回答 2

0

首先我不会推荐 API14(因为它已经过时了),但是如果你仍然想要多行通知,你可以使用Notification.InboxStyle

Notification.InboxStyle inboxStyle = new Notification.InboxStyle();     
String[] events = new String[]{"h1","h2","h3","h4"};                //your lines
inboxStyle.setBigContentTitle("Event tracker details:");
for(int i=0; i < events.length; i++) {
   inboxStyle.addLine(events[i]);
}
Notification notification = new Notification.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentIntent(pintent)
        .setStyle(inboxStyle)           
        .build();

参考

于 2016-02-24T10:35:57.967 回答
0

尝试以下代码

String[] names=new String[5];
        names[0]="Hello 1";
        names[1]="Hello 2";
        names[2]="Hello 3";
        names[3]="Hello 4";
        names[4]="Hello 5";
        inboxStyle.setBigContentTitle("Hello");
        for (String name:names)
        {
            inboxStyle.addLine(name);
        }

        builder.setStyle(inboxStyle);

或者,你也可以参考这个

于 2016-02-24T10:34:28.277 回答