設定自定義通知 - 顯示完整內容文字

如果要在上下文中顯示長文字,則需要設定自定義內容。

例如,你有這個:

StackOverflow 文件

但是你希望你的文字能夠完整展示:

StackOverflow 文件

你需要做的就是為你的內容新增樣式,如下所示:

  private void generateNotification(Context context) {
        String message = "This is a custom notification with a very very very very very very very very very very long text";
        Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), android.R.drawable.ic_dialog_alert);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

        builder.setContentTitle("Title").setContentText(message)
                .setSmallIcon(android.R.drawable.ic_dialog_alert)
                .setLargeIcon(largeIcon)
                .setAutoCancel(true)
                .setWhen(System.currentTimeMillis())
                .setStyle(new NotificationCompat.BigTextStyle().bigText(message));

        Notification notification = builder.build();
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        notificationManager.notify(101, notification);
    }