通知多行

61
如何使长通知多行显示。 我正在使用以下代码片段,但它不起作用:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
  .setContentTitle(title)
  .setSmallIcon(R.drawable.icon)
  .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
  .setContentText(message)
  .setContentIntent(pIntent);

return mBuilder.build();

1
你的目标是哪个SDK版本? - Siddharth_Vyas
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> - teja
多行通知支持4.1+操作系统。请查看我提供的答案部分中的链接。 - Siddharth_Vyas
5个回答

130
在此添加:

NotificationCompat.Builder builder = new NotificationCompat.Builder(
            context);
    Notification notification = builder.setContentIntent(contentIntent)
            .setSmallIcon(icon).setTicker(appname).setWhen(0)
            .setAutoCancel(true).setContentTitle(appname)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setContentText(message).build();

    notificationManager.notify(0, notification);

请查看: 多行通知



1
工作正常,详情请见:http://developer.android.com/reference/android/app/Notification.BigTextStyle.html - user2345998
链接好像挂了。这是之前的链接:https://web.archive.org/web/20190812093224/https://www.vogella.com/tutorials/AndroidNotifications/article.html - android developer
2
你能解释一下你添加了哪些代码行吗?首先,这些代码行的顺序与问题中的不同,其次,你可以使用英语来解释你的解决方案,而不仅仅是编写未格式化的无序代码。 - Vince

12
Bitmap icon1 = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);  
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(getNotificationIcon()).setLargeIcon(icon1)
            .setTicker(title)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setTicker(message)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(title))
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setPriority(Notification.PRIORITY_MAX)
            .setContentIntent(pIntent)
            .setContent(remoteViews)
             .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setVibrate(new long[] {1, 1, 1})
             .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_launcher))
            .setDefaults(Notification.DEFAULT_SOUND)
            .setDefaults(Notification.DEFAULT_ALL);
    NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notify = new Notification();
    notify.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_ONLY_ALERT_ONCE;
    Random random = new Random();
    notificationmanager.notify(random.nextInt(), builder.build());

4
setStyle被调用了两次,这是错误吗?如果不是的话,它是如何工作的? - User12111111
1
您不能两次使用 bigTextStyle,第二次将覆盖第一次的设置。 标题、上下文和大文本可以是不同的值。 如果通知被展开或用户自己展开它,则会显示大文本。 - M.Baraka

5
尝试使用此代码,可在Android Oreo通知渠道id上正常工作。
public static int id = 0;
private void sendNotification(String messageBody) {

    Log.d("FCMID","id : " + id);
    Intent intent = new Intent(this, Main2Activity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent,PendingIntent.FLAG_ONE_SHOT);

    String channelId = "1";
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.drawable.ic_notification)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
            .setContentTitle("Piml Sid")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId,
                "Spike Bot",
                NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }
    id ++;
   notificationManager.notify(id /* ID of notification */, notificationBuilder.build());
}

3
在你的NotificationCompat.Builder中添加:
.setStyle(NotificationCompat.BigTextStyle().bigText(body))
.setContentText(body) 

除了.setContentText(body)之外


-2
Notification notification = new Notification.Builder(context)
         .setContentTitle(title)  
         .setSmallIcon(icon) 
         .setStyle(new Notification.BigTextStyle().bigText(notificationMessage))
         .setAutoCancel(true)
         .setContentIntent(intent)
         .build();

1
代码中的 setStyle(new Notification.BigTextStyle() 必须改为 setStyle(new NotificationCompat.BigTextStyle(),否则会抛出错误,提示BigTextStyle 无法转换为 Style - efkan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接