I am trying to display text in the Status Bar of Android Nougat just as time is displayed. I can display icons using NotificationCompat.Builder but not text. How to proceed?
If you're using NotificationCompat.Builder you can set text there
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
You can also address this
Solved the problem by converting String to Bitmap. Got the idea from here
Related
I'm seeing some apps showing background color in whole notification.
Myntra Notification with BackgroundColor
First Cry Notification with BackgroundColor
I tried solutions from these links but nothing worked.
Changing Notification RemoteViews Background Color
https://cazimirroman.medium.com/android-how-to-set-the-background-color-for-a-notification-in-a-foreground-service-eaa505e2b82d
Tried using DecoratedMediaCustomViewStyle ->
val mediaSession = MediaSessionCompat(context,"tag")
mediaSession.setFlags(0)
mediaSession.setPlaybackState(PlaybackStateCompat.Builder()
.setState(PlaybackStateCompat.STATE_NONE,0,0f)
.build())
val builder = NotificationCompat.Builder(context, "channelId1")
.setSmallIcon(R.drawable.ic_notification_small)
.setContentTitle("The Title")
.setPriority(NotificationCompat.PRIORITY_MAX)
.setWhen(System.currentTimeMillis())
.setOngoing(true)
.setAutoCancel(true)
.setColor(ContextCompat.getColor(context, R.color.pink))
.setColorized(true)
.setStyle(androidx.media.app.NotificationCompat.DecoratedMediaCustomViewStyle().setMediaSession(mediaSession.sessionToken))
In this case notification is coming black always (suppose to be pink) and also doesn't show all the information.
Notification using MediaStyle
You should create two custom layouts for your notifications, one for a collapsed notification and one for an expanded notification, then in your activity:
RemoteView collapsedRV = new RemoteViews (getPackageName(), R.layout.collapsed_notification)
RemoteView expandedRV = new RemoteViews (getPackageName(), R.layout.expanded_notification)
then in your builder you need to set the content view like this:
.setCustomContentView(collapsedRV)
.setCutomBigContentView(expandedRV)
.build()
notificationManager.notify(1,builder)
I would like to make the notification look a different color completely, I am using local notifications so I would like to be able to do this with NotificationCompat.Builder if possible.
Here's an example of what I would like the notification to look like:
Using notification builder methods
NotificationCompat.Builder(context, channelId)
.setColor(ContextCompat.getColor(this, R.color.primaryColor))
.setColorized(true)
.....
.build()
This is how I am setting the icons. However both the icons are not set. The default Android icon is shown for the large icon. How do I solve this?
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
.setSmallIcon(R.drawable.ic_alarm_on_white_24dp)
.setContentTitle(context.getResources().getString(R.string.app_name))
.setTicker(mTitle)
.setContentText(mTitle)
.setContentIntent(mClick)
.setAutoCancel(true)
.setOnlyAlertOnce(true);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(b)
.setContentTitle("this a message!")
.setContentText("Hello world");
can I use only setLagreIcon() or any other alternative solution?
From documentation:
A Notification object must contain the following:
A small icon, set by setSmallIcon()
A title, set by setContentTitle()
Detail text, set by setContentText()
So no, this is not possible.
Edit: A really unsafe and not recommended method seems to be explained here.
On Add a Big View Google show how to make use of BigTextStyle. The screenshot looks very promising:
So I can see a title "Emmet Connolly" which I can set with setContentTitle().
And I can see a bold header "Re: wearables" which I don't know how to set.
Finally there is some text "Our devs are goint to be building a lot of really awesome apps" which should be set with setContentText().
I played around with some setters from NotificationCompat.BigTextStyle and I can summarize that some are ignored. Seems to be a bug ?!
final NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender()
.setBackground(BitmapFactory.decodeResource(context.getResources(), R.drawable.chalk));
final NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("title")
.setContentText("text") // overwritten by bigText()
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("big text")
.setBigContentTitle("big content title") // ignored
.setSummaryText("summary text")) // ignored
.setContentIntent(viewPendingIntent)
.extend(wearableExtender);
final NotificationManagerCompat manager = NotificationManagerCompat.from(context);
manager.notify(notificationId, builder.build());
Html seems to work:
.setContentText(Html.fromHtml("<b>Best</b> restaurant <i>in</i> <font color=\"#0000ff\">San</font> Francisco <a href=\"http://www.stackoverflow.com\">#food"))
An here is a useful link: HTML Tags Supported By TextView