Android 多行通知,如 Gmail 应用程序

标签 android android-notifications

我正在尝试像 Gmail 应用程序那样创建一个多行通知,如下图所示(5 个通知分组在一个通知下)

enter image description here

我尝试了各种示例,但似乎只能创建单个通知,例如

   public void createSingleNotification(String title, String messageText, String tickerttext) {
        int icon = R.drawable.notification_icon; // icon from resources
        CharSequence tickerText = tickerttext; // ticker-text
        long when = System.currentTimeMillis(); // notification time
        Context context = getApplicationContext(); // application Context
        CharSequence contentTitle = title; // expanded message title
        CharSequence contentText = messageText; // expanded message text
        Intent notificationIntent = new Intent(this, MainActivity.class);

        Bundle xtra = new Bundle();
        xtra.putString("title", title);
        xtra.putString("message", messageText);

        notificationIntent.putExtras(xtra);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
          notificationIntent, PendingIntent.FLAG_ONE_SHOT
            + PendingIntent.FLAG_UPDATE_CURRENT);
        String ns = Context.NOTIFICATION_SERVICE;

        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
        Notification notification = new Notification(icon, tickerText, when);
        notification.setLatestEventInfo(context, contentTitle, contentText,   contentIntent);
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.FLAG_AUTO_CANCEL;
        notification.flags = Notification.DEFAULT_LIGHTS
          | Notification.FLAG_AUTO_CANCEL;
        final int HELLO_ID = 0;
        mNotificationManager.notify(HELLO_ID, notification);
      }

我不确定如何创建可以添加行的通知组。

最佳答案

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("Event tracker")
    .setContentText("Events received")
NotificationCompat.InboxStyle inboxStyle =
        new NotificationCompat.InboxStyle();

String[] events = {"line 1","line 2","line 3","line 4","line 5","line 6"};
// Sets a title for the Inbox in expanded layout
inboxStyle.setBigContentTitle("Event tracker details:");
...
// Moves events into the expanded layout
for (int i=0; i < events.length; i++) {
    inboxStyle.addLine(events[i]);
}
// Moves the expanded layout object into the notification object.
mBuilder.setStyle(inboxStyle);

...
// Issue the notification here.

关于Android 多行通知,如 Gmail 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20748049/

相关文章:

android - 扩展 com.mikepenz.materialdrawer.AccountHeaderBuilder

java - 当应用程序在后台时无法从 Intent 中获取额外信息

android - 通知 - 在 Lollipop Android 版本之前设置大图标

java - 计算纵跳腾空时间

java - 如何更改通知的内容而不重新创建它?

android - 在 Android 中自定义通知区域

android - 两个具有不同附加功能的 Activity 通知

android notifyDataSetChanged 不起作用

java - 如何获取内部存储上文件的file.length? .length() 似乎不起作用

java - indexOf 导致 java.lang.ArrayIndexOutOfBoundsException