Как использовать стиль входящих уведомлений с помощью Android?

Я не знаю, как именно использовать стиль входящих уведомлений. Я только что попробовал этот код ниже, и он показывает ошибку в Notification.InboxStyle(). Какую ошибку я сделал? Может ли кто-нибудь помочь мне с этой проблемой? Вот мой код..

  private void generateNotification(Context context, String message) {
    System.out.println(message+"++++++++++2");
    int icon = R.drawable.ic_message;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    String title = context.getString(R.string.message_title);
    String subTitle = context.getString(R.string.message_subtitle);
    Intent notificationIntent = new Intent(context, Output.class);
    notificationIntent.putExtra("content", message);
    PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    Notification base = new Notification.Builder(context)
    .setTicker(message)
    .setSmallIcon(icon)
    .setWhen(when)
    .setContentTitle(title)
    .setContentText(subTitle)
    .setNumber(4)
    .setContentIntent(intent)
    .build();

    Notification notification = new Notification.InboxStyle(base)
    .addLine("First Message")
    .addLine("Second Message")
    .addLine("Third Message")
    .addLine("Fourth Message")
    .setBigContentTitle("Here Your Messages")
    .setSummaryText("+3 more")
    .build();

    //To play the default sound with your notification:
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);



}

Заранее спасибо.


person user3057567    schedule 19.12.2013    source источник


Ответы (2)


Конструктор is

Notification.InboxStyle(Notification.Builder builder)

и вы передаете уведомление. Делай так вместо этого

Notification notification = new Notification.InboxStyle(new Notification.Builder(context)
.setTicker(message)
.setSmallIcon(icon)
.setWhen(when)
.setContentTitle(title)
.setContentText(subTitle)
.setNumber(4)
.setContentIntent(intent))
.addLine("First Message")
.addLine("Second Message")
.addLine("Third Message")
.addLine("Fourth Message")
.setBigContentTitle("Here Your Messages")
.setSummaryText("+3 more")
.build();
person m.Ling    schedule 19.12.2013
comment
Как получить .addLine(Первое сообщение) .addLine(Второе сообщение) .addLine(Третье сообщение) .addLine(Четвертое сообщение) Мне нужно получить из предыдущих уведомлений и добавить сюда, как я могу это сделать? - person Sriram C G; 19.11.2015
comment
@SriramCG добавить предыдущее уведомление в общий преф. И получить, когда вы хотите показать новое уведомление и добавить старые сообщения к новым сообщениям. - person Kishan Vaghela; 20.12.2016
comment
Notification.InboxStyle теперь устарел;) - person Harkal; 12.06.2020

person    schedule
comment
Вы дважды вызываете setWhen и setSmallIcon. Второе их использование перезапишет значения в первом, поэтому вы можете просто удалить первые вызовы. - person Niall; 15.10.2018