Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't display more text in a notification

Tags:

android

I am trying to show a notification in the title bar with a long text.

    PendingIntent contentIntent = PendingIntent.getActivity(context,
            NOTIFICATION_ID, notificationIntent,
            PendingIntent.FLAG_ONE_SHOT);
    NotificationManager nm = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            context);
    builder.setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.icon_push).setTicker(alert)
            .setContentTitle(title).setContentText(alert)
            .setWhen(System.currentTimeMillis()).setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        builder.setStyle(new NotificationCompat.BigTextStyle()
                .bigText(title));
    }
    Notification n = builder.build();
    nm.notify(id, n);

But the builder.setStyle(new NotificationCompat.BigTextStyle() .bigText(title));

The setStyle seems to do nothing, I am testing it on android 4.1

like image 576
user1940676 Avatar asked Feb 15 '23 20:02

user1940676


2 Answers

You should remove this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {

because this is compatible, it's automatically set right.

And this is 100% working code:

        NotificationManager notificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            this);
    builder.setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("titletitletitletitletitletitletitletitletitletitletitletitle").setContentText("contentcontentcontentcontentcontentcontentcontent")
            .setWhen(System.currentTimeMillis()).setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText("bigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbig"));

    Notification notification = builder.build();
    notificationManager.notify(1, notification);
like image 88
artemiygrn Avatar answered Feb 22 '23 22:02

artemiygrn


If your variable names are correct and your variable named title is only the title then your problem is that you are using bigText(title) instead of bigText(aReallyBigText);

like image 42
Sherif elKhatib Avatar answered Feb 22 '23 20:02

Sherif elKhatib