Search code examples
androidpush-notificationfirebase-notifications

Remove 3 dots from notification Payload


What I've done:

I've done with the notification part, which is displaying multiple notification in notification tray without overriding it.

What I want to achieve:

I want to remove three dots(...) which are displayed after the 7th notification in data payload of notification tray. Just like in the 2nd image there are no continuation dots after 7th notification in WhatsApp. I've done lots of research but didn't found any feasible solution.

Image 1

enter image description here

Image 2

enter image description here

Below is my code:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.fcmdemo">

    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/icon_btn_volume"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
        <service
            android:name=".MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>
        <receiver android:name="NotificationActionReceiver">
            <intent-filter>
                <action android:name="CONFIRM" />
                <action android:name="CANCEL" />
            </intent-filter>
        </receiver>
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/icon_btn_volume" />
        <meta-data android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorAccent" />
    </application>

</manifest> 

MyFirebaseMessagingService.java

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
    public static ArrayList<String> notifications = new ArrayList<>();
    private String remoteMSG;

    @TargetApi(Build.VERSION_CODES.M)
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //Log data to Log Cat
        Log.e(TAG, "From: " + remoteMessage.getFrom());
        Log.e(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());

        remoteMSG = remoteMessage.getNotification().getBody();
        notifications.add(remoteMessage.getNotification().getBody());
        sendNotification(remoteMessage.getNotification().getBody());

    }

    private void sendNotification(String messageBody) {
        int i;
        //onDismiss Intent
        Intent intent = new Intent(this, NotificationActionReceiver.class);
        PendingIntent broadcastIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);

        //OnClick Listener
        startWFApplication().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, startWFApplication(),
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.icon_btn_volume)
                .setContentTitle("Title")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
        NotificationCompat.InboxStyle inboxStyle =
                new NotificationCompat.InboxStyle();
        // Sets a title for the Inbox in expanded layout
        inboxStyle.setBigContentTitle("Title - Notification");
        inboxStyle.setSummaryText("You have " + notifications.size() + " Notifications.");

        // Moves events into the expanded layout
        //notifications.add(messageBody);
        for (i = 0; i < notifications.size(); i++) {
            inboxStyle.addLine(notifications.get(i));
        }


        // Moves the expanded layout object into the notification object.
        notificationBuilder.setStyle(inboxStyle);

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

        notificationManager.notify(0, notificationBuilder.build());
        notificationBuilder.setDeleteIntent(broadcastIntent);
    }

    public static Intent startWFApplication() {
        Intent launchIntent = new Intent();
        launchIntent.setComponent(new ComponentName("com.fcmdemo", "com.fcmdemo.MyFirebaseMessagingService"));
        return launchIntent;
    }

    @Override
    public void handleIntent(Intent intent) {
        super.handleIntent(intent);
        Log.e(TAG, "handleIntent" + remoteMSG);
    }
}

Solution

  • Finally I found the solution. I've just put the condition before adding it.

    for (i = 0; i < notifications.size(); i++) {
                if (i <= 6) {
                    inboxStyle.addLine(notifications.get(i));
                    inboxStyle.setSummaryText("You have " + notifications.size() + " Notifications.");
                } else {
                    inboxStyle.setSummaryText("You have " + notifications.size() + " Notifications.");
                }
     }