Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to expand notifications in notification drawer of android

Here I'm displaying message into custom notification but I'm not able to make it expandable.If I display long text message it gets cut from the end. I have tried so far many things as:

  Bitmap icon = BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        mBuilder.setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("New message from " + chatContacts.getName())
                .setContentText("")
                .setLargeIcon(icon)
                .setAutoCancel(true)
                .setWhen(System.currentTimeMillis())
                .setStyle(new NotificationCompat.InboxStyle()
                        .setBigContentTitle("" + message)
                        .setSummaryText("New message from " + chatContacts.getName()))
                .setContentIntent(contentIntent);
    } else if (Build.VERSION_CODES.M >= Build.VERSION_CODES.KITKAT) {
        NotificationCompat.InboxStyle inboxStyle =
                new NotificationCompat.InboxStyle();
        String[] events = new String[6];
        inboxStyle.setBigContentTitle("Event tracker details:");
        inboxStyle.addLine(message);
        mBuilder.setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(chatContacts.getName())
                .setAutoCancel(true)
                .setStyle(inboxStyle)
                .setWhen(System.currentTimeMillis())
                .setContentIntent(contentIntent);
    } else {
        mBuilder.setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(getString(R.string.app_name))
                .setCustomBigContentView(contentView)
                .setWhen(System.currentTimeMillis()).setAutoCancel(true)
                .setContentIntent(contentIntent);
    }
    NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
    bigText.bigText(message);
    mBuilder.setStyle(bigText);
    nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(0, mBuilder.build());
like image 284
Anshul Tyagi Avatar asked Oct 29 '22 01:10

Anshul Tyagi


1 Answers

Finally I resolved this problem which is working fine for Android 7.0, 6.0 and 5.0 without creating any custom layout for notifications. And different actions on lower section is also working fine. Notifications gets cancelled also after redirecting to their activities.

// Create the reply action and add the remote input.
        Notification.Action actionLead =
                new Notification.Action.Builder(0,
                        "Lead Info", contentLeadInfoIntent)
                        .build();
        Notification.Action actionRecordFollowUp =
                new Notification.Action.Builder(0,
                        "Record Follow up", contentRecordFollowupIntent)
                        .build();

        // Build the notification and add the action.
        Notification newMessageNotification =
                new Notification.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("Follow Up Alert")
                        .setContentText(contentText)
                        .setStyle(new Notification.BigTextStyle().bigText(contentText))
                        .addAction(actionLead)
                        .addAction(actionRecordFollowUp)
                        .setDefaults(NotificationCompat.DEFAULT_SOUND)
                        .setContentIntent(contentRecordFollowupIntent)
                        .setColor(getResources().getColor(R.color.colorPrimary))
                        .build();

        // Issue the notification.
        notificationManager.notify(NOTIFICATION_ID, newMessageNotification);
like image 138
Anshul Tyagi Avatar answered Nov 15 '22 06:11

Anshul Tyagi