尝试使用通知inboxStyle在通知中显示类似于GMAIL应用程序的更多行,但它不起作用

时间:2017-11-28 16:08:18

标签: android notifications gmail notificationmanager

这是我的代码:

  NotificationManager notifManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
    Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
    inboxStyle.setBigContentTitle("Title - Notification");
    inboxStyle.setSummaryText("TEST");
    inboxStyle.addLine("LINE");
    Notification noti = new Notification.Builder(getActivity())
            .setContentTitle("PSNGR")
            .setContentText("PITSTOP BLA").setSmallIcon(R.drawable.notification_icon)
            .setStyle(inboxStyle)
            .setContentIntent(null).build();

    noti.flags |= Notification.FLAG_AUTO_CANCEL;
    notifManager.notify(0, noti);

但InboxStyle通知中没有任何内容。我只得到PSNGR和PITSTOP BLA,为什么会这样?

PS:还尝试了BitTextStyle(),但也没有看到: 对于此代码:

 NotificationManager notifManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getActivity())
            .setContentTitle("PSNGR")
            .setSmallIcon(R.drawable.ic_launcher)
            .setStyle(new NotificationCompat.BigTextStyle().bigText("MATA"))
            .setStyle(new NotificationCompat.BigTextStyle().bigText("MATA2"))
            .setAutoCancel(true)
            .setContentIntent(null);
    notifManager.notify(0, notificationBuilder.build());

我只看到:“PSNGR”

1 个答案:

答案 0 :(得分:0)

我使用的是RemoteViews而不是正常的通知:

 NotificationManager notifManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(getActivity()).setSmallIcon(R.drawable.ic_launcher);

    RemoteViews mContentView = new RemoteViews(getActivity().getPackageName(), R.layout.pitstop_notification);
    mContentView.setImageViewResource(R.id.image, R.drawable.notification_icon);
    mContentView.setTextViewText(R.id.title, "Custom notification");
    mContentView.setTextViewText(R.id.text, "This is a custom layout");
    mContentView.setTextViewText(R.id.text2, "This is a custom layout2");
    mBuilder.setContent(mContentView);
    notifManager.notify(0, mBuilder.build());