61

How to make a long notification multiline. I am using the below code snippet but its not working:

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();
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
teja
  • 661
  • 2
  • 6
  • 5

5 Answers5

130

Add this :

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);

Also check : Multiline Notification

Siddharth_Vyas
  • 9,972
  • 10
  • 39
  • 69
  • 1
    Works, details here: http://developer.android.com/reference/android/app/Notification.BigTextStyle.html – user2345998 Mar 17 '15 at 11:31
  • Seems link is down. Here's how it was : https://web.archive.org/web/20190812093224/https://www.vogella.com/tutorials/AndroidNotifications/article.html – android developer Jan 13 '20 at 06:15
  • 2
    Could you explain what lines you added? First the lines do not have the same order as in the question, second you could use English to explain your solution rather than just writing unformatted unordered code. – Vince Aug 15 '20 at 21:02
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());
Dhiraj Kumar
  • 173
  • 1
  • 3
  • 4
    setStyle is being called twice. Is it by mistake? if not, how does this work? – User12111111 Aug 29 '17 at 12:50
  • 1
    You can't use `bigTextStyle` twice, the second time will override the first one. Title, Context, BigText those can be different values. Big text shown if notification is expanded or user expand it himself – M.Baraka Nov 06 '18 at 15:31
5

Try this code working fine with android Oreo notification channel 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());
}
Sagar Jethva
  • 986
  • 12
  • 26
3

In your NotificationCompat.Builder add

.setStyle(NotificationCompat.BigTextStyle().bigText(body))
.setContentText(body) 

alnog with .setContentText(body)

Kishan Solanki
  • 13,761
  • 4
  • 85
  • 82
-2
Notification notification = new Notification.Builder(context)
         .setContentTitle(title)  
         .setSmallIcon(icon) 
         .setStyle(new Notification.BigTextStyle().bigText(notificationMessage))
         .setAutoCancel(true)
         .setContentIntent(intent)
         .build();
Narendran
  • 715
  • 7
  • 8
  • 1
    The code of `setStyle(new Notification.BigTextStyle()` has to be `setStyle(new NotificationCompat.BigTextStyle()`. Otherwise it throws an error as `BigTextStyle cannot be converted to Style`. – efkan Sep 08 '16 at 10:04