简体   繁体   中英

Android Status bar multiline Notification API 14

I'm using API 14 device. I have been trying to develop the multiline notification similar to Gmail notifications. I have gone through several stack overflow questions but didn't find any solution which would give me multiline notification for API<16. Please note that Big view notification works only for OS 4.1+ and I want this for 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”,“ hello2”,“ hell3”等不可见

I am not getting Strings "hello 1", "hello 2", ... in separate lines. Please Help me...

EDIT: Since many wrong solutions have been posted, I would like to point out that the multiline notifications are working fine in other modern mobiles(nexus). The code I posted is not faulty/buggy. But it doesn't work for API 14.

Try Following code

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

OR, you can also refer this

Firstly I will not recommend API14 (as it is obsolete), but if you still want to have multiple line notification,you can have by using 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();

Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM