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);
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 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
I'm trying this code,
NotificationCompat.Builder nfBuilder = new NotificationCompat.Builder(
context)
..setContentTitle(
"XYZ")
.setContentText("ABC")
.setContentIntent(pIntent)
.setDefaults(Notification.DEFAULT_ALL)
.setOnlyAlertOnce(true)
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_HIGH)
.setSmallIcon(R.drawable.woj_ic_launcher);
Notification notification = nfBuilder.build();
NotificationManager nfManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
nfManager.notify(requestCode, notification);
Problem is, it works fine with all other platforms but with lollipop, it shows very small icon with grey circle around it. I tried changing icon sizes and using setLargeIcon() method but still no joy.
The image should have square proportion.
Use this tool (https://romannurik.github.io/AndroidAssetStudio/icons-notification.html) and then make sure you use the "Api v11" icons, since they have the square proportion you need, the older version had a little more height.
Recap:
I got to tell I don't really see you icon being too small, in fact that's the greatest icon size I could get, look..
And for the "grey" background issue, is something like Notification.Builder.setColor(Color.RED) not working for you?
This is how it sorted out finally:
NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(getApplicationContext());
nBuilder.setContentTitle("notificationTitle");
nBuilder.setSmallIcon(R.mipmap.ic_launcher);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher1);
nBuilder.setLargeIcon(bitmap);
nBuilder.setContentText(notificationText);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, nBuilder.build());
Get a drawable/mipmap.
Convert it to bitmap.
Set the bitmap using setLargeIcon(Bitmap bitmap) method of NotificationCompat.Builder.
Note:setSmallIcon() is a mendatory method here. You can't skip it.
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
I have a notification of battery level. I want to make different icon in status bar and notifications center. In status bar: number (2 digits). In body: my app icon.
How can I do that? The app icon is also in the status bar, how can I change that?
I'm not sure what your code looks like right now but I'd do something like this:
Notification.Builder nb = new Notification.Builder(context)
.setContentTitle("title")
.setContentText("content")
.setAutoCancel(true)
.setLargeIcon(R.drawable.large_icon)
.setSmallIcon(R.drawable.small_icon)
.setTicker("ticker text");
NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(100, nb.build());
Note the setLargeIcon() and setSmallIcon(). The icon you want to show in the 'ticker' should be set in setSmallIcon() and for what you have called the 'notification centre', you should use setLargeIcon(). That should work.