1

Here is the Inshorts App Notification screenshotWe are pushing the notifications from Web App built in React JS. The notification title has around 95 characters and gets cropped when received on the phone. How do we wrap the title like in Inshorts App? We don't have any issues with when the app is in the foreground.

here is our React JS code to push the notification:

message = {
            notification: {
              title:"Long title with 95 characters",                  
              seoLink:"google.co.in",               
               image: "abc.jpg",            
              content_available: 'true'
            },           
            data: { 
            title:"Long title with 95 characters",           
            body: "body msg",                         
            seoLink:"google.co.in",             
               image: "abc.jpg",                
            },
            to: fcmToken
          };

This is the code we are using in Android Native:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(image)
                .setContentText(title)
                .setAutoCancel(true)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(title))
                .setColor(context.getResources().getColor(R.color.colorPrimary))
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setColor(context.getResources().getColor(R.color.colorPrimary))
                .setVibrate(vibrate)
                .setLights(0xff00ff00, 300, 100)
                .setWhen(System.currentTimeMillis());
Narasappa
  • 546
  • 1
  • 9
  • 31

1 Answers1

0

If you need a custom layout, you can apply NotificationCompat.DecoratedCustomViewStyle to your notification.

Create a custom xml file called notification_small.xml:

<TextView
    android:layout_width="wrap_content"  //here for the wrap content for the title
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:text="@string/notification_title"
    android:id="@+id/notification_title"
    style="@style/TextAppearance.Compat.Notification.Title" />

Then build your Notification like this:

// Get the layouts to use in the custom notification
RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_small);
RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_large);

// Apply the layouts to the notification
Notification customNotification = new NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
        .setCustomContentView(notificationLayout) 
        .setCustomBigContentView(notificationLayoutExpanded)
        .build();

You can read more about Custom notification layout here :

https://developer.android.com/training/notify-user/custom-notification

For your problem that in background:

Read this

ken
  • 2,426
  • 5
  • 43
  • 98
  • Thank you so much.This is absolutely working fine for the app when it is in foreground but still not getting how to get title wrapped when app is killed or is in background – Narasappa Jan 03 '20 at 10:58
  • how ur notification look like now? – ken Jan 03 '20 at 11:12
  • bro read this : https://stackoverflow.com/a/50642386/4332049, remove the notification payload,just left the data payload – ken Jan 03 '20 at 11:30
  • yes,read that. we are using FCM to send the notification. we need both the payloads while sending through firebase,only data payload doesn't work. we have tried it. we don't get notification without notification payload – Narasappa Jan 04 '20 at 09:01
  • FCM only support notification payload.. But if u have notification payload,then your custom notification appearance in device wont work..Therefore you have to choose either one.. – ken Jan 04 '20 at 09:08
  • That's what I am thinking. Is there any other cloud messaging service which serves our purpose? – Narasappa Jan 06 '20 at 06:49
  • What I suggest is, you send all the notification from your own,but not from firebase dashboard.. Means that,you build your own interface(form),then send the request ur own server to send the request to Firebase by using `data_payload`,see this: https://firebase.google.com/docs/cloud-messaging/send-message. By that,you can only set `data_payload` in ur app. – ken Jan 06 '20 at 08:43
  • Thank you so much for your help. managed to remove notification payload and wrap the text. your suggestion helped us. – Narasappa Jan 09 '20 at 07:00
  • nice bro..happy coding – ken Jan 09 '20 at 07:16