Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Push Notification - How to show multiline message in Notification

Getting Push Notification's message only in a single line, whereas I was expecting it in multiple lines with BigPictureStyle and Base (both the Notification styles).

See the attached Screenshot, whereas in this image, we are just showing "Hello from Firebase Cloud Messaging" so this fits in single line itself.

But the fact is, If I am trying to show "Hello from Firebase Cloud Messaging and again Hello from Firebase Cloud Messaging" even then I am getting Message in single line only with three dots at the end like this ...

enter image description here

Here is the required part of the code, I am using:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setLargeIcon(image)/*Notification icon image*/
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(messageTitle)
                .setContentText(messageBody)
                .setStyle(new NotificationCompat.BigPictureStyle()
                        .bigPicture(image)
                        .setBigContentTitle(messageTitle)
                        .setSummaryText(Html.fromHtml(messageBody)
                        .toString()))/*Notification with Image*/
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

NOTE: Same issue, I am facing whenever, I am sending message without Big Image (Base / Simple Push Notification)

See this screenshot:

enter image description here

So, the only concern is How to show multiline message in Notification ?.

like image 983
Sophie Avatar asked Aug 14 '17 01:08

Sophie


People also ask

How do I show multiple notifications on Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do you handle Firebase push notifications?

A user tap on a notification opens the app launcher by default. Messages with both notification and data payload, when received in the background. In this case, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

How can Android handle multiple push notifications?

If you have multiple push providers you will need create your own Messaging Service to handle push notifications. You will need to pass new tokens to Swrve and make sure Swrve is set to handle incoming notifications.


5 Answers

You almost had it, you just need to set the bigText property.

    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setAutoCancel(true);

    NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
    bigTextStyle.setBigContentTitle("Title");
    bigTextStyle.bigText("Lorem ipsum dolor sit amet, consectetur adipiscing elit," +
            " sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " +
            "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris " +
            "nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in.");

    notificationBuilder.setStyle(bigTextStyle);

    NotificationManager mNotifyMgr = (NotificationManager)
            getSystemService(NOTIFICATION_SERVICE);
    mNotifyMgr.notify(1, notificationBuilder.build());

multiline

like image 70
Dus Avatar answered Oct 20 '22 20:10

Dus


try 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);
like image 32
Nikhil Jadhav Avatar answered Oct 20 '22 18:10

Nikhil Jadhav


You should use your own layout:

Builder builder = new Builder(context);
builder.setSmallIcon(R.drawable.star_white)
builder.setAutoCancel(true)
builder.setContentIntent(pendingIntent);
builder.setPriority(NotificationCompat.PRIORITY_MAX);

RemoteViews remoteViewsBig = new RemoteViews(context.getPackageName(), R.layout.notification_big);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.notification_small);
remoteViewsBig.setImageViewBitmap(R.id.banner, bitmap);

remoteViewsBig.setTextViewText(R.id.button, data.getButtonText());
remoteViews.setTextViewText(R.id.button, data.getButtonText());

remoteViewsBig.setTextViewText(R.id.text_title, data.getTitle());
remoteViews.setTextViewText(R.id.text_title, data.getTitle());

remoteViewsBig.setTextViewText(R.id.text_content, data.getContent());
remoteViews.setTextViewText(R.id.text_content, data.getContent());

builder.setCustomContentView(remoteViews);
builder.setCustomBigContentView(remoteViewsBig);

NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, builder.build());

And xml files:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="@color/custom_blue_gray_800"
              android:orientation="horizontal"
    >

    <ImageView
        android:layout_width="44dp"
        android:layout_height="44dp"
        android:layout_gravity="center"
        android:src="@mipmap/ic_launcher"
        />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:padding="8dp"
        >

        <TextView
            android:id="@+id/text_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:textSize="16sp"
            tools:text="Title"
            />

        <TextView
            android:id="@+id/text_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/darker_gray"
            android:textSize="14sp"
            tools:text="text\ntext\ntext"
            />

    </LinearLayout >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="36dp"
        android:layout_gravity="center_vertical"
        android:background="@drawable/button_main"
        android:text="Click"
        android:textColor="@android:color/white"
        android:textSize="12sp"
        />

</LinearLayout >

and

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="@color/custom_blue_gray_800"
              android:orientation="vertical"
              android:paddingLeft="12dp"
              android:paddingRight="12dp"
              android:paddingBottom="12dp"
    >

    <include layout="@layout/notification_small" />

    <ImageView
        android:id="@+id/banner"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        />


</LinearLayout >
like image 37
mac229 Avatar answered Oct 20 '22 19:10

mac229


You can show multiple line using @Dus answer...But with bigPictureSyle it will always have single line in setSummaryText method and other option is to create RemoteView and implement the UI as per your required design

For RemoteView Refer this

like image 32
Burhanuddin Rashid Avatar answered Oct 20 '22 19:10

Burhanuddin Rashid


I hope this is used to you. Check 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);

Refer this link: Display Notification in multiline

Thank you...

like image 40
Vicky Mahale Avatar answered Oct 20 '22 19:10

Vicky Mahale